use actix_web::{web::{self, Data}, App, HttpResponse, HttpServer, Responder, middleware::Logger};
use tera::{Tera, Context};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Warrior {
description: String,
name: String,
}
async fn index(tera: web::Data<Tera>) -> impl Responder {
let mut data = Context::new();
let mut warriors = Vec::new();
for _ in 1..5 {
warriors.push(Warrior{description:String::from("test"), name:String::from("tost")});
}
data.insert("warriors", &warriors);
let rendered = tera.render("index.html", &data).unwrap();
HttpResponse::Ok().body(rendered)
}
async fn new(tera: web::Data<Tera>) -> impl Responder {
let mut data = Context::new();
let rendered = tera.render("new.html", &data).unwrap();
HttpResponse::Ok().body(rendered)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
HttpServer::new(|| {
let tera = Tera::new("templates/**/*").unwrap();
App::new()
.wrap(Logger::default())
.wrap(Logger::new("%a %{User-Agent}i"))
.app_data(Data::new(tera))
.route("/", web::get().to(index))
.route("/new", web::get().to(new))
})
.bind("127.0.0.1:8000")?
.run()
.await
}