pakkau-webapp/src/main.rs
2024-04-08 01:12:57 +08:00

68 lines
1.4 KiB
Rust

use std::process::Command;
use serde_json::json;
use rocket::response::content::RawHtml;
use rocket::response::Redirect;
use rocket::Request;
#[macro_use] extern crate rocket;
#[get("/index.htm")]
fn index() -> RawHtml<&'static str> {
RawHtml(include_str!("assets/index.htm"))
}
#[get("/index.html")]
fn indexHTML() -> Redirect {
Redirect::to(uri!("./index.htm"))
}
#[get("/")]
fn defau() -> Redirect {
Redirect::to(uri!("./index.htm"))
}
#[catch(404)]
fn not_found(req: &Request) -> RawHtml<&'static str> {
RawHtml(include_str!("assets/404.htm"))
}
#[post("/query", data = "<text>")]
fn query(text: String) -> String {
let pre_import = Command::new("source")
.arg("pyvenv/bin/activate");
let hanji_query = Command::new("python3")
.arg("3rd/pakkau.py")
.arg("--form")
.arg("tl")
.arg(text)
.output().expect("failed to query");
println!("{}", format!("{:?}", hanji_query));
let hanji = format!("{}", String::from_utf8_lossy(&hanji_query.stdout));
let hanji_removed_last_newline = &hanji[..(hanji.len())];
let result_JSON = json!({
"result": hanji_removed_last_newline,
});
return result_JSON.to_string();
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, query, indexHTML, defau])
.configure(rocket::Config::figment().merge(("port", 8001)))
.register("/", catchers![not_found])
}