mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
adding rust, fixing python/php, adding ability to detect rusts src/main.rs file
This commit is contained in:
@@ -33,7 +33,8 @@ And now with the JSON input:
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
That's it!
|
||||
That's it! Our `fn deploy` packaged our function and sent it to the Oracle Functions server. Try editing `func.js`
|
||||
and then doing another `fn deploy`.
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@ And now with the JSON input:
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
That's it!
|
||||
That's it! Our `fn deploy` packaged our function and sent it to the Oracle Functions server. Try editing `func.php`
|
||||
and then doing another `fn deploy`.
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
FROM funcy/python:2
|
||||
|
||||
WORKDIR /app
|
||||
ADD . /app
|
||||
|
||||
ENTRYPOINT ["python", "hello.py"]
|
||||
@@ -1,48 +1,67 @@
|
||||
## Quick Example for a Python Function (4 minutes)
|
||||
# Tutorial 1: Python Function w/ Input (3 minutes)
|
||||
|
||||
This example will show you how to test and deploy Go (Golang) code to Oracle Functions.
|
||||
This example will show you how to test and deploy Python code to Oracle Functions. It will also demonstrate passing data in through stdin.
|
||||
|
||||
### 1. Prepare the `func.yaml` file:
|
||||
|
||||
At func.yaml you will find:
|
||||
|
||||
```yml
|
||||
name: USERNAME/hello
|
||||
version: 0.0.1
|
||||
path: /hello
|
||||
build:
|
||||
- docker run --rm -v "$PWD":/worker -w /worker funcy/python:2-dev pip install -t packages -r requirements.txt
|
||||
```
|
||||
|
||||
The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note:
|
||||
the application name is `pythonapp` and the route for incoming requests is `/hello`. These informations are relevant for
|
||||
the moment you try to test this function.
|
||||
|
||||
### 2. Build:
|
||||
### First, run the following commands:
|
||||
|
||||
```sh
|
||||
# build the function
|
||||
fn build
|
||||
# test it
|
||||
# Initialize your function creating a func.yaml file
|
||||
fn init <DOCKERHUB_USERNAME>/hello
|
||||
|
||||
# Test your function.
|
||||
# This will run inside a container exactly how it will on the server. It will also install and vendor dependencies from Gemfile
|
||||
fn run
|
||||
|
||||
# Now try with an input
|
||||
cat hello.payload.json | fn run
|
||||
# push it to Docker Hub
|
||||
fn push
|
||||
# Create a route to this function on Oracle Functions
|
||||
fn routes create pythonapp /hello
|
||||
|
||||
# Deploy your functions to the Oracle Functions server (default localhost:8080)
|
||||
# This will create a route to your function as well
|
||||
fn deploy myapp
|
||||
```
|
||||
|
||||
`-v` is optional, but it allows you to see how this function is being built.
|
||||
|
||||
### 3. Queue jobs for your function
|
||||
|
||||
Now you can start jobs on your function. Let's quickly queue up a job to try it out.
|
||||
### Now call your function:
|
||||
|
||||
```sh
|
||||
cat hello.payload.json | fn call pythonapp /hello
|
||||
curl http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
Here's a curl example to show how easy it is to do in any language:
|
||||
Or call from a browser: [http://localhost:8080/r/myapp/hello](http://localhost:8080/r/myapp/hello)
|
||||
|
||||
And now with the JSON input:
|
||||
|
||||
```sh
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/pythonapp/hello
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
That's it! Our `fn deploy` packaged our function and sent it to the Oracle Functions server. Try editing `func.py`
|
||||
and then doing another `fn deploy`.
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
In Python, we create a [requirements](https://pip.pypa.io/en/stable/user_guide/) file in your function directory then `fn deploy` will build and deploy with these dependencies.
|
||||
|
||||
# In Review
|
||||
|
||||
1. We piped JSON data into the function at the command line
|
||||
```sh
|
||||
cat hello.payload.json | fn run
|
||||
```
|
||||
|
||||
2. We received our function input through **stdin**
|
||||
```python
|
||||
obj = json.loads(sys.stdin.read())
|
||||
```
|
||||
|
||||
3. We wrote our output to **stdout**
|
||||
```python
|
||||
print "Hello", name, "!"
|
||||
```
|
||||
|
||||
4. We sent **stderr** to the server logs
|
||||
```python
|
||||
sys.stderr.write("Starting Python Function\n")
|
||||
```
|
||||
|
||||
|
||||
# Next Up
|
||||
## [Tutorial 2: Input Parameters](examples/tutorial/params)
|
||||
18
examples/tutorial/hello/python/func.py
Normal file
18
examples/tutorial/hello/python/func.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
sys.path.append("packages")
|
||||
import os
|
||||
import json
|
||||
|
||||
sys.stderr.write("Starting Python Function\n")
|
||||
|
||||
name = "World"
|
||||
|
||||
try:
|
||||
if not os.isatty(sys.stdin.fileno()):
|
||||
obj = json.loads(sys.stdin.read())
|
||||
if obj["name"] != "":
|
||||
name = obj["name"]
|
||||
except:
|
||||
pass
|
||||
|
||||
print "Hello", name, "!"
|
||||
@@ -1,12 +0,0 @@
|
||||
import sys
|
||||
sys.path.append("packages")
|
||||
import os
|
||||
import json
|
||||
|
||||
name = "World"
|
||||
if not os.isatty(sys.stdin.fileno()):
|
||||
obj = json.loads(sys.stdin.read())
|
||||
if obj["name"] != "":
|
||||
name = obj["name"]
|
||||
|
||||
print "Hello", name, "!!!"
|
||||
@@ -33,7 +33,9 @@ And now with the JSON input:
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
That's it!
|
||||
That's it! Our `fn deploy` packaged our function and sent it to the Oracle Functions server. Try editing `func.rb`
|
||||
and then doing another `fn deploy`.
|
||||
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
|
||||
2
examples/tutorial/hello/rust/.gitignore
vendored
Normal file
2
examples/tutorial/hello/rust/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
func.yaml
|
||||
Cargo.lock
|
||||
@@ -1,33 +1,93 @@
|
||||
# Using rust with functions
|
||||
# Tutorial 1: Rust Function w/ Input (3 minutes)
|
||||
|
||||
This example will show you how to test and deploy Rust code to Oracle Functions. It will also demonstrate passing data in through stdin.
|
||||
|
||||
The easiest way to create a function in rust is via ***cargo*** and ***fn***.
|
||||
|
||||
## Prerequisites
|
||||
### Prerequisites
|
||||
|
||||
First create an epty rust project as follows:
|
||||
Create an empty rust project as follows:
|
||||
|
||||
```bash
|
||||
cargo init --name func --bin
|
||||
```
|
||||
|
||||
Make sure the project name is ***func*** and is of type ***bin***. Now just edit your code, once done you can create a function.
|
||||
Make sure the project name is ***func*** and is of type ***bin***.
|
||||
|
||||
## Creating a function
|
||||
Now put the following code in ```main.rs``` (can also copy directly from the file in this repo):
|
||||
|
||||
Simply run
|
||||
```rust
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
```bash
|
||||
fn init --runtime=rust <username>/<funcname>
|
||||
fn main() {
|
||||
let mut buffer = String::new();
|
||||
let stdin = io::stdin();
|
||||
if stdin.lock().read_to_string(&mut buffer).is_ok() {
|
||||
println!("Hello {}", buffer.trim());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This will create the ```func.yaml``` file required by functions, which can be built by running:
|
||||
|
||||
```bash
|
||||
fn build
|
||||
```
|
||||
### Now run the following commands:
|
||||
|
||||
## Testing
|
||||
```sh
|
||||
# Initialize your function creating a func.yaml file
|
||||
fn init <DOCKERHUB_USERNAME>/rust
|
||||
|
||||
```bash
|
||||
# Test your function. This will run inside a container exactly how it will on the server
|
||||
fn run
|
||||
|
||||
# Now try with an input (copy sample.payload.json from this repo)
|
||||
cat sample.payload.json | fn run
|
||||
|
||||
# Deploy your functions to the Oracle Functions server (default localhost:8080)
|
||||
# This will create a route to your function as well
|
||||
fn deploy myapp
|
||||
```
|
||||
### Now call your function:
|
||||
|
||||
```sh
|
||||
curl http://localhost:8080/r/myapp/rust
|
||||
```
|
||||
|
||||
Or call from a browser: [http://localhost:8080/r/myapp/rust](http://localhost:8080/r/myapp/rust)
|
||||
|
||||
And now with the JSON input:
|
||||
|
||||
```sh
|
||||
curl -H "Content-Type: application/json" -X POST -d @sample.payload.json http://localhost:8080/r/myapp/rust
|
||||
```
|
||||
|
||||
That's it!
|
||||
|
||||
### Note on Dependencies
|
||||
|
||||
|
||||
|
||||
# In Review
|
||||
|
||||
1. We piped JSON data into the function at the command line
|
||||
```sh
|
||||
cat sample.payload.json | fn run
|
||||
```
|
||||
|
||||
2. We received our function input through **stdin**
|
||||
```rust
|
||||
read_to_string(&mut buffer)
|
||||
```
|
||||
|
||||
3. We wrote our output to **stdout**
|
||||
```rust
|
||||
println!("Hello {}", buffer.trim());
|
||||
```
|
||||
|
||||
4. We sent **stderr** to the server logs
|
||||
```rust
|
||||
TODO
|
||||
```
|
||||
|
||||
|
||||
# Next Up
|
||||
## [Tutorial 2: Input Parameters](examples/tutorial/params)
|
||||
|
||||
3
examples/tutorial/hello/rust/sample.payload.json
Normal file
3
examples/tutorial/hello/rust/sample.payload.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "Johnny"
|
||||
}
|
||||
@@ -7,4 +7,5 @@ fn main() {
|
||||
if stdin.lock().read_to_string(&mut buffer).is_ok() {
|
||||
println!("Hello {}", buffer.trim());
|
||||
}
|
||||
//todo: decode json from payload
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user