Added logging and restructured

This commit is contained in:
Gvidas Juknevičius 2024-03-23 22:03:16 +02:00
parent 9b242a7a06
commit ae5ebefe4b
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
9 changed files with 137 additions and 24 deletions

View File

@ -8,5 +8,5 @@ indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
trim_trailing_whitespace = true
insert_final_newline = false

3
.gitignore vendored
View File

@ -1,2 +1 @@
node_modules
dist
/target

78
Cargo.lock generated
View File

@ -282,6 +282,12 @@ dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "anyhow"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
[[package]]
name = "arc-swap"
version = "1.7.1"
@ -892,6 +898,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.32.2"
@ -907,6 +922,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "paris"
version = "1.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fecab3723493c7851f292cb060f3ee1c42f19b8d749345d0d7eaf3fd19aa62d"
[[package]]
name = "parking_lot"
version = "0.12.1"
@ -948,7 +969,10 @@ version = "0.1.0"
dependencies = [
"actix-web",
"actix-web-lab",
"anyhow",
"askama",
"log",
"simplelog",
]
[[package]]
@ -1176,6 +1200,18 @@ dependencies = [
"libc",
]
[[package]]
name = "simplelog"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0"
dependencies = [
"log",
"paris",
"termcolor",
"time",
]
[[package]]
name = "slab"
version = "0.4.9"
@ -1223,6 +1259,15 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "time"
version = "0.3.34"
@ -1231,7 +1276,9 @@ checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
@ -1414,6 +1461,37 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"

View File

@ -6,4 +6,8 @@ edition = "2021"
[dependencies]
actix-web = "4.5.1"
actix-web-lab = "0.20.2"
anyhow = "1.0.81"
askama = "0.12.1"
log = "0.4.21"
simplelog = { version = "0.12.2", features = ["paris"] }
# time = { version = "0.3.34", features = ["macros"] }

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,