2017-09-19 03:32:15 -07:00
|
|
|
use rocket;
|
2017-10-29 03:16:08 -07:00
|
|
|
use rocket::State;
|
2017-10-12 02:13:55 -07:00
|
|
|
use rocket::response::Content;
|
|
|
|
use rocket::http::ContentType;
|
2019-01-01 02:22:12 -08:00
|
|
|
use rocket_contrib::json::Json;
|
2019-03-12 02:39:25 -07:00
|
|
|
use crate::language::{ProgrammingLanguageInterface, EvalOptions};
|
|
|
|
use crate::WEBFILES;
|
|
|
|
use crate::PLIGenerator;
|
2017-09-19 03:32:15 -07:00
|
|
|
|
2017-09-19 01:54:54 -07:00
|
|
|
#[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)
|
2017-09-19 01:54:54 -07:00
|
|
|
}
|
|
|
|
|
2017-09-20 23:21:45 -07:00
|
|
|
#[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-09-20 23:21:45 -07:00
|
|
|
}
|
|
|
|
|
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 12:27:24 -07:00
|
|
|
fn interpreter_input(input: Json<Input>, generators: State<Vec<PLIGenerator>>) -> Json<Output> {
|
|
|
|
let schala_gen = generators.get(0).unwrap();
|
|
|
|
let mut schala: Box<ProgrammingLanguageInterface> = schala_gen();
|
2018-04-30 14:34:15 -07:00
|
|
|
let code_output = schala.execute_pipeline(&input.source, &EvalOptions::default());
|
|
|
|
Json(Output { text: code_output.to_repl() })
|
2017-09-19 20:29:08 -07:00
|
|
|
}
|
|
|
|
|
2017-10-29 12:27:24 -07:00
|
|
|
pub fn web_main(language_generators: Vec<PLIGenerator>) {
|
|
|
|
rocket::ignite().manage(language_generators).mount("/", routes![index, js_bundle, interpreter_input]).launch();
|
2017-09-19 01:54:54 -07:00
|
|
|
}
|