From b3a3484e99d7ad311b50219ded6663061852bf9d Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Wed, 26 Jun 2024 10:37:37 +0200 Subject: [PATCH] Replaced main with the new AppBuilder --- Cargo.lock | 31 ++++++++++++++++++++++++++++--- Cargo.toml | 2 +- src/config.rs | 6 ------ src/main.rs | 45 ++++++++------------------------------------- 4 files changed, 37 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c920a57..77eb9d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,7 +138,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "derive" version = "1.0.0" -source = "git+https://github.com/emberal/rust-lib.git?tag=1.0.0#d5974dda2071355115090e26175b657ee82b719b" +source = "git+https://github.com/emberal/rust-lib.git?tag=1.1.1#752d1a9d102a6ed7026909a46d4e0b2cb1e0cb3d" dependencies = [ "quote", "syn", @@ -174,6 +174,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + [[package]] name = "futures-task" version = "0.3.30" @@ -304,13 +310,19 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "lib" -version = "1.0.0" -source = "git+https://github.com/emberal/rust-lib.git?tag=1.0.0#d5974dda2071355115090e26175b657ee82b719b" +version = "1.1.1" +source = "git+https://github.com/emberal/rust-lib.git?tag=1.1.1#752d1a9d102a6ed7026909a46d4e0b2cb1e0cb3d" dependencies = [ "axum", "derive", "nom", "serde", + "tokio", + "tokio-util", + "tower", + "tower-http", + "tracing", + "tracing-subscriber", ] [[package]] @@ -647,6 +659,19 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tower" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index 017de3a..0b533da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ tower = "0.4.13" tracing = "0.1.40" tracing-subscriber = "0.3.18" -lib = { git = "https://github.com/emberal/rust-lib.git", tag = "1.0.0", features = ["axum", "vec", "nom", "serde", "derive"] } +lib = { git = "https://github.com/emberal/rust-lib.git", tag = "1.1.1", features = ["axum", "vec", "nom", "serde", "derive", "tokio"] } diff --git a/src/config.rs b/src/config.rs index b6620fd..c1a09b5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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" diff --git a/src/main.rs b/src/main.rs index 6afe346..b4030c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::::into_make_service(app)).await.unwrap(); }