This commit is contained in:
Dominic Grimm 2022-12-20 19:00:12 +01:00
parent f13c9c905f
commit f614e606f4
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
15 changed files with 269 additions and 214 deletions

View file

@ -1,24 +1,18 @@
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
use actix_cors::Cors;
use actix_web::{
http::header,
middleware,
web::{self, Data},
App, Error, HttpResponse, HttpServer,
};
use juniper_actix::graphql_handler;
async fn graphql_route(
req: actix_web::HttpRequest,
payload: actix_web::web::Payload,
schema: actix_web::web::Data<backend::graphql::Schema>,
) -> Result<actix_web::HttpResponse, actix_web::Error> {
juniper_actix::graphql_handler(
&schema,
&backend::graphql::Context {
pool: backend::db::POOL.clone(),
},
req,
payload,
)
.await
schema: web::Data<backend::graphql::Schema>,
) -> Result<HttpResponse, Error> {
graphql_handler(&schema, &backend::graphql::Context, req, payload).await
}
#[actix_web::main]
@ -26,30 +20,25 @@ async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "info");
env_logger::init();
let server = actix_web::HttpServer::new(move || {
actix_web::App::new()
.app_data(actix_web::web::Data::new(backend::graphql::schema()))
let server = HttpServer::new(move || {
App::new()
.app_data(Data::new(backend::graphql::schema()))
.wrap(
actix_cors::Cors::default()
Cors::default()
.allow_any_origin()
.allowed_methods(vec!["POST", "GET"])
.allowed_headers(vec![
actix_web::http::header::AUTHORIZATION,
actix_web::http::header::ACCEPT,
])
.allowed_header(actix_web::http::header::CONTENT_TYPE)
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.supports_credentials()
.max_age(3600),
)
.wrap(actix_web::middleware::Compress::default())
.wrap(actix_web::middleware::Logger::default())
.wrap(middleware::Compress::default())
.wrap(middleware::Logger::default())
.service(
actix_web::web::resource("/")
.route(actix_web::web::post().to(graphql_route))
.route(actix_web::web::get().to(graphql_route)),
web::resource("/graphql")
.route(web::post().to(graphql_route))
.route(web::get().to(graphql_route)),
)
});
println!("Starting server on port 80!");
server.bind("0.0.0.0:80")?.run().await
server.bind("0.0.0.0:80").unwrap().run().await
}