Rocket/examples/raw_sqlite/src/main.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

#![feature(proc_macro_hygiene)]
#[macro_use] extern crate rocket;
2019-06-13 02:41:29 +00:00
2019-01-27 15:52:03 +00:00
use rusqlite::types::ToSql;
#[cfg(test)] mod tests;
use std::sync::Mutex;
use rocket::{Rocket, State};
use rusqlite::{Connection, Error};
type DbConn = Mutex<Connection>;
fn init_database(conn: &Connection) {
conn.execute("CREATE TABLE entries (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
2019-01-27 15:52:03 +00:00
)", &[] as &[&dyn ToSql])
.expect("create entries table");
conn.execute("INSERT INTO entries (id, name) VALUES ($1, $2)",
2019-01-27 15:52:03 +00:00
&[&0 as &dyn ToSql, &"Rocketeer"])
.expect("insert single entry into entries table");
}
#[get("/")]
2019-06-13 02:41:29 +00:00
fn hello(db_conn: State<'_, DbConn>) -> Result<String, Error> {
db_conn.lock()
.expect("db connection lock")
.query_row("SELECT name FROM entries WHERE id = 0",
2019-01-27 15:52:03 +00:00
&[] as &[&dyn ToSql], |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();
}