algohub_server/routes/
index.rs1use std::path::{Path, PathBuf};
2
3use super::asset;
4use super::category;
5use super::contest;
6use super::organization;
7use super::problem;
8use super::solution;
9use super::submission;
10use crate::{cors::CORS, routes::account};
11use anyhow::Result;
12use rocket::fs::NamedFile;
13use surrealdb::engine::remote::ws::Client;
14use surrealdb::{engine::remote::ws::Ws, opt::auth::Root, Surreal};
15
16#[get("/")]
17async fn index() -> Result<NamedFile, std::io::Error> {
18 NamedFile::open("dist/index.html").await
19}
20
21#[get("/<file..>", rank = 1)]
22async fn files(file: PathBuf) -> Option<NamedFile> {
23 NamedFile::open(Path::new("dist/").join(file)).await.ok()
24}
25
26pub async fn init_db(db_addr: &str) -> Result<Surreal<Client>> {
27 let db = Surreal::new::<Ws>(db_addr)
28 .await
29 .expect("Failed to connect to database");
30
31 db.use_ns("main")
32 .use_db("acm")
33 .await
34 .expect("Failed to use database");
35 db.signin(Root {
36 username: "root",
37 password: "root",
38 })
39 .await
40 .expect("Failed to authenticate");
41
42 Ok(db)
43}
44
45pub async fn rocket(db: Surreal<Client>) -> rocket::Rocket<rocket::Build> {
46 rocket::build()
47 .attach(CORS)
48 .mount("/", routes![index, files])
49 .mount("/account", account::routes())
50 .mount("/asset", asset::routes())
51 .mount("/problem", problem::routes())
52 .mount("/org", organization::routes())
53 .mount("/category", category::routes())
54 .mount("/contest", contest::routes())
55 .mount("/code", submission::routes())
56 .mount("/solution", solution::routes())
57 .manage(db)
58}