diff --git a/README.md b/README.md index 8a9e221..796a79b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A git submodule that shows example async-graphql projects. - [tide] Examples for `tide` - [rocket] Examples for `rocket` - [axum] Examples for `axum` +- [loco] Examples for `loco` - [federation] Examples for [Apollo Federation](https://www.apollographql.com/docs/federation/) diff --git a/loco/starwars/.cargo/config.toml b/loco/starwars/.cargo/config.toml new file mode 100644 index 0000000..5ebf033 --- /dev/null +++ b/loco/starwars/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +loco = "run --" diff --git a/loco/starwars/Cargo.toml b/loco/starwars/Cargo.toml new file mode 100644 index 0000000..6136bed --- /dev/null +++ b/loco/starwars/Cargo.toml @@ -0,0 +1,26 @@ +[workspace] + +[package] +name = "loco-starwars" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] } +eyre = "*" +tokio = { version = "1.33.0", default-features = false } +async-trait = "0.1.74" + +axum = "0.7.1" + +# async-graphql dependencies +async-graphql = { path = "../../.." } +async-graphql-axum = { path = "../../../integrations/axum" } +starwars = { path = "../../models/starwars" } + +[[bin]] +name = "starwars-cli" +path = "src/bin/main.rs" +required-features = [] diff --git a/loco/starwars/README.md b/loco/starwars/README.md new file mode 100644 index 0000000..55aba3c --- /dev/null +++ b/loco/starwars/README.md @@ -0,0 +1,36 @@ +# async-graphql with Loco :train: + +Example async-graphql project with [Loco](https://github.com/loco-rs/loco). + +## Quick Start + +Start your app: + +``` +$ cargo loco start +Finished dev [unoptimized + debuginfo] target(s) in 21.63s + Running `target/debug/myapp start` + + : + : + : + +controller/app_routes.rs:203: [Middleware] Adding log trace id + + ▄ ▀ + ▀ ▄ + ▄ ▀ ▄ ▄ ▄▀ + ▄ ▀▄▄ + ▄ ▀ ▀ ▀▄▀█▄ + ▀█▄ +▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█ + ██████ █████ ███ █████ ███ █████ ███ ▀█ + ██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄ + ██████ █████ ███ █████ █████ ███ ████▄ + ██████ █████ ███ █████ ▄▄▄ █████ ███ █████ + ██████ █████ ███ ████ ███ █████ ███ ████▀ + ▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀ + ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + +started on port 5150 +``` diff --git a/loco/starwars/config/development.yaml b/loco/starwars/config/development.yaml new file mode 100644 index 0000000..fa21014 --- /dev/null +++ b/loco/starwars/config/development.yaml @@ -0,0 +1,45 @@ +# Loco configuration file documentation + +# Application logging configuration +logger: + # Enable or disable logging. + enable: true + # Log level, options: trace, debug, info, warn or error. + level: debug + # Define the logging format. options: compact, pretty or json + format: compact + # By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries + # Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters. + # override_filter: trace + +# Web server configuration +server: + # Port on which the server will listen. the server binding is 0.0.0.0:{PORT} + port: 5150 + # The UI hostname or IP address that mailers will point to. + host: http://localhost + # Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block + middlewares: + # Enable Etag cache header middleware + etag: + enable: true + # Allows to limit the payload size request. payload that bigger than this file will blocked the request. + limit_payload: + # Enable/Disable the middleware. + enable: true + # the limit size. can be b,kb,kib,mb,mib,gb,gib + body_limit: 5mb + # Generating a unique request ID and enhancing logging with additional information such as the start and completion of request processing, latency, status code, and other request details. + logger: + # Enable/Disable the middleware. + enable: true + # when your code is panicked, the request still returns 500 status code. + catch_panic: + # Enable/Disable the middleware. + enable: true + # Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned. + timeout_request: + # Enable/Disable the middleware. + enable: false + # Duration time in milliseconds. + timeout: 5000 diff --git a/loco/starwars/src/app.rs b/loco/starwars/src/app.rs new file mode 100644 index 0000000..6f0d33e --- /dev/null +++ b/loco/starwars/src/app.rs @@ -0,0 +1,42 @@ +use async_trait::async_trait; +use loco_rs::{ + app::{AppContext, Hooks}, + boot::{create_app, BootResult, StartMode}, + controller::AppRoutes, + environment::Environment, + task::Tasks, + worker::Processor, + Result, +}; + +use crate::controllers; + +pub struct App; +#[async_trait] +impl Hooks for App { + fn app_name() -> &'static str { + env!("CARGO_CRATE_NAME") + } + + fn app_version() -> String { + format!( + "{} ({})", + env!("CARGO_PKG_VERSION"), + option_env!("BUILD_SHA") + .or(option_env!("GITHUB_SHA")) + .unwrap_or("dev") + ) + } + + async fn boot(mode: StartMode, environment: &Environment) -> Result { + create_app::(mode, environment).await + } + + fn routes(_ctx: &AppContext) -> AppRoutes { + AppRoutes::empty().add_route(controllers::graphiql::routes()) + } + + fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {} + + fn register_tasks(_tasks: &mut Tasks) {} +} diff --git a/loco/starwars/src/bin/main.rs b/loco/starwars/src/bin/main.rs new file mode 100644 index 0000000..ffe90e7 --- /dev/null +++ b/loco/starwars/src/bin/main.rs @@ -0,0 +1,7 @@ +use loco_rs::cli; +use loco_starwars::app::App; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + cli::main::().await +} diff --git a/loco/starwars/src/controllers/graphiql.rs b/loco/starwars/src/controllers/graphiql.rs new file mode 100644 index 0000000..6263797 --- /dev/null +++ b/loco/starwars/src/controllers/graphiql.rs @@ -0,0 +1,18 @@ +use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema}; +use async_graphql_axum::GraphQL; +use axum::debug_handler; +use loco_rs::prelude::*; +use starwars::{QueryRoot, StarWars}; + +#[debug_handler] +async fn graphiql() -> Result { + format::html(&GraphiQLSource::build().endpoint("/").finish()) +} + +pub fn routes() -> Routes { + let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) + .data(StarWars::new()) + .finish(); + + Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema))) +} diff --git a/loco/starwars/src/controllers/mod.rs b/loco/starwars/src/controllers/mod.rs new file mode 100644 index 0000000..982badf --- /dev/null +++ b/loco/starwars/src/controllers/mod.rs @@ -0,0 +1 @@ +pub mod graphiql; diff --git a/loco/starwars/src/lib.rs b/loco/starwars/src/lib.rs new file mode 100644 index 0000000..cee1ed4 --- /dev/null +++ b/loco/starwars/src/lib.rs @@ -0,0 +1,2 @@ +pub mod app; +pub mod controllers; diff --git a/loco/subscription/.cargo/config.toml b/loco/subscription/.cargo/config.toml new file mode 100644 index 0000000..5ebf033 --- /dev/null +++ b/loco/subscription/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +loco = "run --" diff --git a/loco/subscription/Cargo.toml b/loco/subscription/Cargo.toml new file mode 100644 index 0000000..b6c4381 --- /dev/null +++ b/loco/subscription/Cargo.toml @@ -0,0 +1,25 @@ +[workspace] + +[package] +name = "loco-subscription" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] } +eyre = "*" +tokio = { version = "1.33.0", default-features = false } +async-trait = "0.1.74" +axum = "0.7.1" + +# async-graphql dependencies +async-graphql = { path = "../../.." } +async-graphql-axum = { path = "../../../integrations/axum" } +books = { path = "../../models/books" } + +[[bin]] +name = "starwars-cli" +path = "src/bin/main.rs" +required-features = [] diff --git a/loco/subscription/README.md b/loco/subscription/README.md new file mode 100644 index 0000000..55aba3c --- /dev/null +++ b/loco/subscription/README.md @@ -0,0 +1,36 @@ +# async-graphql with Loco :train: + +Example async-graphql project with [Loco](https://github.com/loco-rs/loco). + +## Quick Start + +Start your app: + +``` +$ cargo loco start +Finished dev [unoptimized + debuginfo] target(s) in 21.63s + Running `target/debug/myapp start` + + : + : + : + +controller/app_routes.rs:203: [Middleware] Adding log trace id + + ▄ ▀ + ▀ ▄ + ▄ ▀ ▄ ▄ ▄▀ + ▄ ▀▄▄ + ▄ ▀ ▀ ▀▄▀█▄ + ▀█▄ +▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█ + ██████ █████ ███ █████ ███ █████ ███ ▀█ + ██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄ + ██████ █████ ███ █████ █████ ███ ████▄ + ██████ █████ ███ █████ ▄▄▄ █████ ███ █████ + ██████ █████ ███ ████ ███ █████ ███ ████▀ + ▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀ + ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ + +started on port 5150 +``` diff --git a/loco/subscription/config/development.yaml b/loco/subscription/config/development.yaml new file mode 100644 index 0000000..fa21014 --- /dev/null +++ b/loco/subscription/config/development.yaml @@ -0,0 +1,45 @@ +# Loco configuration file documentation + +# Application logging configuration +logger: + # Enable or disable logging. + enable: true + # Log level, options: trace, debug, info, warn or error. + level: debug + # Define the logging format. options: compact, pretty or json + format: compact + # By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries + # Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters. + # override_filter: trace + +# Web server configuration +server: + # Port on which the server will listen. the server binding is 0.0.0.0:{PORT} + port: 5150 + # The UI hostname or IP address that mailers will point to. + host: http://localhost + # Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block + middlewares: + # Enable Etag cache header middleware + etag: + enable: true + # Allows to limit the payload size request. payload that bigger than this file will blocked the request. + limit_payload: + # Enable/Disable the middleware. + enable: true + # the limit size. can be b,kb,kib,mb,mib,gb,gib + body_limit: 5mb + # Generating a unique request ID and enhancing logging with additional information such as the start and completion of request processing, latency, status code, and other request details. + logger: + # Enable/Disable the middleware. + enable: true + # when your code is panicked, the request still returns 500 status code. + catch_panic: + # Enable/Disable the middleware. + enable: true + # Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned. + timeout_request: + # Enable/Disable the middleware. + enable: false + # Duration time in milliseconds. + timeout: 5000 diff --git a/loco/subscription/src/app.rs b/loco/subscription/src/app.rs new file mode 100644 index 0000000..3cd8413 --- /dev/null +++ b/loco/subscription/src/app.rs @@ -0,0 +1,54 @@ +use async_graphql::Schema; +use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot}; + +use async_graphql_axum::GraphQLSubscription; +use async_trait::async_trait; +use axum::Router as AxumRouter; +use loco_rs::{ + app::{AppContext, Hooks}, + boot::{create_app, BootResult, StartMode}, + controller::AppRoutes, + environment::Environment, + task::Tasks, + worker::Processor, + Result, +}; + +use crate::controllers; + +pub struct App; +#[async_trait] +impl Hooks for App { + fn app_name() -> &'static str { + env!("CARGO_CRATE_NAME") + } + + fn app_version() -> String { + format!( + "{} ({})", + env!("CARGO_PKG_VERSION"), + option_env!("BUILD_SHA") + .or(option_env!("GITHUB_SHA")) + .unwrap_or("dev") + ) + } + + async fn boot(mode: StartMode, environment: &Environment) -> Result { + create_app::(mode, environment).await + } + + fn routes(_ctx: &AppContext) -> AppRoutes { + AppRoutes::empty().add_route(controllers::graphiql::routes()) + } + + async fn after_routes(router: AxumRouter, _ctx: &AppContext) -> Result { + let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot) + .data(Storage::default()) + .finish(); + Ok(router.route_service("/ws", GraphQLSubscription::new(schema))) + } + + fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {} + + fn register_tasks(_tasks: &mut Tasks) {} +} diff --git a/loco/subscription/src/bin/main.rs b/loco/subscription/src/bin/main.rs new file mode 100644 index 0000000..90f3acc --- /dev/null +++ b/loco/subscription/src/bin/main.rs @@ -0,0 +1,7 @@ +use loco_rs::cli; +use loco_subscription::app::App; + +#[tokio::main] +async fn main() -> eyre::Result<()> { + cli::main::().await +} diff --git a/loco/subscription/src/controllers/graphiql.rs b/loco/subscription/src/controllers/graphiql.rs new file mode 100644 index 0000000..e39a1d4 --- /dev/null +++ b/loco/subscription/src/controllers/graphiql.rs @@ -0,0 +1,23 @@ +use async_graphql::{http::GraphiQLSource, Schema}; +use async_graphql_axum::GraphQL; +use axum::debug_handler; +use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot}; +use loco_rs::prelude::*; + +#[debug_handler] +async fn graphiql() -> Result { + format::html( + &GraphiQLSource::build() + .endpoint("/") + .subscription_endpoint("/ws") + .finish(), + ) +} + +pub fn routes() -> Routes { + let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot) + .data(Storage::default()) + .finish(); + + Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema))) +} diff --git a/loco/subscription/src/controllers/mod.rs b/loco/subscription/src/controllers/mod.rs new file mode 100644 index 0000000..982badf --- /dev/null +++ b/loco/subscription/src/controllers/mod.rs @@ -0,0 +1 @@ +pub mod graphiql; diff --git a/loco/subscription/src/lib.rs b/loco/subscription/src/lib.rs new file mode 100644 index 0000000..cee1ed4 --- /dev/null +++ b/loco/subscription/src/lib.rs @@ -0,0 +1,2 @@ +pub mod app; +pub mod controllers;