add hello world

This commit is contained in:
Mylloon 2023-02-08 20:49:05 +01:00
parent 0840740caf
commit a8a39cf5b9
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 1361 additions and 2 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
/config

1293
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,3 +9,7 @@ repository = "https://git.mylloon.fr/Anri/ewp"
publish = false
[dependencies]
actix-web = "4"
askama = "0.11.1"
toml = "0.5.10"
serde = { version = "1.0.152", features = ["derive"] }

24
src/config.rs Normal file
View file

@ -0,0 +1,24 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Config {
pub scheme: String,
}
pub fn get_config(file_path: &str) -> Config {
match std::fs::read_to_string(file_path) {
Ok(file) => match toml::from_str(&file) {
Ok(stored_config) => stored_config,
Err(file_error) => {
panic!("Error in config file: {file_error}");
}
},
Err(_) =>
// No config file
{
Config {
scheme: "http".to_string(),
}
}
}
}

View file

@ -1,3 +1,18 @@
fn main() {
println!("Hello, world!");
use actix_web::{App, HttpServer};
mod config;
mod template;
#[path = "routes/index.rs"]
mod index;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let config = config::get_config("config/config.toml");
let addr = ("127.0.0.1", 8080);
println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1);
HttpServer::new(|| App::new().service(index::page))
.bind(addr)?
.run()
.await
}

8
src/routes/index.rs Normal file
View file

@ -0,0 +1,8 @@
use crate::template::get_index;
use actix_web::{get, HttpResponse, Responder};
#[get("/")]
pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_index())
}

13
src/template.rs Normal file
View file

@ -0,0 +1,13 @@
use askama::Template;
#[derive(Template)]
#[template(path = "../templates/index.html")]
struct HelloTemplate<'a> {
name: &'a str,
}
pub fn get_index() -> std::string::String {
let hello = HelloTemplate { name: "world" }; // instantiate your struct
hello.render().unwrap()
}

1
templates/index.html Normal file
View file

@ -0,0 +1 @@
Hello, {{ name }}!