Add Docker build

This commit is contained in:
2022-03-27 20:08:12 +02:00
parent c746325495
commit a99af2f845
16 changed files with 198 additions and 29 deletions

View File

@@ -1,7 +1,9 @@
use actix_files as fs;
use actix_web::{dev::Server, http, middleware, web, App, HttpServer};
use configuration::Settings;
use core::time::Duration;
use std::env;
use std::net::TcpListener;
use tracing::info;
use tracing_actix_web::TracingLogger;
#[macro_use]
@@ -12,30 +14,55 @@ pub mod contextswitch;
pub mod observability;
pub mod routes;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let cs_front_base_url =
env::var("CS_FRONT_BASE_URL").unwrap_or_else(|_| "http://localhost:8080".to_string());
pub fn run(listener: TcpListener, settings: &Settings) -> Result<Server, std::io::Error> {
let api_path = settings.application.api_path.clone();
let front_base_url = settings.application.front_base_url.clone();
let static_path = settings.application.static_path.clone();
let static_dir = settings
.application
.static_dir
.clone()
.unwrap_or_else(|| ".".to_string());
let server = HttpServer::new(move || {
App::new()
.wrap(TracingLogger::default())
.wrap(middleware::Compress::default())
info!(
"Mounting API on {}",
if api_path.is_empty() { "/" } else { &api_path }
);
let api_scope = web::scope(&api_path)
.wrap(
middleware::DefaultHeaders::new()
.add(("Access-Control-Allow-Origin", cs_front_base_url.as_bytes()))
.add(("Access-Control-Allow-Origin", front_base_url.as_bytes()))
.add((
"Access-Control-Allow-Methods",
"POST, GET, OPTIONS".as_bytes(),
))
.add(("Access-Control-Allow-Headers", "content-type".as_bytes())),
)
.route("/ping", web::get().to(routes::ping))
.route("/tasks", web::get().to(routes::list_tasks))
.route("/tasks", web::post().to(routes::add_task))
.route("/tasks/{task_id}", web::put().to(routes::update_task))
.route(
"/tasks",
web::method(http::Method::OPTIONS).to(routes::option_task),
)
);
let mut app = App::new()
.wrap(TracingLogger::default())
.wrap(middleware::Compress::default())
.route("/ping", web::get().to(routes::ping))
.service(api_scope);
if let Some(path) = &static_path {
info!(
"Mounting static files on {}",
if path.is_empty() { "/" } else { &path }
);
let static_scope = fs::Files::new(&path, &static_dir)
.use_last_modified(true)
.index_file("index.html");
app = app.service(static_scope);
}
app
})
.keep_alive(http::KeepAlive::Timeout(Duration::from_secs(60)))
.shutdown_timeout(60)