107 lines
3.6 KiB
Rust
107 lines
3.6 KiB
Rust
mod api;
|
|
pub mod email;
|
|
|
|
use actix_files::Files;
|
|
use actix_multipart::form::tempfile::TempFileConfig;
|
|
use actix_web::body::MessageBody;
|
|
use actix_web::dev::{ServiceRequest, ServiceResponse};
|
|
use actix_web::{web, App, Error, HttpServer};
|
|
use api::camp_form::camp_form;
|
|
use api::contact::{self, contact_form};
|
|
use api::health_form::health_form;
|
|
use api::local_trip_form::local_form;
|
|
use api::mt_form::mt_form;
|
|
use api::{
|
|
mt_church_form::mt_church_form, mt_parent_form::mt_parent_form,
|
|
mt_teacher_form::mt_teacher_form,
|
|
};
|
|
use color_eyre::eyre::Context;
|
|
use color_eyre::Result;
|
|
use sqlx::{Connection, SqliteConnection};
|
|
use tracing::level_filters::LevelFilter;
|
|
use tracing::{info, Span};
|
|
use tracing_actix_web::{RootSpanBuilder, TracingLogger};
|
|
use tracing_appender::rolling::{RollingFileAppender, Rotation};
|
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer};
|
|
|
|
pub struct DomainRootSpanBuilder;
|
|
|
|
impl RootSpanBuilder for DomainRootSpanBuilder {
|
|
fn on_request_start(request: &ServiceRequest) -> Span {
|
|
let method = request.method();
|
|
let info = request.connection_info();
|
|
let ip = info.realip_remote_addr().expect("hi");
|
|
let location = request.path();
|
|
info!(?method, ip, location);
|
|
tracing_actix_web::root_span!(request)
|
|
|
|
// let client_id: &str = todo!("Somehow extract it from the authorization header");
|
|
}
|
|
|
|
fn on_request_end<B: MessageBody>(_span: Span, _outcome: &Result<ServiceResponse<B>, Error>) {}
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
let timer =
|
|
tracing_subscriber::fmt::time::ChronoLocal::new("%Y-%m-%d_%I:%M:%S%.6f %P".to_owned());
|
|
let logfile = RollingFileAppender::builder()
|
|
.rotation(Rotation::DAILY)
|
|
.filename_prefix("api")
|
|
.filename_suffix("log")
|
|
.build("./tmp")
|
|
.expect("Shouldn't");
|
|
|
|
let filter = EnvFilter::builder()
|
|
.with_default_directive(LevelFilter::WARN.into())
|
|
.parse_lossy("tfcapi=debug");
|
|
let logfile_layer = tracing_subscriber::fmt::layer()
|
|
.with_writer(logfile)
|
|
.with_line_number(true)
|
|
.with_level(true)
|
|
.with_target(true)
|
|
.with_ansi(false)
|
|
.with_timer(timer.clone());
|
|
let stdout_layer = tracing_subscriber::fmt::layer()
|
|
.pretty()
|
|
.with_line_number(true)
|
|
.with_target(true)
|
|
.with_timer(timer)
|
|
.with_filter(filter);
|
|
let filter = EnvFilter::builder()
|
|
.with_default_directive(LevelFilter::WARN.into())
|
|
.parse_lossy("tfcapi=debug");
|
|
let subscriber = tracing_subscriber::registry()
|
|
.with(logfile_layer.with_filter(filter).and_then(stdout_layer));
|
|
let _ = tracing::subscriber::set_global_default(subscriber).wrap_err("Tracing broked");
|
|
|
|
std::fs::create_dir_all("./tmp")?;
|
|
|
|
info!("starting HTTP server at http://localhost:4242");
|
|
|
|
let conn = SqliteConnection::connect("sqlite://./data.db")
|
|
.await
|
|
.expect("Couldn't connect sqlite db");
|
|
let data = web::Data::new(conn);
|
|
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.app_data(data.clone())
|
|
.wrap(TracingLogger::<DomainRootSpanBuilder>::new())
|
|
.app_data(TempFileConfig::default().directory("./tmp"))
|
|
.service(mt_form)
|
|
.service(health_form)
|
|
.service(mt_parent_form)
|
|
.service(mt_teacher_form)
|
|
.service(mt_church_form)
|
|
.service(local_form)
|
|
.service(camp_form)
|
|
.service(contact_form)
|
|
.service(Files::new("/", "./public").index_file("index.html"))
|
|
})
|
|
.bind(("localhost", 4242))?
|
|
.workers(2)
|
|
.run()
|
|
.await
|
|
}
|