Skip to content

Commit 510e72f

Browse files
committed
Move health and docs routes to meta module
Signed-off-by: Lilly Rose Berner <[email protected]>
1 parent e0aa23f commit 510e72f

File tree

5 files changed

+55
-54
lines changed

5 files changed

+55
-54
lines changed

echo/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use database::{background_task::BackgroundTask, PgDatabase};
66
use figment::providers::{Format, Json};
77
use rocket::{catchers, fairing::AdHoc, figment, routes};
88
use rocket_db_pools::Database;
9-
use routes::{docs, health, pastes, security};
9+
use routes::{meta, pastes, security};
1010
use scanners::InitScanners;
1111

1212
pub mod config;
@@ -21,11 +21,11 @@ pub mod utils;
2121
#[rocket::launch]
2222
fn rocket() -> _ {
2323
let routes = routes![
24-
routes::get_root,
2524
cors::snatcher,
26-
docs::get_docs,
27-
docs::get_spec,
28-
health::health,
25+
meta::get_root,
26+
meta::get_docs,
27+
meta::get_spec,
28+
meta::get_health,
2929
pastes::get_paste,
3030
pastes::create_paste_simple,
3131
pastes::create_paste_structured,

echo/src/routes/docs.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

echo/src/routes/health.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

echo/src/routes/meta.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use rocket::{
2+
get,
3+
response::{
4+
content::{RawHtml, RawJson},
5+
Redirect,
6+
},
7+
serde::json::Json,
8+
uri,
9+
};
10+
use rocket_db_pools::{
11+
sqlx::{self, Row},
12+
Connection,
13+
};
14+
use scalar_doc::{Documentation, Theme};
15+
16+
use crate::{
17+
database::PgDatabase,
18+
result::{HTTPError, Result},
19+
};
20+
21+
#[get("/")]
22+
pub fn get_root() -> Redirect {
23+
Redirect::temporary(uri!("/docs"))
24+
}
25+
26+
#[get("/docs")]
27+
pub fn get_docs() -> RawHtml<String> {
28+
let docs = Documentation::new("MystBin API Documentation", "/docs/spec.json")
29+
.theme(Theme::Alternate)
30+
.build()
31+
.unwrap();
32+
33+
RawHtml(docs)
34+
}
35+
36+
#[get("/docs/spec.json")]
37+
pub fn get_spec() -> RawJson<&'static str> {
38+
RawJson(include_str!("../../openapi.json"))
39+
}
40+
41+
#[get("/health")]
42+
pub async fn get_health(mut db: Connection<PgDatabase>) -> Result<String> {
43+
let result = sqlx::query("SELECT 'ok'").fetch_one(&mut **db).await;
44+
45+
match result {
46+
Ok(row) => Ok(row.get(0)),
47+
Err(_) => Err(HTTPError::new(503, "Database connection unhealthy.")),
48+
}
49+
}

echo/src/routes/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
use rocket::{get, response::Redirect, uri};
2-
3-
pub mod docs;
4-
pub mod health;
1+
pub mod meta;
52
pub mod pastes;
63
pub mod security;
7-
8-
#[get("/")]
9-
pub fn get_root() -> Redirect {
10-
Redirect::temporary(uri!("/docs"))
11-
}

0 commit comments

Comments
 (0)