Files
fx-serverless/examples/functions/Rust/README.md
2019-12-06 14:58:30 +08:00

739 B
Vendored

Make a Rust function a service with fx

Write a function like,

pub mod fns {
    #[derive(Serialize)]
    pub struct Response {
        pub result: i32,
    }

    #[derive(Deserialize)]
    pub struct Request {
        pub a: i32,
        pub b: i32,
    }

    pub fn func(req: Request) -> Response {
        Response {
            result: req.a + req.b,
        }
    }
}

then deploy it with fx up command,

$ fx up -p 8080 func.rs

test it using curl

$ curl -X 'POST' --header 'Content-Type: application/json' --data '{"a":1,"b":1}' '0.0.0.0:3000'

HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json
Date: Fri, 06 Dec 2019 06:45:14 GMT
Server: Rocket

{
    "result": 2
}