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