Add user facing index page

This commit is contained in:
Raphaël Thériault 2019-10-22 14:34:26 -04:00
parent 4927e32fe7
commit 1b2c1b1199
8 changed files with 100 additions and 2 deletions

1
Cargo.lock generated
View File

@ -654,6 +654,7 @@ dependencies = [
"dotenv 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libsqlite3-sys 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -17,6 +17,7 @@ dirs = "2.0.2"
dotenv = { version = "0.14.1", optional = true }
env_logger = "0.7.1"
futures = "0.1.29"
lazy_static = "1.4.0"
num_cpus = "1.10.1"
toml = "0.5.3"
[dependencies.diesel]

View File

@ -0,0 +1,18 @@
{
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "consistent",
"jsxSingleQuote": false,
"trailingComma": "es5",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "never",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
}

11
resources/web/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>filite</title>
</head>
<body>
Hello world!
</body>
</html>

0
resources/web/script.js Normal file
View File

0
resources/web/style.css Normal file
View File

View File

@ -1,6 +1,8 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde;
#[cfg_attr(not(feature = "dev"), macro_use)]
@ -84,6 +86,7 @@ fn main() {
.secure(false),
))
.wrap(setup::logger_middleware())
.route("/", web::get().to(routes::index))
.route("/login", web::get().to(routes::login))
.route("/logout", web::get().to(routes::logout))
.route("/config", web::get().to(routes::get_config))

View File

@ -8,6 +8,11 @@ use chrono::{DateTime, NaiveDateTime, Utc};
use diesel;
use serde::Serialize;
#[cfg(feature = "dev")]
use crate::get_env;
#[cfg(feature = "dev")]
use std::{fs, path::PathBuf};
/// Parses an ID
fn parse_id(id: &str) -> Result<i32, HttpResponse> {
match i32::from_str_radix(id, 36) {
@ -122,12 +127,71 @@ macro_rules! delete {
};
}
#[cfg(feature = "dev")]
lazy_static! {
static ref RESOURCES_DIR: PathBuf = {
let mut ressources_dir = PathBuf::new();
ressources_dir.push(get_env!("CARGO_MANIFEST_DIR"));
ressources_dir.push("resources");
ressources_dir.push("web");
ressources_dir
};
static ref HTML_PATH: PathBuf = {
let mut html_path = RESOURCES_DIR.clone();
html_path.push("index.html");
html_path
};
static ref JS_PATH: PathBuf = {
let mut js_path = RESOURCES_DIR.clone();
js_path.push("script.js");
js_path
};
static ref CSS_PATH: PathBuf = {
let mut css_path = RESOURCES_DIR.clone();
css_path.push("style.css");
css_path
};
}
#[cfg(not(feature = "dev"))]
lazy_static! {
static ref INDEX_CONTENTS: String = {
let html = include_str!("../resources/web/index.html");
let js = include_str!("../resources/web/script.js");
let css = include_str!("../resources/web/style.css");
html.replace("{{ js }}", js).replace("{{ css }}", css)
};
}
/// Index page letting users upload via a UI
pub fn index(_identity: Identity) -> impl Responder {
let contents = {
#[cfg(feature = "dev")]
{
let html = fs::read_to_string(&*HTML_PATH).expect("Can't read index.html");
let js = fs::read_to_string(&*JS_PATH).expect("Can't read script.js");
let css = fs::read_to_string(&*CSS_PATH).expect("Can't read style.css");
html.replace("{{ js }}", &js).replace("{{ css }}", &css)
}
#[cfg(not(feature = "dev"))]
{
INDEX_CONTENTS.clone()
}
};
HttpResponse::Ok()
.header("Content-Type", "text/html")
.body(contents)
}
/// GET the config info
pub fn get_config(
request: HttpRequest,
config: web::Data<Config>,
identity: actix_identity::Identity,
token_hash: actix_web::web::Data<Vec<u8>>,
identity: Identity,
token_hash: web::Data<Vec<u8>>,
) -> impl Responder {
match auth(identity, request, &token_hash) {
Ok(_) => HttpResponse::Ok().json(config.get_ref()),