-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
82 lines (70 loc) · 2.02 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#![feature(decl_macro)]
#![feature(try_trait)]
#[macro_use]
extern crate rocket;
use rocket::State;
use rocket::{http::Status, response::Responder};
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use sled_extensions::bincode::Tree;
use sled_extensions::DbExt;
use std::option::NoneError;
#[derive(thiserror::Error, Debug)]
pub enum ServerError {
#[error("sled db error")]
SledError(#[from] sled_extensions::Error),
#[error("resource not found")]
NotFound,
}
impl From<NoneError> for ServerError {
fn from(_: NoneError) -> Self {
ServerError::NotFound
}
}
impl<'a> Responder<'a> for ServerError {
fn respond_to(self, _: &rocket::Request) -> Result<rocket::Response<'a>, Status> {
match self {
Self::SledError(_) => Err(Status::InternalServerError),
Self::NotFound => Err(Status::NotFound),
}
}
}
struct Database {
users: Tree<User>,
}
#[derive(Deserialize, Serialize, Clone)]
struct User {
username: String,
favorite_food: String,
}
type EndpointResult<T> = Result<T, ServerError>;
#[get("/users/<username>")]
fn get_user(db: State<Database>, username: String) -> EndpointResult<Json<User>> {
todo!()
}
#[delete("/users/<username>")]
fn delete_user(db: State<Database>, username: String) -> EndpointResult<Json<User>> {
let user = db.users.remove(username.as_bytes())??;
Ok(Json(user))
}
#[put("/users", data = "<user>")]
fn put_user(db: State<Database>, user: Json<User>) -> EndpointResult<Json<User>> {
db.users
.insert(user.username.as_bytes(), user.clone())
.unwrap();
Ok(Json(user.0))
}
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();
}