bvplan/backend/src/bin/api.rs
2022-12-12 17:54:07 +01:00

56 lines
1.8 KiB
Rust

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
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
}
#[actix_web::main]
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()))
.wrap(
actix_cors::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)
.supports_credentials()
.max_age(3600),
)
.wrap(actix_web::middleware::Compress::default())
.wrap(actix_web::middleware::Logger::default())
.service(
actix_web::web::resource("/")
.route(actix_web::web::post().to(graphql_route))
.route(actix_web::web::get().to(graphql_route)),
)
});
println!("Starting server on port 80!");
server.bind("0.0.0.0:80")?.run().await
}