chore: orgize-demos package

This commit is contained in:
PoiScript 2019-09-13 21:25:35 +08:00
parent 77eca42760
commit 8e1778a38a
5 changed files with 85 additions and 0 deletions

View file

@ -11,6 +11,14 @@ script:
- cargo fmt --all -- --check - cargo fmt --all -- --check
- cargo test --all - cargo test --all
before_deploy:
- cd orgize-demos/
deploy:
provider: heroku
api_key: $HEROKU_API_KEY
app: orgize
notifications: notifications:
email: email:
on_failure: change on_failure: change

View file

@ -1,4 +1,5 @@
[workspace] [workspace]
members = [ members = [
"orgize", "orgize",
"orgize-demos",
] ]

12
orgize-demos/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "orgize-demos"
version = "0.1.0"
authors = ["PoiScript <poiscript@gmail.com>"]
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"

1
orgize-demos/Procfile Normal file
View file

@ -0,0 +1 @@
web: ./target/release/orgize-demos

63
orgize-demos/src/main.rs Normal file
View file

@ -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(
"<h3><a href=\"https://github.com/PoiScript/orgize\">Orgize</a> demos</h3>\
<form action=\"/export\" method=\"post\">\
<p>Input content:</p>\
<div><textarea name=\"content\" rows=\"10\" cols=\"100\">* DONE Title :tag:</textarea></div>\
<p>Output format:</p>\
<input type=\"radio\" name=\"format\" value=\"json\" checked> Json<br>\
<input type=\"radio\" name=\"format\" value=\"html\"> HTML<br>\
<input type=\"radio\" name=\"format\" value=\"org\"> Org<br>\
<p><input type=\"submit\" value=\"Submit\"></p>\
</form>",
)
}
#[derive(Deserialize)]
struct FormData {
format: String,
content: String,
}
#[post("/export")]
fn export(form: web::Form<FormData>) -> Result<HttpResponse> {
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()
}