mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-06-06 14:16:39 +00:00
docs use const routes
This commit is contained in:
parent
4b6e3496cd
commit
0531a26274
@ -1,4 +1,5 @@
|
|||||||
debug = true
|
debug = true
|
||||||
|
source_code = "https://github.com/mCaptcha/guard"
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
# This section deals with the database location and how to access it
|
# This section deals with the database location and how to access it
|
||||||
|
39
src/docs.rs
39
src/docs.rs
@ -16,12 +16,40 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{web, HttpResponse, Responder};
|
||||||
use mime_guess::from_path;
|
use mime_guess::from_path;
|
||||||
use rust_embed::RustEmbed;
|
use rust_embed::RustEmbed;
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
pub const DOCS: routes::Docs = routes::Docs::new();
|
||||||
|
|
||||||
|
pub mod routes {
|
||||||
|
pub struct Docs {
|
||||||
|
pub home: &'static str,
|
||||||
|
pub spec: &'static str,
|
||||||
|
pub assets: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Docs {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Docs {
|
||||||
|
home: "/docs",
|
||||||
|
spec: "/docs/openapi.json",
|
||||||
|
assets: "/docs/{_:.*}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||||
|
use crate::define_resource;
|
||||||
|
|
||||||
|
define_resource!(cfg, DOCS.home, Methods::Get, index);
|
||||||
|
define_resource!(cfg, DOCS.spec, Methods::Get, spec);
|
||||||
|
define_resource!(cfg, DOCS.assets, Methods::Get, dist);
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(RustEmbed)]
|
#[derive(RustEmbed)]
|
||||||
#[folder = "docs/"]
|
#[folder = "docs/"]
|
||||||
struct Asset;
|
struct Asset;
|
||||||
@ -41,29 +69,20 @@ pub fn handle_embedded_file(path: &str) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/docs/{_:.*}")]
|
|
||||||
async fn dist(path: web::Path<String>) -> impl Responder {
|
async fn dist(path: web::Path<String>) -> impl Responder {
|
||||||
handle_embedded_file(&path.0)
|
handle_embedded_file(&path.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/docs/openapi.json")]
|
|
||||||
async fn spec() -> HttpResponse {
|
async fn spec() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("appilcation/json")
|
.content_type("appilcation/json")
|
||||||
.body(&*crate::OPEN_API_DOC)
|
.body(&*crate::OPEN_API_DOC)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/docs")]
|
|
||||||
async fn index() -> HttpResponse {
|
async fn index() -> HttpResponse {
|
||||||
handle_embedded_file("index.html")
|
handle_embedded_file("index.html")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
||||||
cfg.service(spec);
|
|
||||||
cfg.service(index);
|
|
||||||
cfg.service(dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
|
@ -40,6 +40,7 @@ mod middleware;
|
|||||||
|
|
||||||
pub use api::v1::ROUTES as V1_API_ROUTES;
|
pub use api::v1::ROUTES as V1_API_ROUTES;
|
||||||
pub use data::Data;
|
pub use data::Data;
|
||||||
|
pub use docs::DOCS;
|
||||||
pub use pages::routes::ROUTES as PAGES;
|
pub use pages::routes::ROUTES as PAGES;
|
||||||
pub use settings::Settings;
|
pub use settings::Settings;
|
||||||
use static_assets::FileMap;
|
use static_assets::FileMap;
|
||||||
|
@ -98,6 +98,7 @@ pub struct Settings {
|
|||||||
pub database: Database,
|
pub database: Database,
|
||||||
pub server: Server,
|
pub server: Server,
|
||||||
pub pow: Captcha,
|
pub pow: Captcha,
|
||||||
|
pub source_code: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
@ -116,6 +117,8 @@ impl Settings {
|
|||||||
// TODO change PLACEHOLDER to app name
|
// TODO change PLACEHOLDER to app name
|
||||||
s.merge(Environment::with_prefix("GUARD"))?;
|
s.merge(Environment::with_prefix("GUARD"))?;
|
||||||
|
|
||||||
|
check_url(&s);
|
||||||
|
|
||||||
match env::var("PORT") {
|
match env::var("PORT") {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
s.set("server.port", val).unwrap();
|
s.set("server.port", val).unwrap();
|
||||||
@ -138,6 +141,15 @@ impl Settings {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(tarpaulin_include))]
|
||||||
|
fn check_url(s: &Config) {
|
||||||
|
let url = s
|
||||||
|
.get::<String>("source_code")
|
||||||
|
.expect("Couldn't access source_code");
|
||||||
|
|
||||||
|
Url::parse(&url).expect("Please enter a URL for source_code in settings");
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
fn set_from_database_url(s: &mut Config, database_conf: &DatabaseBuilder) {
|
fn set_from_database_url(s: &mut Config, database_conf: &DatabaseBuilder) {
|
||||||
s.set("database.username", database_conf.username.clone())
|
s.set("database.username", database_conf.username.clone())
|
||||||
|
@ -65,7 +65,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="secondary-menu__item">
|
<li class="secondary-menu__item">
|
||||||
<a class="secondary-menu__item-link" href="/docs/">
|
<a class="secondary-menu__item-link" href="<.= crate::DOCS.home .>">
|
||||||
<img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/file-text.svg").unwrap() .>" alt="" />
|
<img class="secondary-menu__icon" src="<.= crate::FILES.get("./static-assets/img/svg/file-text.svg").unwrap() .>" alt="" />
|
||||||
<div class="secondary-menu__item-name">
|
<div class="secondary-menu__item-name">
|
||||||
API Docs
|
API Docs
|
||||||
@ -73,7 +73,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="secondary-menu__item">
|
<li class="secondary-menu__item">
|
||||||
<a class="secondary-menu__item-link" href="https://github.com/mCaptcha/guard">
|
<a class="secondary-menu__item-link" href="<.= crate::SETTINGS.source_code .>">
|
||||||
<img class="secondary-menu__icon"
|
<img class="secondary-menu__icon"
|
||||||
src="<.= crate::FILES.get("./static-assets/img/svg/github.svg").unwrap() .>" alt="GitHub Icon" />
|
src="<.= crate::FILES.get("./static-assets/img/svg/github.svg").unwrap() .>" alt="GitHub Icon" />
|
||||||
<div class="secondary-menu__item-name">
|
<div class="secondary-menu__item-name">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user