Swap to actix-web
This commit is contained in:
parent
7d6126dd87
commit
e5410a7e9e
7 changed files with 92 additions and 22 deletions
26
.vscode/launch.json
vendored
Normal file
26
.vscode/launch.json
vendored
Normal 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}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -6,5 +6,7 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = { version = "=0.5.0-rc.3", features = ["secrets", "json"] }
|
actix-web = { version = "4" }
|
||||||
rocket_dyn_templates = { version = "=0.1.0-rc.3", features = ["tera"] }
|
actix-files = { version ="0.6.2" }
|
||||||
|
tera = "1.19.0"
|
||||||
|
env_logger = "0.10.0"
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<title>Basic Web Page</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
Hello World!
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
BIN
resources/petersilie_banner.png
Normal file
BIN
resources/petersilie_banner.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 680 KiB |
44
src/main.rs
44
src/main.rs
|
@ -1,14 +1,42 @@
|
||||||
#[macro_use]
|
use std::path::PathBuf;
|
||||||
extern crate rocket;
|
|
||||||
|
|
||||||
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("/")]
|
#[get("/")]
|
||||||
async fn index() -> Option<NamedFile> {
|
async fn index(data: Data<AppData>, req: HttpRequest) -> impl Responder {
|
||||||
NamedFile::open("resources/index.html").await.ok()
|
let mut ctx = Context::new();
|
||||||
|
|
||||||
|
let rendered = data.tmpl.render("templates/index.html", &ctx).unwrap();
|
||||||
|
HttpResponse::Ok().body(rendered)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[launch]
|
#[actix_web::main]
|
||||||
fn rocket() -> _ {
|
async fn main() -> std::io::Result<()> {
|
||||||
rocket::build().mount("/", routes![index])
|
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
4
templates/index.css
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#wrapper,
|
||||||
|
img {
|
||||||
|
width: 100%;
|
||||||
|
}
|
22
templates/index.html
Normal file
22
templates/index.html
Normal 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>
|
Loading…
Reference in a new issue