docs use const routes

This commit is contained in:
realaravinth 2021-05-02 18:36:39 +05:30
parent 4b6e3496cd
commit 0531a26274
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
5 changed files with 45 additions and 12 deletions

View file

@ -1,4 +1,5 @@
debug = true
source_code = "https://github.com/mCaptcha/guard"
[database]
# This section deals with the database location and how to access it

View file

@ -16,12 +16,40 @@
*/
use actix_web::body::Body;
use actix_web::{get, web, HttpResponse, Responder};
use actix_web::{web, HttpResponse, Responder};
use mime_guess::from_path;
use rust_embed::RustEmbed;
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)]
#[folder = "docs/"]
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 {
handle_embedded_file(&path.0)
}
#[get("/docs/openapi.json")]
async fn spec() -> HttpResponse {
HttpResponse::Ok()
.content_type("appilcation/json")
.body(&*crate::OPEN_API_DOC)
}
#[get("/docs")]
async fn index() -> HttpResponse {
handle_embedded_file("index.html")
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(spec);
cfg.service(index);
cfg.service(dist);
}
#[cfg(test)]
mod tests {
use actix_web::http::StatusCode;

View file

@ -40,6 +40,7 @@ mod middleware;
pub use api::v1::ROUTES as V1_API_ROUTES;
pub use data::Data;
pub use docs::DOCS;
pub use pages::routes::ROUTES as PAGES;
pub use settings::Settings;
use static_assets::FileMap;

View file

@ -98,6 +98,7 @@ pub struct Settings {
pub database: Database,
pub server: Server,
pub pow: Captcha,
pub source_code: String,
}
#[cfg(not(tarpaulin_include))]
@ -116,6 +117,8 @@ impl Settings {
// TODO change PLACEHOLDER to app name
s.merge(Environment::with_prefix("GUARD"))?;
check_url(&s);
match env::var("PORT") {
Ok(val) => {
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))]
fn set_from_database_url(s: &mut Config, database_conf: &DatabaseBuilder) {
s.set("database.username", database_conf.username.clone())

View file

@ -65,7 +65,7 @@
</a>
</li>
<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="" />
<div class="secondary-menu__item-name">
API Docs
@ -73,7 +73,7 @@
</a>
</li>
<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"
src="<.= crate::FILES.get("./static-assets/img/svg/github.svg").unwrap() .>" alt="GitHub Icon" />
<div class="secondary-menu__item-name">