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:
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