Use a cargo workspace for better modularity
Currently, there is only a `gateway` crate, but in the future, likely more will be added to handle db stuff, abstractions, etc once I actually start implementing the city portal logic itself.
This commit is contained in:
parent
86374617c2
commit
2485c99c2d
8 changed files with 157 additions and 214 deletions
48
services/gateway/src/lib.rs
Normal file
48
services/gateway/src/lib.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, post},
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
mod models;
|
||||
mod resolvers;
|
||||
mod schema;
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn start() {
|
||||
// TODO: Implement abstractions, types, and everything else from a portal_core crate
|
||||
// to have something that can eventually dynamically change and be grown.
|
||||
// This is a big project, and I'm a small student,
|
||||
// so the focus should be on modularity and proof that it can
|
||||
// be grown more than anything else.
|
||||
let schema = schema::build_schema();
|
||||
|
||||
let app = Router::new()
|
||||
.route(
|
||||
"/graphql",
|
||||
post({
|
||||
let schema = schema.clone();
|
||||
move |req: GraphQLRequest| async move {
|
||||
GraphQLResponse::from(schema.execute(req.into_inner()).await)
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/graphql",
|
||||
get({
|
||||
let schema = schema.clone();
|
||||
move || async move { axum::Json(schema.sdl()) }
|
||||
}),
|
||||
);
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||
println!("listening on http://{}", addr);
|
||||
|
||||
axum::serve(
|
||||
tokio::net::TcpListener::bind(addr).await.unwrap(),
|
||||
app.into_make_service(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
7
services/gateway/src/models.rs
Normal file
7
services/gateway/src/models.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
use async_graphql::SimpleObject;
|
||||
|
||||
#[derive(SimpleObject)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
22
services/gateway/src/resolvers.rs
Normal file
22
services/gateway/src/resolvers.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use crate::models::User;
|
||||
use async_graphql::Object;
|
||||
|
||||
pub struct QueryRoot;
|
||||
|
||||
#[Object]
|
||||
impl QueryRoot {
|
||||
async fn hello(&self) -> &str {
|
||||
"Hello, world!"
|
||||
}
|
||||
|
||||
async fn testing(&self) -> &str {
|
||||
"Testing"
|
||||
}
|
||||
|
||||
async fn user(&self, id: i32) -> User {
|
||||
User {
|
||||
id,
|
||||
name: String::from("Zeph"),
|
||||
}
|
||||
}
|
||||
}
|
||||
8
services/gateway/src/schema.rs
Normal file
8
services/gateway/src/schema.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
use crate::resolvers::QueryRoot;
|
||||
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
|
||||
|
||||
pub type AppSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
|
||||
|
||||
pub fn build_schema() -> AppSchema {
|
||||
Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue