use actix_web::{get, App, HttpServer, Responder, HttpResponse}; use serde::{Serialize, Deserialize}; #[derive(Debug, Deserialize, Serialize)] struct GenerateResponse { length: i32, token: String } #[get("/generate/{length}")] async fn generate(path: actix_web::web::Path) -> impl Responder { let length = path.into_inner(); let token = tokengen_core::generate(Some(length)); let response = GenerateResponse { length, token }; let x = serde_json::to_string(&response).unwrap_or(String::from("{}")); HttpResponse::Ok().content_type("application/json").body(x) } #[get("/generate")] async fn generate_nopath() -> impl Responder { let token = tokengen_core::generate(None); let response = GenerateResponse { length: 32, token }; let x = serde_json::to_string(&response).unwrap_or(String::from("{}")); HttpResponse::Ok().content_type("application/json").body(x) } #[actix_web::main] async fn main() -> Result<(), std::io::Error> { let mut addr = String::from("127.0.0.1"); let mut port = 8080; let addr_env = std::env::var("TOKENGEN_ADDR"); let port_env = std::env::var("TOKENGEN_PORT"); if addr_env.is_ok() { addr = addr_env.unwrap(); } if port_env.is_ok() { port = port_env.unwrap().parse::().unwrap() } println!("Starting service on {}:{}", addr, port); HttpServer::new(|| { App::new() .service(generate) .service(generate_nopath) }) .bind((addr, port))? .run() .await }