2019-06-08 19:47:39 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate actix_web;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
|
|
|
|
use actix_web::web;
|
|
|
|
use actix_web::{App, HttpResponse, HttpServer};
|
|
|
|
|
|
|
|
use handlebars::Handlebars;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
// Macro documentation can be found in the actix_web_codegen crate
|
|
|
|
#[get("/")]
|
2019-06-22 09:07:39 +02:00
|
|
|
fn index(hb: web::Data<Handlebars>) -> HttpResponse {
|
2019-06-08 19:47:39 +02:00
|
|
|
let data = json!({
|
|
|
|
"name": "Handlebars"
|
|
|
|
});
|
|
|
|
let body = hb.render("index", &data).unwrap();
|
|
|
|
|
|
|
|
HttpResponse::Ok().body(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/{user}/{data}")]
|
|
|
|
fn user(
|
2019-06-22 09:07:39 +02:00
|
|
|
hb: web::Data<Handlebars>,
|
2019-06-08 19:47:39 +02:00
|
|
|
info: web::Path<(String, String)>,
|
|
|
|
) -> HttpResponse {
|
|
|
|
let data = json!({
|
|
|
|
"user": info.0,
|
|
|
|
"data": info.1
|
|
|
|
});
|
|
|
|
let body = hb.render("user", &data).unwrap();
|
|
|
|
|
|
|
|
HttpResponse::Ok().body(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
// Handlebars uses a repository for the compiled templates. This object must be
|
|
|
|
// shared between the application threads, and is therefore passed to the
|
|
|
|
// Application Builder as an atomic reference-counted pointer.
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
handlebars
|
|
|
|
.register_templates_directory(".html", "./static/templates")
|
|
|
|
.unwrap();
|
2019-06-22 09:07:39 +02:00
|
|
|
let handlebars_ref = web::Data::new(handlebars);
|
2019-06-08 19:47:39 +02:00
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.register_data(handlebars_ref.clone())
|
|
|
|
.service(index)
|
|
|
|
.service(user)
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
|
|
|
}
|