1
0
mirror of https://github.com/actix/examples synced 2025-06-29 18:24:57 +02:00

Update to master (#90)

This commit is contained in:
Juan Aguilar
2019-03-26 04:29:00 +01:00
committed by Douman
parent 1779f963d9
commit 53fc2221ef
44 changed files with 139 additions and 143 deletions

View File

@ -1,21 +1 @@
use actix_web::{error::ErrorInternalServerError, web::Query, HttpResponse, Result};
use yarte::Template;
use std::collections::HashMap;
#[derive(Template)]
#[template(path = "index.hbs")]
struct IndexTemplate {
query: Query<HashMap<String, String>>,
}
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
IndexTemplate { query }
.call()
.map(|s| {
HttpResponse::Ok()
.content_type(IndexTemplate::mime())
.body(s)
})
.map_err(|_| ErrorInternalServerError("Template parsing error"))
}

View File

@ -1,7 +1,31 @@
use actix_web::{middleware, web, App, HttpServer};
#[macro_use]
extern crate actix_web;
#[path = "lib.rs"]
mod template;
use std::collections::HashMap;
use actix_web::{
error::ErrorInternalServerError, middleware, web::Query, App, HttpResponse,
HttpServer, Result,
};
use yarte::Template;
#[derive(Template)]
#[template(path = "index.hbs")]
struct IndexTemplate {
query: Query<HashMap<String, String>>,
}
#[get("/")]
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
IndexTemplate { query }
.call()
.map(|s| {
HttpResponse::Ok()
.content_type(IndexTemplate::mime())
.body(s)
})
.map_err(|_| ErrorInternalServerError("Template parsing error"))
}
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
@ -11,8 +35,8 @@ fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
// enable logger
.middleware(middleware::Logger::default())
.service(web::resource("/").to(template::index))
.wrap(middleware::Logger::default())
.service(index)
})
.bind("127.0.0.1:8080")?
.run()