This commit is contained in:
Michael Zhang 2018-12-06 04:49:00 -06:00
parent 1709cbf418
commit 705ac39094
No known key found for this signature in database
GPG Key ID: A1B65B603268116B
8 changed files with 106 additions and 19 deletions

View File

@ -1,7 +1,7 @@
use std::sync::{Arc, Mutex};
use failure::Error;
use tera::Tera;
use tera::{Context, Tera};
use config::{Config, FilestoreConfig, WebConfig};
use db::{establish_connection, Connection, Pool};
@ -40,15 +40,13 @@ impl State {
self.get_web_config().and_then(|cfg| cfg.filestore.as_ref())
}
pub fn renderer<F, T>(&self, f: F) -> Result<T, Error>
where
F: Fn(&Tera) -> T,
{
pub fn render(&self, page: impl AsRef<str>, ctx: &Context) -> Result<String, Error> {
let t = self
.tera
.lock()
.map_err(|err| format_err!("Internal error acquiring Tera lock: {}", err))?;
Ok(f(&t))
t.render(page.as_ref(), ctx)
.map_err(|err| format_err!("Internal error rendering template: {}", err))
}
pub fn add_templates(&mut self, templates: Vec<(&str, &str)>) -> Result<(), Error> {

View File

@ -1,8 +1,13 @@
mod pages;
mod user;
use actix_web::App;
use actix_web::{middleware::csrf::CsrfFilter, App};
use core::State;
pub fn router(state: State) -> App<State> {
App::with_state(state).resource("/", |r| r.get().with(self::pages::handler))
App::with_state(state)
.middleware(CsrfFilter::new())
.resource("/", |r| r.get().with(self::pages::handler))
.resource("/static/{path:.*}", |r| r.get().with(self::pages::statics))
.scope("/user", self::user::scope)
}

View File

@ -1,8 +1,14 @@
use std::path::PathBuf;
use actix_web::{HttpRequest, HttpResponse};
use core::{pages::get_page, State};
use tera::Context;
const WELCOME_MESSAGE: &'static str = "Welcome to LibreCTF! You're seeing this message because you haven't set up your index page yet.";
const WELCOME_MESSAGE: &'static str = "Welcome to LibreCTF! You're seeing this message because you probably haven't set up your index page yet. Head over to the admin panel to set it up!";
#[derive(Embed)]
#[folder = "components/frontend/static"]
struct Static;
pub fn handler(req: HttpRequest<State>) -> HttpResponse {
let state = req.state();
@ -15,16 +21,20 @@ pub fn handler(req: HttpRequest<State>) -> HttpResponse {
};
state
.renderer(|tera| {
tera.render("index.html", &ctx)
.map(|content| HttpResponse::Ok().body(content))
.map(|err| err.into())
.unwrap_or_else(|err| {
error!("Error during Tera rendering: {}", err);
HttpResponse::InternalServerError().finish()
})
}).unwrap_or_else(|err| {
error!("{}", err);
.render("page.html", &ctx)
.map(|content| HttpResponse::Ok().body(content))
.map(|err| err.into())
.unwrap_or_else(|err| {
error!("Error during Tera rendering: {}", err);
HttpResponse::InternalServerError().finish()
})
}
pub fn statics(req: HttpRequest<State>) -> HttpResponse {
let path = req.match_info().query::<String>("path").unwrap();
println!("{:?}", path);
match Static::get(&path) {
Some(contents) => HttpResponse::Ok().body(contents),
None => HttpResponse::NotFound().finish(),
}
}

View File

@ -0,0 +1,20 @@
use actix_web::{HttpRequest, HttpResponse, Scope};
use core::State;
use tera::Context;
pub fn scope(app: Scope<State>) -> Scope<State> {
app.resource("/login", |r| r.with(login))
}
fn login(req: HttpRequest<State>) -> HttpResponse {
let state = req.state();
let ctx = Context::new();
state.render("user/login.html", &ctx)
.map(|content| HttpResponse::Ok().body(content))
.map(|err| err.into())
.unwrap_or_else(|err| {
error!("Error during Tera rendering: {}", err);
HttpResponse::InternalServerError().finish()
})
}

View File

@ -0,0 +1,37 @@
body {
max-width: 854px;
margin: auto;
font-size: 11pt;
font-family: monospace;
}
header {
margin-bottom: 14px;
}
header > nav {
background-color: black;
}
header > nav > ul {
list-style-type: none;
padding: 0;
}
header > nav > ul > li {
display: inline-block;
}
header > nav > ul > li.right {
float: right;
}
header > nav > ul > li > a {
display: inline-block;
color: white;
padding: 5px;
}
header > nav > ul > li > a:visited {
color: white;
}

View File

@ -1,8 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/main.css" />
</head>
<body>
<header>
<h1>LibreCTF</h1>
<nav>
<ul>
<li><a href="/">home</a></li>
<li><a href="/scoreboard">scoreboard</a></li>
<li class="right"><a href="/user/login">login</a></li>
<li class="right"><a href="/user/register">register</a></li>
</ul>
</nav>
</header>
{% block content %}{% endblock %}
</body>
</html>

View File

@ -0,0 +1,5 @@
{% extends "layout.html" %}
{% block content %}
login
{% endblock %}