Starting to restructure the module space to allow for cleaner route additions

This commit is contained in:
R Tyler Croy 2021-07-05 12:21:33 -07:00
parent 75fb8cf760
commit 6af9c32de4
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
7 changed files with 90 additions and 43 deletions

1
Cargo.lock generated
View File

@ -2036,6 +2036,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "y10n" name = "y10n"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rtyler/y10n?branch=main#877a3c95dd2de2f1796c2e6e587dc1dcbcd64520"
dependencies = [ dependencies = [
"glob", "glob",
"lazy_static", "lazy_static",

View File

@ -14,7 +14,7 @@ glob = "0"
handlebars = { version = "4", features = ["dir_source"] } handlebars = { version = "4", features = ["dir_source"] }
log = "*" log = "*"
pretty_env_logger = "0.3" pretty_env_logger = "0.3"
# Used for embedding templates # Used for embedding templates, etc for an all in one binary
rust-embed = "5" rust-embed = "5"
# All the object serialization for APIs and persisting data on disk # All the object serialization for APIs and persisting data on disk
@ -28,4 +28,4 @@ serde_yaml = "0.8"
tide = "*" tide = "*"
# Localization support # Localization support
y10n = { "path" = "../y10n" } y10n = { "git" = "https://github.com/rtyler/y10n", branch = "main" }

View File

@ -7,6 +7,7 @@ extern crate serde_json;
use log::*; use log::*;
mod dao; mod dao;
mod routes;
mod state; mod state;
#[async_std::main] #[async_std::main]
@ -23,21 +24,7 @@ async fn main() -> Result<(), std::io::Error> {
let state = AppState::new(); let state = AppState::new();
let mut app = tide::with_state(state); let mut app = tide::with_state(state);
routes::views::register(&mut app);
app.at("/")
.get(|req: tide::Request<AppState<'static>>| async move {
let data = json!({
"projects" : dao::v1::Project::load_all(),
});
let lang = match req.header("Accept-Language") {
Some(l) => l.as_str(),
None => "en",
};
let langs = y10n::parse_accept_language(lang);
info!("Lang: {:?}", langs);
req.state().render("index", &langs, Some(data)).await
});
if let Some(fd) = std::env::var("LISTEN_FD") if let Some(fd) = std::env::var("LISTEN_FD")
.ok() .ok()

4
src/routes/mod.rs Normal file
View File

@ -0,0 +1,4 @@
/**
* This module contains all the re-exported routes for different aspects of Geoffrey
*/
pub mod views;

25
src/routes/views.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::dao::v1::*;
/**
* View routes
*/
use crate::state::*;
use log::*;
use tide::{Body, Request, Server};
pub fn register(app: &mut Server<AppState<'static>>) {
app.at("/").get(index);
}
async fn index(req: Request<AppState<'static>>) -> tide::Result<Body> {
let data = json!({
"projects" : Project::load_all(),
});
let lang = match req.header("Accept-Language") {
Some(l) => l.as_str(),
None => "en",
};
let langs = y10n::parse_accept_language(lang);
info!("Lang: {:?}", langs);
req.state().render("index", &langs, Some(data)).await
}

View File

@ -1,41 +1,65 @@
<html> <html>
<head> <head>
<title>Geoffrey</title> <title>Geoffrey</title>
<style type="text/css">
.project {
float: left;
}
.project {
margin-right: 2em;
padding: 1em;
border: 1px solid #cecece;
}
.project h3 {
margin: 0;
}
.actions ul {
padding: 0;
}
.actions li {
list-style: none;
float: left;
font-size: 0.6em;
margin-right: 1em;
}
</style>
</head> </head>
<body> <body>
<div class="banner"> {{> partials/_header}}
{{t.banner}}
</div>
<div class="projects"> <div class="projects">
<ul> <h2>
Projects
</h2>
{{#each projects}} {{#each projects}}
<li> <div class="project" id="project-{{slug}}">
<a href="project/{{slug}}"> <a href="project/{{slug}}">
{{name}} <h3>
</a> {{name}}
</h3>
</a>
<p> <p>
{{description}} {{description}}
</p> </p>
<div class="actions"> <div class="actions">
<ul> <ul>
<li> <li>
<a href="project/{{slug}}/run"> <a href="api/project/{{slug}}/run?return=/">
{{../t.actions.run}} {{../t.actions.run}}
</a> </a>
</li> </li>
<li> <li>
<a href="project/{{slug}}/edit"> <a href="api/project/{{slug}}/edit?return=/">
{{../t.actions.edit}} {{../t.actions.edit}}
</a> </a>
</li> </li>
</ul> </ul>
</div>
</div> </div>
</li>
{{/each}} {{/each}}
</ul>
</div> </div>
</body> </body>

View File

@ -0,0 +1,6 @@
<div class="banner">
<h1>
{{t.banner}}
</h1>
</div>