diff --git a/backend/src/api/routes/random.rs b/backend/src/api/routes/random.rs index 24f6900..72f1b1e 100644 --- a/backend/src/api/routes/random.rs +++ b/backend/src/api/routes/random.rs @@ -1,7 +1,8 @@ use rand::Rng; use serde_json::Value; use std::{fs, string::String}; -use tide::{prelude::*, Body, Request, Response, Result}; +use tide::{prelude::*, Request, Result}; +use crate::utils::{build_response, ResponseOptions}; #[derive(Deserialize)] #[serde(default)] @@ -31,27 +32,6 @@ fn get_random_seal(seal_type: String) -> std::result::Result -where - T: Into, -{ - status: u16, - content_type: &'static str, - contents: T, -} - -fn build_response(opts: ResponseOptions) -> Response -where - T: Into, -{ - let mut resp = Response::new(opts.status); - - resp.append_header("Content-Type", opts.content_type); - resp.set_body(opts.contents); - - return resp; -} - pub async fn handler(req: Request<()>) -> Result { let value_type = req.query::().unwrap().r#type; diff --git a/backend/src/lib.rs b/backend/src/lib.rs index c23e666..a6de26f 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -4,6 +4,7 @@ use framework::{setup_server, ServerOptions, Server}; mod api; +pub mod utils; pub async fn init() -> tide::Result { let server = setup_server(ServerOptions { diff --git a/backend/src/utils.rs b/backend/src/utils.rs new file mode 100644 index 0000000..b6b9b05 --- /dev/null +++ b/backend/src/utils.rs @@ -0,0 +1,22 @@ +use tide::{Response, Body}; + +pub struct ResponseOptions +where + T: Into, +{ + pub status: u16, + pub content_type: &'static str, + pub contents: T, +} + +pub fn build_response(opts: ResponseOptions) -> Response +where + T: Into, +{ + let mut resp = Response::new(opts.status); + + resp.append_header("Content-Type", opts.content_type); + resp.set_body(opts.contents); + + return resp; +} \ No newline at end of file