1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

update juniper and middleware examples

This commit is contained in:
Nikolay Kim 2019-03-10 19:19:50 -07:00
parent 0b46125f5d
commit 14eed91fcd
7 changed files with 170 additions and 152 deletions

View File

@ -77,7 +77,6 @@ fn with_param(req: HttpRequest, path: web::Path<(String,)>) -> HttpResponse {
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
env::set_var("RUST_LOG", "actix_web=debug"); env::set_var("RUST_LOG", "actix_web=debug");
env::set_var("RUST_BACKTRACE", "1");
env_logger::init(); env_logger::init();
let sys = actix_rt::System::new("basic-example"); let sys = actix_rt::System::new("basic-example");

View File

@ -2,17 +2,14 @@
name = "juniper-example" name = "juniper-example"
version = "0.1.0" version = "0.1.0"
authors = ["pyros2097 <pyros2097@gmail.com>"] authors = ["pyros2097 <pyros2097@gmail.com>"]
workspace = "../" workspace = ".."
edition = "2018"
[dependencies] [dependencies]
env_logger = "0.5" actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
env_logger = "0.6"
actix = "0.7"
actix-web = "0.7"
futures = "0.1" futures = "0.1"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
juniper = "0.9.2" juniper = "0.9.2"

View File

@ -1,110 +1,59 @@
//! Actix web juniper example //! Actix web juniper example
//! //!
//! A simple example integrating juniper in actix-web //! A simple example integrating juniper in actix-web
extern crate serde; use std::io;
extern crate serde_json; use std::sync::Arc;
#[macro_use]
extern crate serde_derive;
#[macro_use] #[macro_use]
extern crate juniper; extern crate juniper;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
use actix::prelude::*; use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use actix_web::{
http, middleware, server, App, AsyncResponder, Error, FutureResponse, HttpRequest,
HttpResponse, Json, State,
};
use futures::future::Future; use futures::future::Future;
use juniper::http::graphiql::graphiql_source; use juniper::http::graphiql::graphiql_source;
use juniper::http::GraphQLRequest; use juniper::http::GraphQLRequest;
mod schema; mod schema;
use schema::create_schema; use crate::schema::{create_schema, Schema};
use schema::Schema;
struct AppState { fn graphiql() -> HttpResponse {
executor: Addr<GraphQLExecutor>,
}
#[derive(Serialize, Deserialize)]
pub struct GraphQLData(GraphQLRequest);
impl Message for GraphQLData {
type Result = Result<String, Error>;
}
pub struct GraphQLExecutor {
schema: std::sync::Arc<Schema>,
}
impl GraphQLExecutor {
fn new(schema: std::sync::Arc<Schema>) -> GraphQLExecutor {
GraphQLExecutor { schema: schema }
}
}
impl Actor for GraphQLExecutor {
type Context = SyncContext<Self>;
}
impl Handler<GraphQLData> for GraphQLExecutor {
type Result = Result<String, Error>;
fn handle(&mut self, msg: GraphQLData, _: &mut Self::Context) -> Self::Result {
let res = msg.0.execute(&self.schema, &());
let res_text = serde_json::to_string(&res)?;
Ok(res_text)
}
}
fn graphiql(_req: &HttpRequest<AppState>) -> Result<HttpResponse, Error> {
let html = graphiql_source("http://127.0.0.1:8080/graphql"); let html = graphiql_source("http://127.0.0.1:8080/graphql");
Ok(HttpResponse::Ok() HttpResponse::Ok()
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(html)) .body(html)
} }
fn graphql( fn graphql(
(st, data): (State<AppState>, Json<GraphQLData>), st: web::State<Arc<Schema>>,
) -> FutureResponse<HttpResponse> { data: web::Json<GraphQLRequest>,
st.executor ) -> impl Future<Item = HttpResponse, Error = Error> {
.send(data.0) web::block(move || {
.from_err() let res = data.execute(&st, &());
.and_then(|res| match res { Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
Ok(user) => Ok(HttpResponse::Ok() })
.content_type("application/json") .map_err(Error::from)
.body(user)), .and_then(|user| {
Err(_) => Ok(HttpResponse::InternalServerError().into()), Ok(HttpResponse::Ok()
}) .content_type("application/json")
.responder() .body(user))
})
} }
fn main() { fn main() -> io::Result<()> {
::std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
let sys = actix::System::new("juniper-example");
// Create Juniper schema
let schema = std::sync::Arc::new(create_schema()); let schema = std::sync::Arc::new(create_schema());
let addr = SyncArbiter::start(3, move || GraphQLExecutor::new(schema.clone()));
// Start http server // Start http server
server::new(move || { HttpServer::new(move || {
App::with_state(AppState { App::new()
executor: addr.clone(), .state(schema.clone())
}) .middleware(middleware::Logger::default())
// enable logger .service(web::resource("/graphql").route(web::post().to_async(graphql)))
.middleware(middleware::Logger::default()) .service(web::resource("/graphiql").route(web::get().to(graphiql)))
.resource("/graphql", |r| r.method(http::Method::POST).with(graphql))
.resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.unwrap() .run()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
} }

View File

@ -2,7 +2,11 @@
name = "middleware-example" name = "middleware-example"
version = "0.1.0" version = "0.1.0"
authors = ["Gorm Casper <gcasper@gmail.com>"] authors = ["Gorm Casper <gcasper@gmail.com>"]
edition = "2018"
workspace = ".."
[dependencies] [dependencies]
actix = "0.7" actix-service = "0.3.3"
actix-web = "0.7" actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" }
futures = "0.1.25"
env_logger = "0.6"

View File

@ -1,32 +1,27 @@
extern crate actix; use actix_web::{web, App, HttpServer};
extern crate actix_web;
use actix_web::{server, App};
#[allow(dead_code)] #[allow(dead_code)]
mod redirect; mod redirect;
#[allow(dead_code)] #[allow(dead_code)]
mod simple; mod simple;
fn main() { fn main() -> std::io::Result<()> {
let sys = actix::System::new("middleware-example"); std::env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init();
let _addr = server::new(|| { HttpServer::new(|| {
App::new() App::new()
.middleware(redirect::CheckLogin)
.middleware(simple::SayHi) .middleware(simple::SayHi)
// .middleware(redirect::CheckLogin) .service(web::resource("/login").to(|| {
.resource("/login", |r| { "You are on /login. Go to src/redirect.rs to change this behavior."
r.f(|_| { }))
"You are on /login. Go to src/redirect.rs to change this behavior." .service(
}) web::resource("/").to(|| {
}) "Hello, middleware! Check the console where the server is run."
.resource("/", |r| { }),
r.f(|_| "Hello, middleware! Check the console where the server is run.") )
})
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")?
.unwrap() .run()
.start();
let _ = sys.run();
} }

View File

@ -1,28 +1,64 @@
extern crate actix_web; use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::middleware::{Middleware, Started}; use actix_web::{http, HttpResponse};
use actix_web::{http, HttpRequest, HttpResponse, Result}; use futures::future::{ok, Either, FutureResult};
use futures::Poll;
pub struct CheckLogin; pub struct CheckLogin;
impl<S> Middleware<S> for CheckLogin { impl<S, P, B> Transform<S> for CheckLogin
// We only need to hook into the `start` for this middleware. where
fn start(&self, req: &HttpRequest<S>) -> Result<Started> { S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
S::Future: 'static,
{
type Request = ServiceRequest<P>;
type Response = ServiceResponse<B>;
type Error = S::Error;
type InitError = ();
type Transform = CheckLoginMiddleware<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
ok(CheckLoginMiddleware { service })
}
}
pub struct CheckLoginMiddleware<S> {
service: S,
}
impl<S, P, B> Service for CheckLoginMiddleware<S>
where
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
S::Future: 'static,
{
type Request = ServiceRequest<P>;
type Response = ServiceResponse<B>;
type Error = S::Error;
type Future = Either<S::Future, FutureResult<Self::Response, Self::Error>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()
}
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
// We only need to hook into the `start` for this middleware.
let is_logged_in = false; // Change this to see the change in outcome in the browser let is_logged_in = false; // Change this to see the change in outcome in the browser
if is_logged_in { if is_logged_in {
return Ok(Started::Done); Either::A(self.service.call(req))
} else {
// Don't forward to /login if we are already on /login
if req.path() == "/login" {
Either::A(self.service.call(req))
} else {
Either::B(ok(req.into_response(
HttpResponse::Found()
.header(http::header::LOCATION, "/login")
.finish()
.into_body(),
)))
}
} }
// Don't forward to /login if we are already on /login
if req.path() == "/login" {
return Ok(Started::Done);
}
Ok(Started::Response(
HttpResponse::Found()
.header(http::header::LOCATION, "/login")
.finish(),
))
} }
} }

View File

@ -1,25 +1,63 @@
extern crate actix_web; use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use futures::future::{ok, FutureResult};
use futures::{Future, Poll};
use actix_web::middleware::{Finished, Middleware, Response, Started}; // There are two step in middleware processing.
use actix_web::{HttpRequest, HttpResponse, Result}; // 1. Middleware initialization, middleware factory get called with
// next service in chain as parameter.
// Middleware can get called at three stages during the request/response handling. Below is a // 2. Middleware's call method get called with normal request.
// struct that implements all three of them.
pub struct SayHi; pub struct SayHi;
impl<S> Middleware<S> for SayHi { // Middleware factory is `Transform` trait from actix-service crate
fn start(&self, req: &HttpRequest<S>) -> Result<Started> { // `S` - type of the next service
println!("Hi from start. You requested: {}", req.path()); // `P` - type of request's payload
Ok(Started::Done) // `B` - type of response's body
} impl<S, P, B> Transform<S> for SayHi
where
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
S::Future: 'static,
S::Error: 'static,
B: 'static,
{
type Request = ServiceRequest<P>;
type Response = ServiceResponse<B>;
type Error = S::Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn response(&self, _req: &HttpRequest<S>, resp: HttpResponse) -> Result<Response> { fn new_transform(&self, service: S) -> Self::Future {
println!("Hi from response"); ok(SayHiMiddleware { service })
Ok(Response::Done(resp)) }
} }
fn finish(&self, _req: &HttpRequest<S>, _resp: &HttpResponse) -> Finished { pub struct SayHiMiddleware<S> {
println!("Hi from finish"); service: S,
Finished::Done }
impl<S, P, B> Service for SayHiMiddleware<S>
where
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
S::Future: 'static,
S::Error: 'static,
B: 'static,
{
type Request = ServiceRequest<P>;
type Response = ServiceResponse<B>;
type Error = S::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()
}
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
println!("Hi from start. You requested: {}", req.path());
Box::new(self.service.call(req).and_then(|res| {
println!("Hi from response");
Ok(res)
}))
} }
} }