diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index eaca70f5..a43fb4e2 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -13,6 +13,7 @@ sea-orm = { version = "^0.9", features = ["sqlx-mysql", "runtime-async-std-nativ tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.34" } tracing-subscriber = { version = "0.3.11" } +lazy_static = { version = "1.4.0" } [dependencies.seaography] path = "../../" # remove this line in your own project diff --git a/examples/mysql/src/main.rs b/examples/mysql/src/main.rs index 836051c7..e95cd344 100644 --- a/examples/mysql/src/main.rs +++ b/examples/mysql/src/main.rs @@ -5,58 +5,66 @@ use async_graphql::{ }; use async_graphql_poem::GraphQL; use dotenv::dotenv; +use lazy_static::lazy_static; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; use seaography_mysql_example::*; use std::env; +lazy_static! { + static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); + static ref DATABASE_URL: String = + env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); + static ref DEPTH_LIMIT: Option = env::var("DEPTH_LIMIT").map_or(None, |data| Some( + data.parse().expect("DEPTH_LIMIT is not a number") + )); + static ref COMPLEXITY_LIMIT: Option = env::var("COMPLEXITY_LIMIT") + .map_or(None, |data| { + Some(data.parse().expect("COMPLEXITY_LIMIT is not a number")) + }); +} + #[handler] async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new("/"))) + Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } #[tokio::main] async fn main() { dotenv().ok(); - let db_url = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); - let depth_limit = env::var("DEPTH_LIMIT") - .map(|data| data.parse::().expect("DEPTH_LIMIT is not a number")) - .map_or(None, |data| Some(data)); - let complexity_limit = env::var("COMPLEXITY_LIMIT") - .map(|data| { - data.parse::() - .expect("COMPLEXITY_LIMIT is not a number") - }) - .map_or(None, |data| Some(data)); tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_test_writer() .init(); - let database = Database::connect(db_url).await.unwrap(); + + let database = Database::connect(&*DATABASE_URL) + .await + .expect("Fail to initialize database connection"); let orm_dataloader: DataLoader = DataLoader::new( OrmDataloader { db: database.clone(), }, tokio::spawn, ); - let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) + let mut schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) .data(database) .data(orm_dataloader); - let schema = if let Some(depth) = depth_limit { - schema.limit_depth(depth) - } else { - schema - }; - let schema = if let Some(complexity) = complexity_limit { - schema.limit_complexity(complexity) - } else { - schema - }; + if let Some(depth) = *DEPTH_LIMIT { + schema = schema.limit_depth(depth); + } + if let Some(complexity) = *COMPLEXITY_LIMIT { + schema = schema.limit_complexity(complexity); + } let schema = schema.finish(); - let app = Route::new().at("/", get(graphql_playground).post(GraphQL::new(schema))); - println!("Playground: http://localhost:8000"); - Server::new(TcpListener::bind("0.0.0.0:8000")) + let app = Route::new().at( + &*ENDPOINT, + get(graphql_playground).post(GraphQL::new(schema)), + ); + + println!("Visit GraphQL Playground at http://{}", *URL); + Server::new(TcpListener::bind(&*URL)) .run(app) .await - .unwrap(); + .expect("Fail to start web server"); } diff --git a/examples/postgres/Cargo.toml b/examples/postgres/Cargo.toml index 82acf37b..6b736d2a 100644 --- a/examples/postgres/Cargo.toml +++ b/examples/postgres/Cargo.toml @@ -13,6 +13,7 @@ sea-orm = { version = "^0.9", features = ["sqlx-postgres", "runtime-async-std-na tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.34" } tracing-subscriber = { version = "0.3.11" } +lazy_static = { version = "1.4.0" } [dependencies.seaography] path = "../../" # remove this line in your own project diff --git a/examples/postgres/src/main.rs b/examples/postgres/src/main.rs index f2ce4be7..414cc426 100644 --- a/examples/postgres/src/main.rs +++ b/examples/postgres/src/main.rs @@ -5,58 +5,66 @@ use async_graphql::{ }; use async_graphql_poem::GraphQL; use dotenv::dotenv; +use lazy_static::lazy_static; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; use seaography_postgres_example::*; use std::env; +lazy_static! { + static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); + static ref DATABASE_URL: String = + env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); + static ref DEPTH_LIMIT: Option = env::var("DEPTH_LIMIT").map_or(None, |data| Some( + data.parse().expect("DEPTH_LIMIT is not a number") + )); + static ref COMPLEXITY_LIMIT: Option = env::var("COMPLEXITY_LIMIT") + .map_or(None, |data| { + Some(data.parse().expect("COMPLEXITY_LIMIT is not a number")) + }); +} + #[handler] async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new("/"))) + Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } #[tokio::main] async fn main() { dotenv().ok(); - let db_url = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); - let depth_limit = env::var("DEPTH_LIMIT") - .map(|data| data.parse::().expect("DEPTH_LIMIT is not a number")) - .map_or(None, |data| Some(data)); - let complexity_limit = env::var("COMPLEXITY_LIMIT") - .map(|data| { - data.parse::() - .expect("COMPLEXITY_LIMIT is not a number") - }) - .map_or(None, |data| Some(data)); tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_test_writer() .init(); - let database = Database::connect(db_url).await.unwrap(); + + let database = Database::connect(&*DATABASE_URL) + .await + .expect("Fail to initialize database connection"); let orm_dataloader: DataLoader = DataLoader::new( OrmDataloader { db: database.clone(), }, tokio::spawn, ); - let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) + let mut schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) .data(database) .data(orm_dataloader); - let schema = if let Some(depth) = depth_limit { - schema.limit_depth(depth) - } else { - schema - }; - let schema = if let Some(complexity) = complexity_limit { - schema.limit_complexity(complexity) - } else { - schema - }; + if let Some(depth) = *DEPTH_LIMIT { + schema = schema.limit_depth(depth); + } + if let Some(complexity) = *COMPLEXITY_LIMIT { + schema = schema.limit_complexity(complexity); + } let schema = schema.finish(); - let app = Route::new().at("/", get(graphql_playground).post(GraphQL::new(schema))); - println!("Playground: http://localhost:8000"); - Server::new(TcpListener::bind("0.0.0.0:8000")) + let app = Route::new().at( + &*ENDPOINT, + get(graphql_playground).post(GraphQL::new(schema)), + ); + + println!("Visit GraphQL Playground at http://{}", *URL); + Server::new(TcpListener::bind(&*URL)) .run(app) .await - .unwrap(); + .expect("Fail to start web server"); } diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index 8750d129..d63771dd 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -13,6 +13,7 @@ sea-orm = { version = "^0.9", features = ["sqlx-sqlite", "runtime-async-std-nati tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.34" } tracing-subscriber = { version = "0.3.11" } +lazy_static = { version = "1.4.0" } [dependencies.seaography] path = "../../" # remove this line in your own project diff --git a/examples/sqlite/src/main.rs b/examples/sqlite/src/main.rs index 03de233a..2bfe1430 100644 --- a/examples/sqlite/src/main.rs +++ b/examples/sqlite/src/main.rs @@ -5,58 +5,66 @@ use async_graphql::{ }; use async_graphql_poem::GraphQL; use dotenv::dotenv; +use lazy_static::lazy_static; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; use seaography_sqlite_example::*; use std::env; +lazy_static! { + static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); + static ref DATABASE_URL: String = + env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); + static ref DEPTH_LIMIT: Option = env::var("DEPTH_LIMIT").map_or(None, |data| Some( + data.parse().expect("DEPTH_LIMIT is not a number") + )); + static ref COMPLEXITY_LIMIT: Option = env::var("COMPLEXITY_LIMIT") + .map_or(None, |data| { + Some(data.parse().expect("COMPLEXITY_LIMIT is not a number")) + }); +} + #[handler] async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new("/"))) + Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } #[tokio::main] async fn main() { dotenv().ok(); - let db_url = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); - let depth_limit = env::var("DEPTH_LIMIT") - .map(|data| data.parse::().expect("DEPTH_LIMIT is not a number")) - .map_or(None, |data| Some(data)); - let complexity_limit = env::var("COMPLEXITY_LIMIT") - .map(|data| { - data.parse::() - .expect("COMPLEXITY_LIMIT is not a number") - }) - .map_or(None, |data| Some(data)); tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_test_writer() .init(); - let database = Database::connect(db_url).await.unwrap(); + + let database = Database::connect(&*DATABASE_URL) + .await + .expect("Fail to initialize database connection"); let orm_dataloader: DataLoader = DataLoader::new( OrmDataloader { db: database.clone(), }, tokio::spawn, ); - let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) + let mut schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) .data(database) .data(orm_dataloader); - let schema = if let Some(depth) = depth_limit { - schema.limit_depth(depth) - } else { - schema - }; - let schema = if let Some(complexity) = complexity_limit { - schema.limit_complexity(complexity) - } else { - schema - }; + if let Some(depth) = *DEPTH_LIMIT { + schema = schema.limit_depth(depth); + } + if let Some(complexity) = *COMPLEXITY_LIMIT { + schema = schema.limit_complexity(complexity); + } let schema = schema.finish(); - let app = Route::new().at("/", get(graphql_playground).post(GraphQL::new(schema))); - println!("Playground: http://localhost:8000"); - Server::new(TcpListener::bind("0.0.0.0:8000")) + let app = Route::new().at( + &*ENDPOINT, + get(graphql_playground).post(GraphQL::new(schema)), + ); + + println!("Visit GraphQL Playground at http://{}", *URL); + Server::new(TcpListener::bind(&*URL)) .run(app) .await - .unwrap(); + .expect("Fail to start web server"); } diff --git a/generator/src/_Cargo.toml b/generator/src/_Cargo.toml index 39b5a8a7..94e0738d 100644 --- a/generator/src/_Cargo.toml +++ b/generator/src/_Cargo.toml @@ -13,6 +13,7 @@ sea-orm = { version = "^0.9", features = ["", "runtime-a tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.34" } tracing-subscriber = { version = "0.3.11" } +lazy_static = { version = "1.4.0" } [dependencies.seaography] version = "" # seaography version diff --git a/generator/src/writer.rs b/generator/src/writer.rs index c486a821..e517a173 100644 --- a/generator/src/writer.rs +++ b/generator/src/writer.rs @@ -105,70 +105,69 @@ pub fn generate_main(crate_name: &str) -> TokenStream { }; use async_graphql_poem::GraphQL; use dotenv::dotenv; + use lazy_static::lazy_static; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; + use #crate_name_token::*; use std::env; - use #crate_name_token::*; + lazy_static! { + static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); + static ref DATABASE_URL: String = + env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); + static ref DEPTH_LIMIT: Option = env::var("DEPTH_LIMIT").map_or(None, |data| Some( + data.parse().expect("DEPTH_LIMIT is not a number") + )); + static ref COMPLEXITY_LIMIT: Option = env::var("COMPLEXITY_LIMIT") + .map_or(None, |data| { + Some(data.parse().expect("COMPLEXITY_LIMIT is not a number")) + }); + } #[handler] async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new("/"))) + Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } + #[tokio::main] async fn main() { dotenv().ok(); - - let db_url = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); - - let depth_limit = env::var("DEPTH_LIMIT") - .map(|data| data.parse::().expect("DEPTH_LIMIT is not a number")) - .map_or(None, |data| Some(data)); - - let complexity_limit = env::var("COMPLEXITY_LIMIT") - .map(|data| { - data.parse::() - .expect("COMPLEXITY_LIMIT is not a number") - }) - .map_or(None, |data| Some(data)); - tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_test_writer() .init(); - let database = Database::connect(db_url).await.unwrap(); + + let database = Database::connect(&*DATABASE_URL) + .await + .expect("Fail to initialize database connection"); let orm_dataloader: DataLoader = DataLoader::new( OrmDataloader { db: database.clone(), }, tokio::spawn, ); - let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) + let mut schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) .data(database) .data(orm_dataloader); - - let schema = if let Some(depth) = depth_limit { - schema.limit_depth(depth) - } else { - schema - }; - - let schema = if let Some(complexity) = complexity_limit { - schema.limit_complexity(complexity) - } else { - schema - }; - + if let Some(depth) = *DEPTH_LIMIT { + schema = schema.limit_depth(depth); + } + if let Some(complexity) = *COMPLEXITY_LIMIT { + schema = schema.limit_complexity(complexity); + } let schema = schema.finish(); + let app = Route::new().at( + &*ENDPOINT, + get(graphql_playground).post(GraphQL::new(schema)), + ); - let app = Route::new().at("/", get(graphql_playground).post(GraphQL::new(schema))); - println!("Playground: http://localhost:8000"); - Server::new(TcpListener::bind("0.0.0.0:8000")) + println!("Visit GraphQL Playground at http://{}", *URL); + Server::new(TcpListener::bind(&*URL)) .run(app) .await - .unwrap(); + .expect("Fail to start web server"); } - } }