Added logging and restructured

This commit is contained in:
2024-03-23 22:03:16 +02:00
parent 9b242a7a06
commit ae5ebefe4b
9 changed files with 137 additions and 24 deletions

19
src/logger.rs Normal file
View File

@@ -0,0 +1,19 @@
use simplelog::*;
pub fn init_logger() {
// TODO: figure out what these do
let config = ConfigBuilder::new()
.build();
CombinedLogger::init(
vec![
TermLogger::new(LevelFilter::Debug, config, TerminalMode::Mixed, ColorChoice::Auto),
// TODO: Set up loggin to file
// WriteLogger::new(LevelFilter::Info, Config::default(), File::create("my_rust_binary.log").unwrap()),
]
).unwrap();
}

View File

@@ -1,16 +1,12 @@
use actix_web::{web, App, HttpServer};
mod public;
mod logger;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
logger::init_logger();
HttpServer::new(|| {
App::new()
.route("/", web::get().to(public::index))
})
.bind("0.0.0.0:8080")?
.run()
.await
if let Err(e) = public::start_actix().await {
log::error!("Actix had an error: {e}");
}
Ok(())
}

View File

@@ -1,15 +1,19 @@
use actix_web_lab::respond::Html;
use actix_web::{Result, Responder};
use askama::Template;
mod routes;
mod templates;
pub async fn index() -> Result<impl Responder> {
let html = templates::IndexTemplate {
placeholder: "hewwo world"
}.render().expect("Failed to render index.html");
use actix_web::{web, App, HttpServer};
Ok(Html(html))
pub(crate) async fn start_actix() -> anyhow::Result<()> {
log::info!("Serving an http server at https://0.0.0.0:8080");
HttpServer::new(|| {
App::new()
.route("/", web::get().to(routes::index))
})
.bind("0.0.0.0:8080")?
.run()
.await?;
Ok(())
}

13
src/public/routes/mod.rs Normal file
View File

@@ -0,0 +1,13 @@
use actix_web_lab::respond::Html;
use actix_web::{Result, Responder};
use askama::Template;
use crate::public::templates::IndexTemplate;
pub async fn index() -> Result<impl Responder> {
let html = IndexTemplate {
placeholder: "hewwo world"
}.render().expect("Failed to render index.html");
Ok(Html(html))
}

View File

@@ -1,6 +1,6 @@
use askama::Template;
#[derive(Template)]
#[derive(Debug, Clone, Copy, Template)]
#[template(path = "index.html")]
pub struct IndexTemplate<'a> {
pub placeholder: &'a str,