Commit committed....

This commit is contained in:
Erica Marigold 2023-06-01 19:12:17 +05:30
parent 0c4dff1695
commit 90679afadb
No known key found for this signature in database
GPG key ID: DA2C44572707FB1C
3 changed files with 25 additions and 22 deletions

View file

@ -1,7 +1,8 @@
use rand::Rng; use rand::Rng;
use serde_json::Value; use serde_json::Value;
use std::{fs, string::String}; 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)] #[derive(Deserialize)]
#[serde(default)] #[serde(default)]
@ -31,27 +32,6 @@ fn get_random_seal(seal_type: String) -> std::result::Result<String, std::io::Er
Ok((&seals_vec[rand::thread_rng().gen_range(0..seals_vec.len())]).clone()) Ok((&seals_vec[rand::thread_rng().gen_range(0..seals_vec.len())]).clone())
} }
struct ResponseOptions<T>
where
T: Into<Body>,
{
status: u16,
content_type: &'static str,
contents: T,
}
fn build_response<T>(opts: ResponseOptions<T>) -> Response
where
T: Into<Body>,
{
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 { pub async fn handler(req: Request<()>) -> Result {
let value_type = req.query::<Params>().unwrap().r#type; let value_type = req.query::<Params>().unwrap().r#type;

View file

@ -4,6 +4,7 @@
use framework::{setup_server, ServerOptions, Server}; use framework::{setup_server, ServerOptions, Server};
mod api; mod api;
pub mod utils;
pub async fn init() -> tide::Result<Server> { pub async fn init() -> tide::Result<Server> {
let server = setup_server(ServerOptions { let server = setup_server(ServerOptions {

22
backend/src/utils.rs Normal file
View file

@ -0,0 +1,22 @@
use tide::{Response, Body};
pub struct ResponseOptions<T>
where
T: Into<Body>,
{
pub status: u16,
pub content_type: &'static str,
pub contents: T,
}
pub fn build_response<T>(opts: ResponseOptions<T>) -> Response
where
T: Into<Body>,
{
let mut resp = Response::new(opts.status);
resp.append_header("Content-Type", opts.content_type);
resp.set_body(opts.contents);
return resp;
}