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:
@ -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"))
|
||||
}
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user