mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Update to master (#90)
This commit is contained in:
parent
1779f963d9
commit
53fc2221ef
@ -40,7 +40,6 @@ script:
|
||||
cd actix_todo && cargo check && cd ..
|
||||
cd basics && cargo check && cd ..
|
||||
cd cookie-auth && cargo check && cd ..
|
||||
cd cookie-auth-full && cargo check && cd ..
|
||||
cd cookie-session && cargo check && cd ..
|
||||
cd diesel && cargo check && cd ..
|
||||
cd error_handling && cargo check && cd ..
|
||||
@ -55,7 +54,6 @@ script:
|
||||
cd protobuf && cargo check && cd ..
|
||||
cd r2d2 && cargo check && cd ..
|
||||
cd redis-session && cargo check && cd ..
|
||||
cd simple-auth-sarver && cargo check && cd ..
|
||||
cd state && cargo check && cd ..
|
||||
cd static_index && cargo check && cd ..
|
||||
cd template_askama && cargo check && cd ..
|
||||
|
@ -6,9 +6,9 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git" }
|
||||
dotenv = "0.13.0"
|
||||
env_logger = "0.5.10"
|
||||
futures = "0.1.22"
|
||||
|
@ -1,6 +1,6 @@
|
||||
use actix_files::NamedFile;
|
||||
use actix_session::Session;
|
||||
use actix_web::middleware::ErrorHandlerResponse;
|
||||
use actix_web::middleware::errhandlers::ErrorHandlerResponse;
|
||||
use actix_web::{dev, error, http, web, Error, HttpResponse, Responder, Result};
|
||||
use futures::future::{err, Either, Future, IntoFuture};
|
||||
use tera::{Context, Tera};
|
||||
|
@ -11,7 +11,7 @@ use std::{env, io};
|
||||
|
||||
use actix_files as fs;
|
||||
use actix_session::CookieSession;
|
||||
use actix_web::middleware::{ErrorHandlers, Logger};
|
||||
use actix_web::middleware::{errhandlers::ErrorHandlers, Logger};
|
||||
use actix_web::{http, web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use tera::Tera;
|
||||
@ -49,11 +49,11 @@ fn main() -> io::Result<()> {
|
||||
.handler(http::StatusCode::NOT_FOUND, api::not_found);
|
||||
|
||||
App::new()
|
||||
.state(templates)
|
||||
.state(pool.clone())
|
||||
.middleware(Logger::default())
|
||||
.middleware(session_store)
|
||||
.middleware(error_handlers)
|
||||
.data(templates)
|
||||
.data(pool.clone())
|
||||
.wrap(Logger::default())
|
||||
.wrap(session_store)
|
||||
.wrap(error_handlers)
|
||||
.service(web::resource("/").route(web::get().to_async(api::index)))
|
||||
.service(web::resource("/todo").route(web::post().to_async(api::create)))
|
||||
.service(
|
||||
|
@ -7,7 +7,7 @@ workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.2"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
dotenv = "0.10"
|
||||
env_logger = "0.5"
|
||||
|
@ -78,8 +78,8 @@ fn main() -> io::Result<()> {
|
||||
// Start http server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.state(pool.clone())
|
||||
.middleware(middleware::Logger::default())
|
||||
.data(pool.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
web::resource("/asyncio_weather")
|
||||
.route(web::get().to_async(asyncio_weather)),
|
||||
|
@ -8,7 +8,7 @@ workspace = ".."
|
||||
[dependencies]
|
||||
actix-rt = "0.2"
|
||||
actix-http = { git="https://github.com/actix/actix-http.git", features=["ssl"] }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0", features=["ssl"] }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", features=["ssl"] }
|
||||
|
||||
futures = "0.1"
|
||||
serde = "1.0.43"
|
||||
|
@ -24,8 +24,9 @@ use std::collections::HashMap;
|
||||
use std::io;
|
||||
|
||||
use actix_http::client;
|
||||
use actix_web::{web, App, Error, HttpMessage, HttpResponse, HttpServer};
|
||||
use futures::future::{ok, Future};
|
||||
use actix_web::web::BytesMut;
|
||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::{Future, Stream};
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Debug, Validate, Deserialize, Serialize)]
|
||||
@ -54,7 +55,7 @@ struct HttpBinResponse {
|
||||
|
||||
/// post json to httpbin, get it back in the response body, return deserialized
|
||||
fn step_x_v1(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> {
|
||||
let mut connector = client::Connector::default().service();
|
||||
let mut connector = client::Connector::new().service();
|
||||
|
||||
Box::new(
|
||||
client::ClientRequest::post("https://httpbin.org/post")
|
||||
@ -62,13 +63,17 @@ fn step_x_v1(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> {
|
||||
.unwrap()
|
||||
.send(&mut connector)
|
||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
||||
.and_then(|mut resp| {
|
||||
resp.body() // <- this is MessageBody type, resolves to complete body
|
||||
.and_then(|resp| {
|
||||
resp // <- this is MessageBody type, resolves to complete body
|
||||
.from_err() // <- convert PayloadError to an Error
|
||||
.and_then(|body| {
|
||||
let resp: HttpBinResponse =
|
||||
.fold(BytesMut::new(), |mut acc, chunk| {
|
||||
acc.extend_from_slice(&chunk);
|
||||
Ok::<_, Error>(acc)
|
||||
})
|
||||
.map(|body| {
|
||||
let body: HttpBinResponse =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
ok(resp.json)
|
||||
body.json
|
||||
})
|
||||
}),
|
||||
)
|
||||
@ -95,19 +100,22 @@ fn create_something_v1(
|
||||
|
||||
/// post json to httpbin, get it back in the response body, return deserialized
|
||||
fn step_x_v2(data: SomeData) -> impl Future<Item = SomeData, Error = Error> {
|
||||
let mut connector = client::Connector::default().service();
|
||||
let mut connector = client::Connector::new().service();
|
||||
|
||||
client::ClientRequest::post("https://httpbin.org/post")
|
||||
.json(data)
|
||||
.unwrap()
|
||||
.send(&mut connector)
|
||||
.map_err(Error::from) // <- convert SendRequestError to an Error
|
||||
.and_then(|mut resp| {
|
||||
resp.body() // <- this is MessageBody type, resolves to complete body
|
||||
.from_err() // <- convert PayloadError to an Error
|
||||
.and_then(|body| {
|
||||
let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
||||
ok(resp.json)
|
||||
.and_then(|resp| {
|
||||
resp.from_err()
|
||||
.fold(BytesMut::new(), |mut acc, chunk| {
|
||||
acc.extend_from_slice(&chunk);
|
||||
Ok::<_, Error>(acc)
|
||||
})
|
||||
.map(|body| {
|
||||
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
||||
body.json
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.2"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
futures = "0.1.25"
|
||||
env_logger = "0.5"
|
||||
|
@ -83,9 +83,9 @@ fn main() -> io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
// cookie session middleware
|
||||
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
// register favicon
|
||||
.service(favicon)
|
||||
// register simple route, handle all methods
|
||||
|
@ -6,5 +6,5 @@ edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
env_logger = "0.6"
|
||||
|
@ -22,8 +22,8 @@ fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.middleware(middleware::Logger::default())
|
||||
.middleware(IdentityService::new(
|
||||
.wrap(middleware::Logger::default())
|
||||
.wrap(IdentityService::new(
|
||||
CookieIdentityPolicy::new(&[0; 32])
|
||||
.name("auth-example")
|
||||
.secure(false),
|
||||
|
@ -6,8 +6,8 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-session = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
futures = "0.1"
|
||||
time = "0.1"
|
||||
|
@ -32,9 +32,9 @@ fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(Logger::default())
|
||||
.wrap(Logger::default())
|
||||
// cookie session middleware
|
||||
.middleware(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
|
@ -6,7 +6,7 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
bytes = "0.4"
|
||||
env_logger = "0.6"
|
||||
|
@ -10,7 +10,7 @@ extern crate diesel;
|
||||
extern crate serde_derive;
|
||||
|
||||
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use futures::future::{err, Either};
|
||||
@ -61,13 +61,10 @@ struct MyUser {
|
||||
const MAX_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
/// This handler manually load request payload and parse json object
|
||||
fn index_add<P>(
|
||||
pl: web::Payload<P>,
|
||||
fn index_add(
|
||||
pl: web::Payload,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
where
|
||||
P: Stream<Item = Bytes, Error = error::PayloadError>,
|
||||
{
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pl
|
||||
// `Future::from_err` acts like `?` in that it coerces the error type from
|
||||
// the future into the final error type
|
||||
@ -132,9 +129,9 @@ fn main() -> std::io::Result<()> {
|
||||
// Start http server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.state(pool.clone())
|
||||
.data(pool.clone())
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
// This can be called with:
|
||||
// curl -S --header "Content-Type: application/json" --request POST --data '{"name":"xyz"}' http://127.0.0.1:8080/add
|
||||
// Use of the extractors makes some post conditions simpler such
|
||||
@ -142,7 +139,18 @@ fn main() -> std::io::Result<()> {
|
||||
.service(
|
||||
web::resource("/add2").route(
|
||||
web::post()
|
||||
.config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
|
||||
.data(
|
||||
web::JsonConfig::default()
|
||||
.limit(4096) // <- limit size of the payload
|
||||
.error_handler(|err, _| {
|
||||
// <- create custom error response
|
||||
error::InternalError::from_response(
|
||||
err,
|
||||
HttpResponse::Conflict().finish(),
|
||||
)
|
||||
.into()
|
||||
}),
|
||||
)
|
||||
.to_async(add2),
|
||||
),
|
||||
)
|
||||
|
@ -6,7 +6,7 @@ edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
derive_more = "0.14.0"
|
||||
futures = "0.1.23"
|
||||
|
@ -6,7 +6,7 @@ edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
|
@ -12,10 +12,10 @@ struct AppState {
|
||||
fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.state(AppState {
|
||||
.data(AppState {
|
||||
foo: "bar".to_string(),
|
||||
})
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.service(web::resource("/post1").route(web::post().to(handle_post_1)))
|
||||
.service(web::resource("/post2").route(web::post().to(handle_post_2)))
|
||||
|
@ -7,4 +7,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.6"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
@ -12,7 +12,7 @@ fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/index.html").to(|| "Hello world!"))
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
|
@ -6,7 +6,7 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
|
@ -4,7 +4,7 @@ extern crate json;
|
||||
use actix_web::{
|
||||
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use futures::{Future, Stream};
|
||||
use json::JsonValue;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
@ -32,12 +32,9 @@ fn extract_item(item: web::Json<MyObj>, req: HttpRequest) -> HttpResponse {
|
||||
const MAX_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
/// This handler manually load request payload and parse json object
|
||||
fn index_manual<P>(
|
||||
payload: web::Payload<P>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
where
|
||||
P: Stream<Item = Bytes, Error = error::PayloadError>,
|
||||
{
|
||||
fn index_manual(
|
||||
payload: web::Payload,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
// payload is a stream of Bytes objects
|
||||
payload
|
||||
// `Future::from_err` acts like `?` in that it coerces the error type from
|
||||
@ -64,12 +61,7 @@ where
|
||||
}
|
||||
|
||||
/// This handler manually load request payload and parse json-rust
|
||||
fn index_mjsonrust<P>(
|
||||
pl: web::Payload<P>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
where
|
||||
P: Stream<Item = Bytes, Error = error::PayloadError>,
|
||||
{
|
||||
fn index_mjsonrust(pl: web::Payload) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pl.concat2().from_err().and_then(|body| {
|
||||
// body is loaded, now we can deserialize json-rust
|
||||
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result
|
||||
@ -90,18 +82,18 @@ fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
web::resource("/extractor").route(
|
||||
web::post()
|
||||
.config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
|
||||
.data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
|
||||
.to(index),
|
||||
),
|
||||
)
|
||||
.service(
|
||||
web::resource("/extractor2").route(
|
||||
web::post()
|
||||
.config(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
|
||||
.data(web::JsonConfig::default().limit(4096)) // <- limit size of the payload
|
||||
.to_async(extract_item),
|
||||
),
|
||||
)
|
||||
|
@ -6,7 +6,7 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
env_logger = "0.6"
|
||||
futures = "0.1"
|
||||
serde = "1.0"
|
||||
|
@ -49,8 +49,8 @@ fn main() -> io::Result<()> {
|
||||
// Start http server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.state(schema.clone())
|
||||
.middleware(middleware::Logger::default())
|
||||
.data(schema.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/graphql").route(web::post().to_async(graphql)))
|
||||
.service(web::resource("/graphiql").route(web::get().to(graphiql)))
|
||||
})
|
||||
|
@ -7,6 +7,6 @@ workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.3.3"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
futures = "0.1.25"
|
||||
env_logger = "0.6"
|
@ -11,8 +11,8 @@ fn main() -> std::io::Result<()> {
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.middleware(redirect::CheckLogin)
|
||||
.middleware(simple::SayHi)
|
||||
.wrap(redirect::CheckLogin)
|
||||
.wrap(simple::SayHi)
|
||||
.service(web::resource("/login").to(|| {
|
||||
"You are on /login. Go to src/redirect.rs to change this behavior."
|
||||
}))
|
||||
|
@ -7,7 +7,7 @@ workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.2"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
futures = "0.1"
|
||||
env_logger = "0.6"
|
||||
|
@ -45,8 +45,8 @@ fn main() -> io::Result<()> {
|
||||
// start http server
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.state(pool.clone()) // <- store db pool in app state
|
||||
.middleware(middleware::Logger::default())
|
||||
.data(pool.clone()) // <- store db pool in app state
|
||||
.wrap(middleware::Logger::default())
|
||||
.route("/{name}", web::get().to_async(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
|
@ -6,7 +6,7 @@ workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
||||
futures = "0.1"
|
||||
env_logger = "0.6"
|
||||
|
@ -35,7 +35,7 @@ fn main() -> io::Result<()> {
|
||||
App::new()
|
||||
.data(counter.clone()) // <- create app with shared state
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
// register simple handler, handle all methods
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
|
@ -9,5 +9,5 @@ edition = "2018"
|
||||
futures = "0.1"
|
||||
env_logger = "0.5"
|
||||
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git" }
|
||||
|
@ -8,7 +8,7 @@ fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
// static files
|
||||
fs::Files::new("/", "./static/").index_file("index.html"),
|
||||
|
@ -8,7 +8,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
env_logger = "0.6"
|
||||
askama = "0.6"
|
||||
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]
|
||||
askama = "0.6"
|
||||
|
@ -8,4 +8,4 @@ edition = "2018"
|
||||
[dependencies]
|
||||
env_logger = "0.6"
|
||||
tera = "*"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
|
@ -33,8 +33,8 @@ fn main() -> std::io::Result<()> {
|
||||
compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"));
|
||||
|
||||
App::new()
|
||||
.state(tera)
|
||||
.middleware(middleware::Logger::default()) // enable logger
|
||||
.data(tera)
|
||||
.wrap(middleware::Logger::default()) // enable logger
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
|
@ -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
|
||||
|
@ -14,4 +14,4 @@ env_logger = "0.5"
|
||||
openssl = { version="0.10" }
|
||||
|
||||
actix-rt = "0.2"
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0", features=["ssl"] }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", features=["ssl"] }
|
||||
|
@ -27,7 +27,7 @@ fn main() -> io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
// register simple handler, handle all methods
|
||||
.service(web::resource("/index.html").to(index))
|
||||
// with path parameters
|
||||
|
@ -15,9 +15,9 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
actix = { git="https://github.com/actix/actix.git" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web-actors = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
|
||||
actix-web = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-web-actors = { git="https://github.com/actix/actix-web.git" }
|
||||
actix-files = { git="https://github.com/actix/actix-web.git" }
|
||||
env_logger = "0.6"
|
||||
futures = "0.1"
|
||||
bytes = "0.4"
|
@ -7,12 +7,8 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use actix::prelude::*;
|
||||
use actix_files as fs;
|
||||
use actix_web::{
|
||||
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
};
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web_actors::ws;
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
|
||||
/// How often heartbeat pings are sent
|
||||
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
||||
@ -20,10 +16,7 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
|
||||
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// do websocket handshake and start `MyWebSocket` actor
|
||||
fn ws_index<S>(r: HttpRequest, stream: web::Payload<S>) -> Result<HttpResponse, Error>
|
||||
where
|
||||
S: Stream<Item = Bytes, Error = error::PayloadError> + 'static,
|
||||
{
|
||||
fn ws_index(r: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
||||
println!("{:?}", r);
|
||||
let res = ws::start(MyWebSocket::new(), &r, stream);
|
||||
println!("{:?}", res.as_ref().unwrap());
|
||||
@ -103,7 +96,7 @@ fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
// websocket route
|
||||
.service(web::resource("/ws/").route(web::get().to(ws_index)))
|
||||
// static files
|
||||
|
Loading…
Reference in New Issue
Block a user