-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40bbe32
commit 5c8ec9e
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "gotham_diesel_example" | ||
version = "0.1.0" | ||
authors = ["Nicolas Pochet <npochet@gmail.com>", "Colin Bankier <colinbankier@gmail.com>"] | ||
description = "An example of the diesel middleware used with Gotham" | ||
license = "MIT/Apache-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
gotham = { path = "../../gotham" } | ||
gotham_derive = { path = "../../gotham_derive" } | ||
gotham_middleware_diesel = { path = "../../middleware/diesel" } | ||
|
||
hyper = "0.12" | ||
futures = "0.1" | ||
mime = "0.3" | ||
log = "0.3" | ||
diesel = { version = "1", features = ["sqlite"] } | ||
diesel_migrations = { version = "1", features = ["sqlite"] } | ||
r2d2 = "0.8" | ||
r2d2-diesel = "1" | ||
serde = "1" | ||
serde_json = "1" | ||
serde_derive = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Diesel Example | ||
|
||
An example of the Gotham Diesel Middelware. | ||
|
||
## Running | ||
|
||
From the `examples/diesel` directory: | ||
|
||
``` | ||
Terminal 1: | ||
$ cargo run | ||
Compiling basic_diesel (file:///.../examples/diesel) | ||
Finished dev [unoptimized + debuginfo] target(s) in 3.32 secs | ||
Running `../basic_diesel` | ||
Listening for requests at http://127.0.0.1:7878 | ||
Terminal 2: | ||
$ curl -v http://127.0.0.1:7878 | ||
* Rebuilt URL to: localhost:7878/ | ||
* Trying 127.0.0.1... | ||
* TCP_NODELAY set | ||
* Connected to localhost (127.0.0.1) port 7878 (#0) | ||
> GET / HTTP/1.1 | ||
> Host: localhost:7878 | ||
> User-Agent: curl/7.57.0 | ||
> Accept: */* | ||
> | ||
< HTTP/1.1 200 OK | ||
< X-Runtime-Microseconds: 12604 | ||
< Transfer-Encoding: chunked | ||
< Date: Fri, 02 Feb 2018 21:42:14 GMT | ||
< | ||
* Connection #0 to host localhost left intact | ||
[]% | ||
$ curl -v -H "Content-Type: application/json" -X POST -d '{"title":"test","price":1.0,"link":"http://localhost"}' 'http://localhost:7878' | ||
* Rebuilt URL to: http://localhost:7878/ | ||
* Trying 127.0.0.1... | ||
* TCP_NODELAY set | ||
* Connected to localhost (127.0.0.1) port 7878 (#0) | ||
> POST / HTTP/1.1 | ||
> Host: localhost:7878 | ||
> User-Agent: curl/7.57.0 | ||
> Accept: */* | ||
> Content-Type: application/json | ||
> Content-Length: 47 | ||
> | ||
* upload completely sent off: 47 out of 47 bytes | ||
< HTTP/1.1 201 Created | ||
< Content-Length: 0 | ||
< Content-Type: text/plain | ||
< X-Request-ID: fcfcb0e2-604a-4070-8f5c-97ba1e729888 | ||
< X-Frame-Options: DENY | ||
< X-XSS-Protection: 1; mode=block | ||
< X-Content-Type-Options: nosniff | ||
< X-Runtime-Microseconds: 9510 | ||
< Date: Fri, 02 Feb 2018 21:42:34 GMT | ||
< | ||
* Connection #0 to host localhost left intact | ||
$ curl -v localhost:7878 | ||
* Rebuilt URL to: localhost:7878/ | ||
* Trying 127.0.0.1... | ||
* TCP_NODELAY set | ||
* Connected to localhost (127.0.0.1) port 7878 (#0) | ||
> GET / HTTP/1.1 | ||
> Host: localhost:7878 | ||
> User-Agent: curl/7.58.0 | ||
> Accept: */* | ||
> | ||
< HTTP/1.1 200 OK | ||
< X-Runtime-Microseconds: 427 | ||
< Transfer-Encoding: chunked | ||
< Date: Tue, 06 Feb 2018 18:54:12 GMT | ||
< | ||
* Connection #0 to host localhost left intact | ||
[{"id":1,"title":"test","price":1.0,"link":"http://localhost"}]% | ||
``` | ||
|
||
## License | ||
|
||
Licensed under your option of: | ||
|
||
* [MIT License](../../LICENSE-MIT) | ||
* [Apache License, Version 2.0](../../LICENSE-APACHE) | ||
|
||
## Community | ||
|
||
The following policies guide participation in our project and our community: | ||
|
||
* [Conduct](../../CONDUCT.md) | ||
* [Contributing](../../CONTRIBUTING.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# For documentation on how to configure this file, | ||
# see diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/schema.rs" |
1 change: 1 addition & 0 deletions
1
examples/diesel/migrations/2019-04-09-111334_create_products/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE products; |
6 changes: 6 additions & 0 deletions
6
examples/diesel/migrations/2019-04-09-111334_create_products/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS products ( | ||
id INTEGER PRIMARY KEY, | ||
title VARCHAR NOT NULL, | ||
price REAL NOT NULL, | ||
link VARCHAR NOT NULL | ||
); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use gotham::handler::HandlerFuture; | ||
use gotham::handler::IntoHandlerError; | ||
use gotham::helpers::http::response::create_response; | ||
use gotham::pipeline::new_pipeline; | ||
use gotham::pipeline::single::single_pipeline; | ||
use gotham::router::builder::*; | ||
use gotham::router::Router; | ||
use gotham::state::{FromState, State}; | ||
use gotham_middleware_diesel::DieselMiddleware; | ||
|
||
static DATABASE_URL: &'static str = "products.db"; | ||
|
||
fn create_product_handler(mut state: State) -> Box<HandlerFuture> { | ||
let repo = Repo::borrow_from(&state).clone(); | ||
repo.run(move |conn| users.find(user_id).first(&conn)) | ||
} | ||
|
||
fn get_products_handler(mut state: State) -> Box<HandlerFuture> {} | ||
|
||
|
||
fn router(repo: Repo) -> Router { | ||
// Add the middleware to a new pipeline | ||
let (chain, pipeline) = single_pipeline(new_pipeline().add(DieselMiddleware::new(repo)).build()); | ||
|
||
|
||
// Build the router | ||
build_router(chain, pipeline, |route| { | ||
route.get("/").to(get_products_handler); | ||
route.post("/").to(create_product_handler); | ||
}) | ||
} | ||
|
||
/// Start a server and use a `Router` to dispatch requests | ||
fn main() { | ||
let addr = "127.0.0.1:7878"; | ||
|
||
println!("Listening for requests at http://{}", addr); | ||
gotham::start(addr, router(middleware)); | ||
} |