diff --git a/.travis.yml b/.travis.yml index 1b2737f..f85b33b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,14 @@ script: - cargo fmt --all -- --check - cargo test --all +before_deploy: + - cd orgize-demos/ + +deploy: + provider: heroku + api_key: $HEROKU_API_KEY + app: orgize + notifications: email: on_failure: change diff --git a/Cargo.toml b/Cargo.toml index 06faa09..bc4d8c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ "orgize", + "orgize-demos", ] diff --git a/orgize-demos/Cargo.toml b/orgize-demos/Cargo.toml new file mode 100644 index 0000000..1b2306f --- /dev/null +++ b/orgize-demos/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "orgize-demos" +version = "0.1.0" +authors = ["PoiScript "] +edition = "2018" +publish = false + +[dependencies] +actix-web = { version = "1.0.7", default-features = false } +orgize = { path = "../orgize", git = "https://github.com/PoiScript/orgize" } +serde = { version = "1.0.100", features = ["derive"] } +serde_json = "1.0.40" diff --git a/orgize-demos/Procfile b/orgize-demos/Procfile new file mode 100644 index 0000000..b5e2e72 --- /dev/null +++ b/orgize-demos/Procfile @@ -0,0 +1 @@ +web: ./target/release/orgize-demos diff --git a/orgize-demos/src/main.rs b/orgize-demos/src/main.rs new file mode 100644 index 0000000..3f24937 --- /dev/null +++ b/orgize-demos/src/main.rs @@ -0,0 +1,63 @@ +use actix_web::{get, post, web, App, HttpResponse, HttpServer}; +use orgize::Org; +use serde::Deserialize; +use std::env; +use std::io::Result; + +#[get("/")] +fn index() -> HttpResponse { + HttpResponse::Ok().content_type("text/html").body( + "

Orgize demos

\ +
\ +

Input content:

\ +
\ +

Output format:

\ + Json
\ + HTML
\ + Org
\ +

\ +
", + ) +} + +#[derive(Deserialize)] +struct FormData { + format: String, + content: String, +} + +#[post("/export")] +fn export(form: web::Form) -> Result { + let org = Org::parse(&form.content); + match &*form.format { + "json" => Ok(HttpResponse::Ok() + .content_type("application/json") + .body(serde_json::to_string(&org)?)), + "html" => { + let mut writer = Vec::new(); + org.html(&mut writer)?; + Ok(HttpResponse::Ok() + .content_type("text/html") + .body(String::from_utf8(writer).unwrap())) + } + "org" => { + let mut writer = Vec::new(); + org.org(&mut writer)?; + Ok(HttpResponse::Ok() + .content_type("text/plain") + .body(String::from_utf8(writer).unwrap())) + } + _ => Ok(HttpResponse::BadRequest().body("Unsupported format".to_string())), + } +} + +fn main() -> Result<()> { + let port = env::var("PORT") + .unwrap_or_else(|_| "3000".into()) + .parse() + .expect("PORT must be a number"); + + HttpServer::new(|| App::new().service(index).service(export)) + .bind(("0.0.0.0", port))? + .run() +}