Replaced main with the new AppBuilder

This commit is contained in:
Martin Berg Alstad
2024-06-26 10:37:37 +02:00
parent 1b94e63915
commit b3a3484e99
4 changed files with 37 additions and 47 deletions

View File

@ -1,10 +1,4 @@
#![allow(dead_code)]
use std::net::Ipv4Addr;
pub const IP: Ipv4Addr = Ipv4Addr::UNSPECIFIED;
pub const PORT: u16 = 8000;
pub const SOCKET: (Ipv4Addr, u16) = (IP, PORT);
pub const IS_DEV: bool = cfg!(debug_assertions);
pub const RESOURCE_DIR: &str = if IS_DEV {
"./src/resources/static"

View File

@ -1,17 +1,8 @@
use std::net::SocketAddr;
use axum::extract::Request;
use axum::ServiceExt;
use lib::{create_app, join_routes};
use tokio::net::TcpListener;
use tower::Layer;
use tower_http::cors::{Any, CorsLayer};
use tower_http::normalize_path::NormalizePathLayer;
use tower_http::trace;
use tower_http::trace::TraceLayer;
use tracing::Level;
use lib::axum::app::AppBuilder;
use tower_http::cors::CorsLayer;
use crate::routing::routes::*;
use crate::routing::routes::index::not_found;
mod expressions;
mod parsing;
@ -21,31 +12,11 @@ mod utils;
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(config::SOCKET);
let listener = TcpListener::bind(&addr)
AppBuilder::new()
.routes(&[index::router(), simplify::router(), table::router()])
.fallback(not_found)
.cors(CorsLayer::permissive())
.serve()
.await
.unwrap();
tracing_subscriber::fmt()
.with_target(false)
.compact()
.init();
let routes = join_routes![
simplify::router(),
index::router(),
table::router()
].fallback(index::not_found);
let app = NormalizePathLayer::trim_trailing_slash()
.layer(create_app!(routes,
CorsLayer::new().allow_origin(Any),
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
));
tracing::info!("Starting server on: {addr}");
axum::serve(listener, ServiceExt::<Request>::into_make_service(app)).await.unwrap();
}