From c7d885e4b8189e4539c1f259a44ec37c6cd027b2 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Mon, 2 May 2022 22:32:52 -0400 Subject: feat: reset button read config start db pool and launch axum server --- dichroism/src/main.rs | 99 ++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 61 deletions(-) (limited to 'dichroism/src/main.rs') diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs index 6c76f0a..86de76f 100644 --- a/dichroism/src/main.rs +++ b/dichroism/src/main.rs @@ -1,66 +1,43 @@ -#[macro_use] -extern crate serde; -#[macro_use] -extern crate diesel; - -use actix_cors::Cors; -use actix_web::{middleware::Logger, App, HttpServer}; -use config::CONFIG_INSTANCE as CONFIG; -use diesel::prelude::SqliteConnection; -use diesel::r2d2::ConnectionManager; -use diesel::r2d2::Pool; -use listenfd::ListenFd; -use result::Result; +use anyhow::Context; +use axum::{http::StatusCode, routing::get_service, Router}; +use r2d2::Pool; +use r2d2_sqlite::SqliteConnectionManager; +use rusqlite::params; +use std::io::Error; +use tower_http::services::ServeDir; mod config; -mod constants; -mod dtos; -mod error; -mod handlers; -mod image_service; -mod models; -mod repo; mod result; -mod schema; -mod types; - -#[actix_web::main] -async fn main() -> Result<()> { - // Init logging - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); - - // Init DB connection pool - let manager = ConnectionManager::::new(&CONFIG.db_url); - let pool = Pool::builder().build(manager)?; - - // Init application server - let mut server = HttpServer::new(move || { - // Init CORS policy - let cors = Cors::permissive(); - - App::new() - .data(pool.clone()) - .wrap(cors) - .wrap(Logger::default()) - .service(handlers::hello) - .service(handlers::get_products) - .service(handlers::patch_product) - .service(handlers::post_product) - .service(handlers::post_photo) - }); - - // If using listenfd, bind to it instead of the configured address to allow for cargo watch - // auto-reloading - let mut listenfd = ListenFd::from_env(); - server = if let Some(l) = listenfd - .take_tcp_listener(0) - .expect("Unable to grab TCP listener!") - { - server.listen(l)? - } else { - server.bind(CONFIG.bind_addr)? - }; - Ok(server.run().await?) +#[tokio::main] +async fn main() -> result::Result<()> { + // read config from environment + let config = config::Config::from_env()?; + + // start db connection pool + let manager = SqliteConnectionManager::file(&config.db_path); + let pool = Pool::new(manager).with_context(|| "Failed to create database connection pool.")?; + + // migrate/init db if empty + pool.get()? + .execute(include_str!("./migrations/photo_sets.sql"), params![])?; + pool.get()? + .execute(include_str!("./migrations/products.sql"), params![])?; + + let app = Router::new().nest( + "/static", + get_service(ServeDir::new("/zroot/gl/images")).handle_error(|error: Error| async move { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Unhandled internal error: {}", error), + ) + }), + ); + + axum::Server::bind(&config.bind_addr) + .serve(app.into_make_service()) + .await + .with_context(|| "Server error!")?; + + Ok(()) } -- cgit v1.2.3