Skip to content
Permalink
Browse files
Managed state
  • Loading branch information
marcusbuffett committed Oct 23, 2020
1 parent 156f632 commit 1ede32c744e0872aa5fac82deacb287edf0cda5a
Showing 1 changed file with 20 additions and 3 deletions.
@@ -3,8 +3,16 @@
#[macro_use]
extern crate rocket;

use rocket::State;
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use sled_extensions::bincode::Tree;
use sled_extensions::DbExt;
use std::error::Error;

struct Database {
users: Tree<User>,
}

#[derive(Deserialize, Serialize, Clone)]
struct User {
@@ -15,22 +23,31 @@ struct User {
type EndpointResult<T> = Result<T, &'static str>;

#[get("/users/<username>")]
fn get_user(username: String) -> EndpointResult<Json<User>> {
fn get_user(db: State<Database>, username: String) -> EndpointResult<Json<User>> {
todo!()
}

#[delete("/users/<username>")]
fn delete_user(username: String) -> EndpointResult<Json<User>> {
fn delete_user(db: State<Database>, username: String) -> EndpointResult<Json<User>> {
todo!()
}

#[put("/users", data = "<user>")]
fn put_user(user: Json<User>) -> EndpointResult<Json<User>> {
fn put_user(db: State<Database>, user: Json<User>) -> EndpointResult<Json<User>> {
todo!()
}

fn main() {
let db = sled_extensions::Config::default()
.path("./sled_data")
.open()
.expect("Failed to open sled db");
rocket::ignite()
.manage(Database {
users: db
.open_bincode_tree("users")
.expect("failed to open user tree"),
})
.mount("/api/", routes![get_user, put_user, delete_user])
.launch();
}

0 comments on commit 1ede32c

Please sign in to comment.