#![feature(plugin, decl_macro, proc_macro_non_items)] #![plugin(rocket_codegen)] #[macro_use] extern crate rocket; extern crate rusqlite; #[cfg(test)] mod tests; use std::sync::Mutex; use rocket::{Rocket, State}; use rusqlite::{Connection, Error}; type DbConn = Mutex; fn init_database(conn: &Connection) { conn.execute("CREATE TABLE entries ( id INTEGER PRIMARY KEY, name TEXT NOT NULL )", &[]) .expect("create entries table"); conn.execute("INSERT INTO entries (id, name) VALUES ($1, $2)", &[&0, &"Rocketeer"]) .expect("insert single entry into entries table"); } #[get("/")] fn hello(db_conn: State) -> Result { db_conn.lock() .expect("db connection lock") .query_row("SELECT name FROM entries WHERE id = 0", &[], |row| { row.get(0) }) } fn rocket() -> Rocket { // Open a new in-memory SQLite database. let conn = Connection::open_in_memory().expect("in memory db"); // Initialize the `entries` table in the in-memory database. init_database(&conn); // Have Rocket manage the database pool. rocket::ignite() .manage(Mutex::new(conn)) .mount("/", routes![hello]) } fn main() { rocket().launch(); }