Commit a0b01b0c authored by Chunchi Che's avatar Chunchi Che

add strings service

parent 9986628a
Pipeline #18768 passed with stages
in 9 minutes and 39 seconds
use std::{net::SocketAddr, str::FromStr};
use std::{collections::HashMap, net::SocketAddr, str::FromStr};
use warp::Filter;
mod infra;
......@@ -11,6 +11,8 @@ const SERVE_ADDR: &str = "127.0.0.1:3030";
async fn main() -> anyhow::Result<()> {
pretty_env_logger::try_init()?;
let strings_manager = HashMap::new();
// TODO: use CORS correctly
let cors = warp::cors()
.allow_any_origin()
......@@ -27,12 +29,16 @@ async fn main() -> anyhow::Result<()> {
.map(service::cards_service)
.with(cors.clone());
let strings = warp::path!("strings" / String)
.map(service::strings_service(strings_manager))
.with(cors.clone());
// TODO: 模块化
let images = warp::path("images")
.and(warp::fs::dir("./images/"))
.with(cors);
warp::serve(deck.or(cards).or(images))
warp::serve(deck.or(cards).or(images).or(strings))
.run(SocketAddr::from_str(SERVE_ADDR)?)
.await;
......
mod cards;
mod deck;
mod strings;
pub use cards::service as cards_service;
pub use deck::service as deck_service;
pub use strings::service_maker as strings_service;
use std::collections::HashMap;
// TODO: `strings_manager`应该做好持久化存储
pub fn service_maker(
strings_manager: HashMap<String, HashMap<i64, String>>,
) -> impl Fn(String) -> String + Clone {
move |param| {
// TODO:应该做好错误处理
let (r#type, code) = param.split_once('_').unwrap_or_default();
let code = code.parse::<i64>().unwrap_or_default();
strings_manager
.get(r#type)
.map_or(Some("".to_string()), |hash| hash.get(&code).cloned())
.unwrap_or_default()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment