-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[development] | ||
address = "0.0.0.0" | ||
port = 8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |