-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmod.rs
148 lines (131 loc) · 4.19 KB
/
mod.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::path::PathBuf;
use std::sync::Mutex;
use gethostname::gethostname;
use rocket::get;
use rocket::response::NamedFile;
use rocket::State;
use rocket_contrib::json::Json;
use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};
use rocket_okapi::{openapi, routes_with_openapi};
use crate::config::AWConfig;
use aw_datastore::Datastore;
use aw_models::Info;
pub struct ServerState {
pub datastore: Mutex<Datastore>,
pub asset_path: PathBuf,
pub device_id: String,
}
#[macro_use]
mod util;
mod bucket;
mod cors;
mod export;
mod import;
mod query;
mod settings;
pub use util::HttpErrorJson;
#[get("/")]
fn root_index(state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("index.html")).ok()
}
#[get("/css/<file..>")]
fn root_css(file: PathBuf, state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("css").join(file)).ok()
}
#[get("/fonts/<file..>")]
fn root_fonts(file: PathBuf, state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("fonts").join(file)).ok()
}
#[get("/js/<file..>")]
fn root_js(file: PathBuf, state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("js").join(file)).ok()
}
#[get("/static/<file..>")]
fn root_static(file: PathBuf, state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("static").join(file)).ok()
}
#[get("/favicon.ico")]
fn root_favicon(state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("favicon.ico")).ok()
}
#[openapi]
#[get("/")]
fn server_info(config: State<AWConfig>, state: State<ServerState>) -> Json<Info> {
#[allow(clippy::or_fun_call)]
let hostname = gethostname().into_string().unwrap_or("unknown".to_string());
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
Json(Info {
hostname,
version: format!("v{} (rust)", VERSION.unwrap_or("(unknown)")),
testing: config.testing,
device_id: state.device_id.clone(),
})
}
fn get_docs() -> SwaggerUIConfig {
use rocket_okapi::swagger_ui::UrlObject;
SwaggerUIConfig {
url: "/info/openapi.json".to_string(),
urls: vec![
UrlObject::new("Info", "/api/0/info/openapi.json"),
UrlObject::new("Bucket", "/api/0/buckets/openapi.json"),
UrlObject::new("Export", "/api/0/export/openapi.json"),
],
..Default::default()
}
}
pub fn build_rocket(server_state: ServerState, config: AWConfig) -> rocket::Rocket {
info!(
"Starting aw-server-rust at {}:{}",
config.address, config.port
);
rocket::custom(config.to_rocket_config())
.mount(
"/",
routes![
root_index,
root_favicon,
root_fonts,
root_css,
root_js,
root_static,
],
)
.mount("/api/0/info", routes_with_openapi![server_info])
.mount(
"/api/0/buckets",
routes_with_openapi![
bucket::bucket_new,
bucket::bucket_delete,
bucket::buckets_get,
bucket::bucket_get,
bucket::bucket_events_get,
bucket::bucket_events_create,
bucket::bucket_events_heartbeat,
bucket::bucket_event_count,
bucket::bucket_events_delete_by_id,
bucket::bucket_export
],
)
.mount("/api/0/query", routes![query::query])
.mount(
"/api/0/import",
routes![import::bucket_import_json, import::bucket_import_form],
)
.mount(
"/api/0/export",
routes_with_openapi![export::buckets_export],
)
.mount(
"/api/0/settings",
routes![
settings::setting_get,
settings::settings_list_get,
settings::setting_set,
settings::setting_delete
],
)
.mount("/api", make_swagger_ui(&get_docs()))
.attach(cors::cors(&config))
.manage(server_state)
.manage(config)
}