schala/src/webapp.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

use rocket;
use rocket::State;
2017-10-12 02:13:55 -07:00
use rocket::response::Content;
use rocket::http::ContentType;
2017-09-19 20:29:08 -07:00
use rocket_contrib::Json;
2017-10-02 00:15:39 -07:00
use language::{ProgrammingLanguageInterface, EvalOptions};
2017-10-12 02:13:55 -07:00
use WEBFILES;
use ::PLIGenerator;
#[get("/")]
2017-10-12 02:13:55 -07:00
fn index() -> Content<String> {
let path = "static/index.html";
let html_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
Content(ContentType::HTML, html_contents)
}
#[get("/bundle.js")]
2017-10-12 02:13:55 -07:00
fn js_bundle() -> Content<String> {
let path = "static/bundle.js";
let js_contents = String::from_utf8(WEBFILES.get(path).unwrap().into_owned()).unwrap();
Content(ContentType::JavaScript, js_contents)
}
2017-10-01 23:25:36 -07:00
#[derive(Debug, Serialize, Deserialize)]
2017-09-19 20:29:08 -07:00
struct Input {
source: String,
}
#[derive(Serialize, Deserialize)]
struct Output {
text: String,
}
#[post("/input", format = "application/json", data = "<input>")]
2017-10-29 04:09:10 -07:00
fn interpreter_input(input: Json<Input>, schala_gen: State<Vec<PLIGenerator>>) -> Json<Output> {
let mut schala: Box<ProgrammingLanguageInterface> = (schala_gen.get(0).unwrap())();
2017-10-02 00:15:39 -07:00
let code_output = schala.evaluate_in_repl(&input.source, &EvalOptions::default());
Json(Output { text: code_output.to_string() })
2017-09-19 20:29:08 -07:00
}
2017-10-29 04:09:10 -07:00
pub fn web_main(language_generators: Vec<Box<ProgrammingLanguageInterface>>, func: Vec<PLIGenerator>) {
2017-10-29 04:04:54 -07:00
rocket::ignite().manage(func).mount("/", routes![index, js_bundle, interpreter_input]).launch();
}