Skip to content

Instantly share code, notes, and snippets.

@ubnt-intrepid
Last active July 20, 2018 07:33
Show Gist options
  • Save ubnt-intrepid/266ecd06d28b0072eb94d9c918183afd to your computer and use it in GitHub Desktop.
Save ubnt-intrepid/266ecd06d28b0072eb94d9c918183afd to your computer and use it in GitHub Desktop.
tsukuyomi 0.3 preview
#[macro_use]
extern crate tsukuyomi;
extern crate http;
use tsukuyomi::App;
use http::Method;
fn establish_db_connection(addr: &str) -> Pool { ... }
fn main() -> tsukuyomi::AppResult<()> {
let pool = establish_db_connection("postgres://localhost/app");
global_key!(static DB_POOL: Pool);
let app = App::builder()
.route(("/", hello))
.mount(("/api", |scope| {
scope.route(("/posts", Method::GET, all_posts))
.route(("/posts/:id", Method::GET, get_post))
.route(("/posts", Method::POST, add_post))
.route(("/posts/:id", Method::DELETE, delete_post));
// scope-local value
scope.set(&DB_POOL, pool);
// scope-level error handler (implemented as a `Modifier`)
scope.modifier(JsonErrorHandler::new());
})
.finish()?;
tsukuyomi::run(app)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment