use typeway::prelude::*;
typeway_path!(type UsersPath = "users");
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToProtoType)]
struct User {
#[proto(tag = 1)]
id: u32,
#[proto(tag = 2)]
name: String,
}
#[derive(Debug, serde::Deserialize, ToProtoType)]
struct CreateUser {
#[proto(tag = 1)]
name: String,
}
type UserAPI = (
GetEndpoint<UsersPath, Vec<User>>,
PostEndpoint<UsersPath, CreateUser, User>,
);
async fn list_users() -> Json<Vec<User>> {
Json(vec![
User {
id: 1,
name: "Alice".into(),
},
User {
id: 2,
name: "Bob".into(),
},
])
}
async fn create_user(body: Json<CreateUser>) -> (http::StatusCode, Json<User>) {
let user = User {
id: 3,
name: body.0.name,
};
(http::StatusCode::CREATED, Json(user))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Server::<UserAPI>::new((bind!(list_users), bind!(create_user)))
.with_grpc("UserService", "users.v1")
.with_grpc_docs()
.serve("0.0.0.0:3000".parse()?)
.await
}