78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
mod guards;
|
|
mod request;
|
|
mod consent;
|
|
mod helpers;
|
|
|
|
use crate::app_config::CONFIG;
|
|
use crate::orm::DbConn;
|
|
use crate::api::consent::{consent as consent_template, consent_post};
|
|
|
|
// Alternativly iport rocket manually: use rocket::get;
|
|
use rocket::{fs::FileServer, Config};
|
|
use rocket::response::Redirect;
|
|
use rocket::fs::NamedFile;
|
|
use rocket::State;
|
|
use rocket_dyn_templates::Template;
|
|
use serde::Deserialize;
|
|
use core::marker::{Send, Sync};
|
|
use std::net::IpAddr;
|
|
use rocket::http::Status;
|
|
use rocket::Request;
|
|
|
|
|
|
|
|
#[derive(Responder, Debug)]
|
|
pub(super) enum HttpResult {
|
|
UnlockingSucceeded(String),
|
|
AckAuthReq(String),
|
|
AuthFailiure(String),
|
|
ShareNotFound(String),
|
|
TimelimitExceeded(String),
|
|
TooManyRequests(String),
|
|
UnlockingFailed(String),
|
|
}
|
|
|
|
impl Into<(Status, HttpResult)> for HttpResult {
|
|
fn into(self) -> (Status, HttpResult) {
|
|
let status = match &self {
|
|
HttpResult::UnlockingSucceeded(_) => Status::Ok,
|
|
HttpResult::AckAuthReq(_) => Status::Accepted,
|
|
HttpResult::AuthFailiure(_) => Status::Unauthorized,
|
|
HttpResult::ShareNotFound(_) => Status::NotFound,
|
|
HttpResult::TimelimitExceeded(_) => Status::RequestTimeout,
|
|
HttpResult::TooManyRequests(_) => Status::TooManyRequests,
|
|
HttpResult::UnlockingFailed(_) => Status::InternalServerError,
|
|
};
|
|
(status, self)
|
|
}
|
|
}
|
|
|
|
#[get("/", rank = 1)]
|
|
async fn index() -> String {
|
|
"Auto-Decrypt API! DEV!".to_string()
|
|
}
|
|
|
|
|
|
#[catch(default)]
|
|
fn default(_status: Status, req: &Request) -> String {
|
|
format!("{:?}", req)
|
|
}
|
|
|
|
|
|
#[rocket::main]
|
|
pub(crate) async fn start_api() -> () {
|
|
let rocket_config = Config{
|
|
port: CONFIG.wait().port,
|
|
..Default::default()};
|
|
|
|
let _ = rocket::build()
|
|
.manage(DbConn::establish_connection(&CONFIG.wait().db_file))
|
|
.attach(Template::fairing())
|
|
.mount("/", routes![index])
|
|
.mount("/request", routes![request::request_handler])
|
|
.mount("/consent", routes![consent_template, consent_post])
|
|
.register("/", catchers![default])
|
|
.configure(rocket_config)
|
|
.launch()
|
|
.await;
|
|
}
|