Skip to content

Commit

Permalink
Refactoring the main function (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Oct 11, 2022
1 parent e7fe487 commit 6a80a35
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 117 deletions.
1 change: 1 addition & 0 deletions examples/mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 35 additions & 27 deletions examples/mysql/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = env::var("DEPTH_LIMIT").map_or(None, |data| Some(
data.parse().expect("DEPTH_LIMIT is not a number")
));
static ref COMPLEXITY_LIMIT: Option<usize> = 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::<usize>().expect("DEPTH_LIMIT is not a number"))
.map_or(None, |data| Some(data));
let complexity_limit = env::var("COMPLEXITY_LIMIT")
.map(|data| {
data.parse::<usize>()
.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<OrmDataloader> = 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");
}
1 change: 1 addition & 0 deletions examples/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 35 additions & 27 deletions examples/postgres/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = env::var("DEPTH_LIMIT").map_or(None, |data| Some(
data.parse().expect("DEPTH_LIMIT is not a number")
));
static ref COMPLEXITY_LIMIT: Option<usize> = 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::<usize>().expect("DEPTH_LIMIT is not a number"))
.map_or(None, |data| Some(data));
let complexity_limit = env::var("COMPLEXITY_LIMIT")
.map(|data| {
data.parse::<usize>()
.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<OrmDataloader> = 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");
}
1 change: 1 addition & 0 deletions examples/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 35 additions & 27 deletions examples/sqlite/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = env::var("DEPTH_LIMIT").map_or(None, |data| Some(
data.parse().expect("DEPTH_LIMIT is not a number")
));
static ref COMPLEXITY_LIMIT: Option<usize> = 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::<usize>().expect("DEPTH_LIMIT is not a number"))
.map_or(None, |data| Some(data));
let complexity_limit = env::var("COMPLEXITY_LIMIT")
.map(|data| {
data.parse::<usize>()
.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<OrmDataloader> = 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");
}
1 change: 1 addition & 0 deletions generator/src/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sea-orm = { version = "^0.9", features = ["<seaography-sql-library>", "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>" # seaography version
Expand Down
Loading

0 comments on commit 6a80a35

Please sign in to comment.