Skip to content

Commit

Permalink
Add endpoint that serves the API docs (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
flamion authored Sep 18, 2022
1 parent 25a614d commit 585cb4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/file_serving/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use actix_web::{get, HttpRequest, HttpResponse, Responder, web};
use tracing::{debug, warn};
use crate::CONFIG;

const INDEX_HTML: &str = include_str!("../../website/index.html");
const MAIN_JS: &str = include_str!("../../website/main.js");
const STYLE_CSS: &str = include_str!("../../website/style.css");
const ROBOTO_MONO_TTF: &[u8] = include_bytes!("../../website/roboto_mono.ttf");

// The function is async because the actix-web macro requires it.
#[allow(clippy::unused_async)]
Expand All @@ -26,6 +22,15 @@ pub async fn index(req: HttpRequest) -> Result<impl Responder, Box<dyn std::erro
)
}

#[get("/documentation")]
pub async fn api_docs() -> impl Responder {
const DOCUMENTATION_YAML: &str = include_str!("../../meta/docs/api.yaml");

HttpResponse::Ok()
.content_type("application/x-yaml")
.body(DOCUMENTATION_YAML)
}

// The function is async because the actix-web macro requires it.
#[allow(clippy::unused_async)]
#[get("/assets/{asset:.*}")]
Expand Down Expand Up @@ -55,13 +60,22 @@ pub async fn serve_file(asset: web::Path<String>, req: HttpRequest) -> Result<im

/// Returns a Tuple of Mime Type (as &str) and file content (as &[u8]).
fn get_embedded_file(file: &str) -> Option<(&'static str, &'static [u8])> {
const INDEX_HTML: &str = include_str!("../../website/index.html");
const MAIN_JS: &str = include_str!("../../website/main.js");
const STYLE_CSS: &str = include_str!("../../website/style.css");
const ROBOTO_MONO_TTF: &[u8] = include_bytes!("../../website/roboto_mono.ttf");

debug!("Getting embedded file: {file}");

match file {
"index.html" => { Some(("text/html", INDEX_HTML.as_bytes())) }
"main.js" => { Some(("text/javascript", MAIN_JS.as_bytes())) }
"style.css" => { Some(("text/css", STYLE_CSS.as_bytes())) }
"roboto_mono.ttf" => { Some(("font/ttf", ROBOTO_MONO_TTF)) }
_ => { warn!("Got request for {file} but couldn't find embedded asset."); None }
_ => {
warn!("Got request for {file} but couldn't find embedded asset.");
None
}
}
}

3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing_subscriber::EnvFilter;
use crate::config::Config;
use crate::config::SAMPLE_CONFIG;
use crate::error::ShortyError;
use crate::file_serving::endpoints::{index, serve_file};
use crate::file_serving::endpoints::{api_docs, index, serve_file};
use crate::link::{LinkConfig, LinkStore};
use crate::util::{ensure_http_prefix, uri_to_url};

Expand Down Expand Up @@ -213,6 +213,7 @@ async fn main() -> Result<(), ShortyError> {
.service(get_shortened)
.service(create_shortened_custom)
.service(create_shortened)
.service(api_docs)
})
.bind((CONFIG.listen_url.as_str(), CONFIG.port))
.expect("Failed to bind port or listen address.")
Expand Down

0 comments on commit 585cb4a

Please sign in to comment.