2021-03-30 18:19:09 +03:00
|
|
|
/*
|
2022-01-08 19:46:05 +03:00
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
2021-08-12 14:43:17 +03:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-05-04 20:57:58 +03:00
|
|
|
use std::borrow::Cow;
|
2021-03-30 18:19:09 +03:00
|
|
|
|
2022-05-07 13:40:14 +03:00
|
|
|
use actix_web::body::BoxBody;
|
2021-05-04 20:57:58 +03:00
|
|
|
use actix_web::{http::header, web, HttpResponse, Responder};
|
2021-03-30 18:19:09 +03:00
|
|
|
use mime_guess::from_path;
|
|
|
|
use rust_embed::RustEmbed;
|
|
|
|
|
2021-05-04 20:57:58 +03:00
|
|
|
use crate::CACHE_AGE;
|
2021-03-30 18:19:09 +03:00
|
|
|
|
2021-05-02 16:06:39 +03:00
|
|
|
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 {
|
2021-05-08 12:42:25 +03:00
|
|
|
home: "/docs/",
|
2021-10-08 13:06:42 +03:00
|
|
|
spec: "/docs/openapi.yaml",
|
2021-05-02 16:06:39 +03:00
|
|
|
assets: "/docs/{_:.*}",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2021-08-12 14:43:17 +03:00
|
|
|
cfg.service(index).service(spec).service(dist);
|
2021-05-02 16:06:39 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:19:09 +03:00
|
|
|
#[derive(RustEmbed)]
|
2021-05-29 09:52:31 +03:00
|
|
|
#[folder = "static/openapi/"]
|
2021-03-30 18:19:09 +03:00
|
|
|
struct Asset;
|
|
|
|
|
|
|
|
pub fn handle_embedded_file(path: &str) -> HttpResponse {
|
|
|
|
match Asset::get(path) {
|
|
|
|
Some(content) => {
|
2022-05-07 13:40:14 +03:00
|
|
|
let body: BoxBody = match content.data {
|
|
|
|
Cow::Borrowed(bytes) => BoxBody::new(bytes),
|
|
|
|
Cow::Owned(bytes) => BoxBody::new(bytes),
|
2021-03-30 18:19:09 +03:00
|
|
|
};
|
2022-05-07 13:40:14 +03:00
|
|
|
|
2021-03-30 18:19:09 +03:00
|
|
|
HttpResponse::Ok()
|
2021-06-30 17:44:15 +03:00
|
|
|
.insert_header(header::CacheControl(vec![
|
2022-05-07 13:40:14 +03:00
|
|
|
header::CacheDirective::Public,
|
|
|
|
header::CacheDirective::Extension("immutable".into(), None),
|
2021-06-30 17:44:15 +03:00
|
|
|
header::CacheDirective::MaxAge(CACHE_AGE),
|
|
|
|
]))
|
2021-03-30 18:19:09 +03:00
|
|
|
.content_type(from_path(path).first_or_octet_stream().as_ref())
|
|
|
|
.body(body)
|
|
|
|
}
|
|
|
|
None => HttpResponse::NotFound().body("404 Not Found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:43:17 +03:00
|
|
|
#[my_codegen::get(path = "DOCS.assets")]
|
2021-03-30 18:19:09 +03:00
|
|
|
async fn dist(path: web::Path<String>) -> impl Responder {
|
2021-06-30 17:43:12 +03:00
|
|
|
handle_embedded_file(&path)
|
2021-03-30 18:19:09 +03:00
|
|
|
}
|
2021-10-08 13:25:53 +03:00
|
|
|
const OPEN_API_SPEC: &str = include_str!("../docs/openapi/dist/openapi.yaml");
|
2021-03-30 18:19:09 +03:00
|
|
|
|
2021-08-12 14:43:17 +03:00
|
|
|
#[my_codegen::get(path = "DOCS.spec")]
|
2021-04-01 12:53:36 +03:00
|
|
|
async fn spec() -> HttpResponse {
|
|
|
|
HttpResponse::Ok()
|
2021-10-08 13:06:42 +03:00
|
|
|
.content_type("text/yaml")
|
|
|
|
.body(OPEN_API_SPEC)
|
2021-04-01 12:53:36 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 14:43:17 +03:00
|
|
|
#[my_codegen::get(path = "&DOCS.home[0..DOCS.home.len() -1]")]
|
2021-03-30 18:19:09 +03:00
|
|
|
async fn index() -> HttpResponse {
|
|
|
|
handle_embedded_file("index.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use actix_web::http::StatusCode;
|
|
|
|
use actix_web::test;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2021-05-04 12:48:07 +03:00
|
|
|
async fn docs_works() {
|
|
|
|
const FILE: &str = "favicon-32x32.png";
|
2021-03-30 18:19:09 +03:00
|
|
|
|
2021-07-11 19:16:50 +03:00
|
|
|
let app = test::init_service(
|
2021-05-08 12:42:25 +03:00
|
|
|
App::new()
|
|
|
|
.wrap(actix_middleware::NormalizePath::new(
|
2021-06-30 17:43:12 +03:00
|
|
|
actix_middleware::TrailingSlash::Trim,
|
2021-05-08 12:42:25 +03:00
|
|
|
))
|
|
|
|
.configure(services),
|
|
|
|
)
|
|
|
|
.await;
|
2021-03-30 18:19:09 +03:00
|
|
|
|
2021-05-04 12:48:07 +03:00
|
|
|
let resp = test::call_service(
|
2021-07-11 19:16:50 +03:00
|
|
|
&app,
|
2021-05-04 12:48:07 +03:00
|
|
|
test::TestRequest::get().uri(DOCS.home).to_request(),
|
|
|
|
)
|
|
|
|
.await;
|
2021-03-30 18:19:09 +03:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
2021-05-04 12:48:07 +03:00
|
|
|
let resp = test::call_service(
|
2021-07-11 19:16:50 +03:00
|
|
|
&app,
|
2021-05-04 12:48:07 +03:00
|
|
|
test::TestRequest::get().uri(DOCS.spec).to_request(),
|
|
|
|
)
|
|
|
|
.await;
|
2021-03-30 18:19:09 +03:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2021-04-01 12:53:36 +03:00
|
|
|
|
2021-05-04 12:48:07 +03:00
|
|
|
let uri = format!("{}{}", DOCS.home, FILE);
|
|
|
|
|
2021-07-12 18:52:26 +03:00
|
|
|
let resp =
|
|
|
|
test::call_service(&app, test::TestRequest::get().uri(&uri).to_request())
|
|
|
|
.await;
|
2021-04-01 12:53:36 +03:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2021-03-30 18:19:09 +03:00
|
|
|
}
|
|
|
|
}
|