From 6f329557aff3c7810699bf1bafa0a073798e1b9f Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 10 Mar 2021 20:51:26 +0100 Subject: feat: add logging with slog --- src/main.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 51c2f6d..0b3d67f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ mod config; -use actix_web::{web, App, HttpResponse, HttpServer}; +use actix_web::{middleware, web, App, HttpServer}; use dotenv::dotenv; +use slog::info; use tokio_postgres::NoTls; -use crate::config::Config; +use crate::config::{Config, AppState}; + +async fn index(state: web::Data) -> &'static str { + info!(state.log, "GET `/` page"); + + "Hello from Rust!" +} #[actix_rt::main] async fn main() -> std::io::Result<()> { @@ -12,12 +19,23 @@ async fn main() -> std::io::Result<()> { let config = Config::from_env().unwrap(); let pool = config.pg.create_pool(NoTls).unwrap(); + let log = Config::logging(); + + info!( + log, + "Starting server at http://{}:{}", + config.server.host, + config.server.port + ); HttpServer::new(move || { - App::new().data(pool.clone()).service( - web::resource("/") - .to(|| HttpResponse::Ok().body("Hello from Rust!")), - ) + App::new() + .data(AppState { + pool: pool.clone(), + log: log.clone(), + }) + .wrap(middleware::Logger::default()) + .route("/", web::get().to(index)) }) .bind(format!("{}:{}", config.server.host, config.server.port))? .run() -- cgit v1.2.3-18-g5258