Swap to actix-web

This commit is contained in:
hodasemi 2023-08-29 16:08:43 +02:00
parent 7d6126dd87
commit e5410a7e9e
7 changed files with 92 additions and 22 deletions

26
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'petersilie_server'",
"cargo": {
"args": [
"build",
"--bin=petersilie_server",
"--package=petersilie_server"
],
"filter": {
"name": "petersilie_server",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View file

@ -6,5 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = { version = "=0.5.0-rc.3", features = ["secrets", "json"] }
rocket_dyn_templates = { version = "=0.1.0-rc.3", features = ["tera"] }
actix-web = { version = "4" }
actix-files = { version ="0.6.2" }
tera = "1.19.0"
env_logger = "0.10.0"

View file

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic Web Page</title>
</head>
<body>
Hello World!
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 KiB

View file

@ -1,14 +1,42 @@
#[macro_use]
extern crate rocket;
use std::path::PathBuf;
use rocket::fs::NamedFile;
use actix_files::NamedFile;
use actix_web::middleware::Logger;
use actix_web::{get, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder, Result};
use env_logger::Env;
use tera::{Context, Tera};
struct AppData {
tmpl: Tera,
}
#[get("/")]
async fn index() -> Option<NamedFile> {
NamedFile::open("resources/index.html").await.ok()
async fn index(data: Data<AppData>, req: HttpRequest) -> impl Responder {
let mut ctx = Context::new();
let rendered = data.tmpl.render("templates/index.html", &ctx).unwrap();
HttpResponse::Ok().body(rendered)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(
Env::default()
.default_filter_or("info")
.default_filter_or("debug"),
);
HttpServer::new(|| {
App::new()
.service(index)
.service(actix_files::Files::new("/resources", ".").show_files_listing())
.service(actix_files::Files::new("/templates", ".").show_files_listing())
.app_data(Data::new(AppData {
tmpl: Tera::new("/templates/**/*").unwrap(),
}))
.wrap(Logger::default())
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}

4
templates/index.css Normal file
View file

@ -0,0 +1,4 @@
#wrapper,
img {
width: 100%;
}

22
templates/index.html Normal file
View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Petersilie Shop</title>
<link href="templates/index.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<img src="resources/petersilie_banner.png">
<h1 class="title" style='font-size: 25px; color:#ffff00;'>Header info 1</h1>
<h2 class="title" style='font-size: 15px; color:#00ff00;'>Header info 2</h2>
<h3 class="title" style='font-size: 10px; color:#0000ff;'>Header info 3</h3>
</div>
<nav>
<! --this part works fine so code omitted -->
</nav>
</body>
</html>