Changed logger, and crate name
This commit is contained in:
29
src/web/mod.rs
Normal file
29
src/web/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
mod routes;
|
||||
mod templates;
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_files as actix_fs;
|
||||
|
||||
use crate::{config::definition::Config, database::Database};
|
||||
|
||||
pub(crate) async fn start_actix(config: &Config, database: Database) -> anyhow::Result<()> {
|
||||
let bindip = format!("{}:{}", config.webserver.host, config.webserver.port);
|
||||
|
||||
log::info!("Serving an http server at http://{bindip}");
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(actix_web::web::Data::new(Mutex::new(database.clone())))
|
||||
.route("/", web::get().to(routes::index)) // index.html
|
||||
.service(actix_fs::Files::new("/static", "./static").index_file("index.html")) // static directory
|
||||
.service(web::redirect("/favicon.ico", "/static/favicon.ico")) //? special redirect for favicon
|
||||
})
|
||||
|
||||
.bind(bindip)?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
17
src/web/routes/mod.rs
Normal file
17
src/web/routes/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use actix_web_lab::respond::Html;
|
||||
use actix_web::{web::Data, Responder, Result};
|
||||
use askama::Template;
|
||||
|
||||
use crate::{database::Database, web::templates::IndexTemplate};
|
||||
|
||||
|
||||
// NOTE: Not usefull to have database here but just so u know how
|
||||
pub async fn index(_: Data<Mutex<Database>>) -> Result<impl Responder> {
|
||||
let html = IndexTemplate {
|
||||
placeholder: "hewwo world"
|
||||
}.render().expect("Failed to render index.html");
|
||||
|
||||
Ok(Html(html))
|
||||
}
|
||||
7
src/web/templates.rs
Normal file
7
src/web/templates.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use askama::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