bvplan/bvplan/src/bin/web.rs
2023-02-27 11:09:16 +01:00

49 lines
1.3 KiB
Rust

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
use actix_web::{get, middleware, web::Data, App, HttpResponse, HttpServer};
use r2d2_redis::redis;
use std::ops::DerefMut;
use bvplan::*;
#[get("/")]
async fn index(redis_pool: Data<cache::RedisPool>) -> HttpResponse {
let redis_conn = &mut match redis_pool.get() {
Ok(x) => x,
Err(_) => return HttpResponse::InternalServerError().finish(),
};
if let Some(html) = match redis::cmd("GET")
.arg(cache::keys::SUBSTITUTIONS_HTML)
.query::<Option<String>>(redis_conn.deref_mut())
{
Ok(x) => x,
Err(_) => return HttpResponse::InternalServerError().finish(),
} {
HttpResponse::Accepted()
.content_type("text/html; charset=utf-8")
.body(html)
} else {
HttpResponse::InternalServerError().finish()
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
let server = HttpServer::new(move || {
App::new()
.app_data(Data::new(cache::pool().unwrap()))
.wrap(middleware::Compress::default())
.wrap(middleware::Logger::default())
.service(index)
});
server.bind("0.0.0.0:80").unwrap().run().await
}