Skip to content

Commit

Permalink
Add initial diesel example.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinbankier committed May 14, 2019
1 parent 40bbe32 commit 5c8ec9e
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/diesel/Cargo.toml
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"
89 changes: 89 additions & 0 deletions examples/diesel/README.md
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)
5 changes: 5 additions & 0 deletions examples/diesel/diesel.toml
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE products;
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 added examples/diesel/products.db
Binary file not shown.
39 changes: 39 additions & 0 deletions examples/diesel/src/main.rs
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));
}

0 comments on commit 5c8ec9e

Please sign in to comment.