mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Update to master (#90)
This commit is contained in:
@ -11,7 +11,7 @@ workspace = ".."
|
||||
env_logger = "0.6"
|
||||
|
||||
yarte = "0.1"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
[build-dependencies]
|
||||
yarte = "0.1"
|
||||
|
@ -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()
|
||||
|
@ -1,14 +1,7 @@
|
||||
# root dir of templates
|
||||
[main]
|
||||
dir = "templates"
|
||||
debug = "code"
|
||||
|
||||
# Alias for partials. In call, change the start of partial path with one of this, if exist.
|
||||
[partials]
|
||||
alias = "./deep/more/deep"
|
||||
|
||||
[debug]
|
||||
# prettyprint themes, put anything for options
|
||||
theme = "zenburn"
|
||||
number_line = true
|
||||
grid = true
|
||||
|
Reference in New Issue
Block a user