Skip to content

Commit

Permalink
Rustc bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hashedone committed Sep 21, 2017
1 parent 9c8a2ee commit 022e181
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ version = "0.1.0"
authors = ["Bartłomiej Kuras <hash.deb@gmail.com>"]

[dependencies]
rocket = "0.3.2"
rocket_codegen = "0.3.2"
clap = "2.26.2"
maud = { version = "0.17.1", features = ["rocket"] }
3 changes: 3 additions & 0 deletions Rocket.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[development]
address = "0.0.0.0"
port = 8000
73 changes: 72 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]
#![feature(proc_macro)]
#![feature(conservative_impl_trait)]

extern crate rocket;
extern crate clap;
extern crate maud;

mod view;

use view::{View, ViewContext};
use rocket::State;
use rocket::response::Responder;

#[get("/")]
fn index(ctx: State<ViewContext>) -> impl Responder {
view::Frame{}.render(&ctx)
}

fn load_config() -> rocket::Config {
let cmdargs = clap::App::new("RPG Web Tool server")
.version("0.0.1")
.author("Bartłomiej `hashed` Kuras <bartlomiej.kuras@o2.pl>")
.arg(clap::Arg::with_name("env")
.short("e")
.long("env")
.value_name("ENVIRONMENT")
.help("Rocket environment to load")
.takes_value(true)
.possible_values(&["dev", "staging", "prod"])
.default_value("prod"))
.arg(clap::Arg::with_name("host")
.short("h")
.long("host")
.value_name("HOST")
.help("Address to listen on")
.takes_value(true))
.arg(clap::Arg::with_name("port")
.short("p")
.long("port")
.value_name("PORT")
.help("Port to listen on")
.takes_value(true))
.get_matches();

let env = match cmdargs.value_of("env").unwrap_or("prod") {
"dev" => rocket::config::Environment::Development,
"staging" => rocket::config::Environment::Staging,
"prod" => rocket::config::Environment::Production,
&_ => unreachable!()
};

let mut config = rocket::Config::build(env).finalize().unwrap();

cmdargs.value_of("host")
.map(|h| String::from(h))
.or_else(|| std::env::var_os("HOST").map(|h| h.into_string().unwrap()))
.map(|h| config.address = h);

cmdargs.value_of("port")
.map(|p| String::from(p))
.or_else(|| std::env::var_os("PORT").map(|p| p.into_string().unwrap()))
.map(|p| config.port = p.parse().unwrap());

config
}

fn main() {
println!("Hello, world!");
rocket::custom(load_config(), true)
.attach(view::ViewContext::fairing())
.mount("/", routes![index])
.launch();
}
24 changes: 24 additions & 0 deletions src/view/frame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use maud::{html, Markup, PreEscaped};
use super::ViewContext;

pub struct Frame;

impl super::View for Frame {
type Response = Markup;

fn render(self, _: &ViewContext) -> Self::Response {
html! {
(PreEscaped("<!DOCTYPE html>"))
head {
(PreEscaped(r#"<meta charset="utf-8">"#))
(PreEscaped(r#"<meta http-equiv="X-UA-Compatible" content="IE=edge">"#))
(PreEscaped(r#"<meta name="viewport" content="width=device-width, initial-scale=1">"#))

title { "RPG Tool" }
}

body {
}
}
}
}
24 changes: 24 additions & 0 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use rocket::response::Responder;
use rocket::{Request, Response, State};
use rocket::fairing::{Fairing, AdHoc};
use rocket::http::Status;

pub mod frame;

pub struct ViewContext;

impl ViewContext {
pub fn fairing() -> impl Fairing {
AdHoc::on_attach(|rocket| {
Ok(rocket.manage(ViewContext{}))
})
}
}

pub trait View {
type Response: Responder<'static>;

fn render(self, ctx: &ViewContext) -> Self::Response;
}

pub use self::frame::Frame;

0 comments on commit 022e181

Please sign in to comment.