Added logging and restructured
This commit is contained in:
19
src/logger.rs
Normal file
19
src/logger.rs
Normal 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();
|
||||
}
|
||||
16
src/main.rs
16
src/main.rs
@@ -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(())
|
||||
}
|
||||
@@ -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
13
src/public/routes/mod.rs
Normal 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))
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user