-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add actix framework generator option (#74)
* Add actix framework generator option
- Loading branch information
Showing
11 changed files
with
341 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
use crate::util::add_line_break; | ||
|
||
/// | ||
/// Used to generate project/src/main.rs file content | ||
/// | ||
pub fn generate_main(crate_name: &str) -> TokenStream { | ||
let crate_name_token: TokenStream = crate_name.replace('-', "_").parse().unwrap(); | ||
|
||
quote! { | ||
use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer, Result}; | ||
use async_graphql::{ | ||
dataloader::DataLoader, http::{playground_source, GraphQLPlaygroundConfig}, EmptyMutation, EmptySubscription, Schema, | ||
}; | ||
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse}; | ||
use dotenv::dotenv; | ||
use lazy_static::lazy_static; | ||
use sea_orm::Database; | ||
use #crate_name_token::*; | ||
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")) | ||
}); | ||
} | ||
|
||
type AppSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>; | ||
|
||
async fn index(schema: web::Data<AppSchema>, req: GraphQLRequest) -> GraphQLResponse { | ||
schema.execute(req.into_inner()).await.into() | ||
} | ||
|
||
async fn graphql_playground() -> Result<HttpResponse> { | ||
Ok(HttpResponse::Ok() | ||
.content_type("text/html; charset=utf-8") | ||
.body( | ||
playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000")) | ||
)) | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
dotenv().ok(); | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
.with_test_writer() | ||
.init(); | ||
|
||
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 mut schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) | ||
.data(database) | ||
.data(orm_dataloader); | ||
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(); | ||
|
||
println!("Visit GraphQL Playground at http://{}", *URL); | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.app_data(Data::new(schema.clone())) | ||
.service(web::resource("/").guard(guard::Post()).to(index)) | ||
.service(web::resource("/").guard(guard::Get()).to(graphql_playground)) | ||
}) | ||
.bind("127.0.0.1:8000")? | ||
.run() | ||
.await | ||
} | ||
} | ||
} | ||
|
||
pub fn write_main<P: AsRef<std::path::Path>>(path: &P, crate_name: &str) -> std::io::Result<()> { | ||
let tokens = generate_main(crate_name); | ||
|
||
let file_name = path.as_ref().join("main.rs"); | ||
|
||
std::fs::write(file_name, add_line_break(tokens))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
edition = '2021' | ||
name = '<seaography-package-name>' | ||
version = '0.1.0' | ||
|
||
[dependencies] | ||
actix-web = { version = "4.0.1", default-features = false, features = ["macros"] } | ||
async-graphql = { version = "4.0.14", features = ["decimal", "chrono", "dataloader"] } | ||
async-graphql-actix-web = { version = "4.0.14" } | ||
async-trait = { version = "0.1.53" } | ||
dotenv = "0.15.0" | ||
sea-orm = { version = "^0.9", features = ["<seaography-sql-library>", "runtime-async-std-native-tls"] } | ||
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 | ||
features = ["with-decimal", "with-chrono"] | ||
|
||
[dev-dependencies] | ||
serde_json = { version = '1.0.82' } | ||
|
||
[workspace] | ||
members = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod actix; | ||
pub mod poem; |
Oops, something went wrong.