Browse Source

Make templates static

This way, they get bundled into the binary and don't require the templates files to actually be on the server
master
Dylan Baker 3 years ago
parent
commit
8a2f5f884c
6 changed files with 30 additions and 11 deletions
  1. 1
    0
      Cargo.lock
  2. 1
    0
      oslo-lib/Cargo.toml
  3. 1
    0
      oslo-lib/src/lib.rs
  4. 3
    3
      oslo-lib/src/middleware.rs
  5. 2
    8
      oslo-lib/src/routes.rs
  6. 22
    0
      oslo-lib/src/templates.rs

+ 1
- 0
Cargo.lock View File

1006
  "async-std",
1006
  "async-std",
1007
  "chrono",
1007
  "chrono",
1008
  "dotenv",
1008
  "dotenv",
1009
+ "lazy_static",
1009
  "pulldown-cmark",
1010
  "pulldown-cmark",
1010
  "serde",
1011
  "serde",
1011
  "serde_json",
1012
  "serde_json",

+ 1
- 0
oslo-lib/Cargo.toml View File

10
 async-std = { version = "1.6.0", features = ["attributes"] }
10
 async-std = { version = "1.6.0", features = ["attributes"] }
11
 chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
11
 chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
12
 dotenv = "0.15.0"
12
 dotenv = "0.15.0"
13
+lazy_static = "1.4.0"
13
 pulldown-cmark = { version = "0.7", default-features = false }
14
 pulldown-cmark = { version = "0.7", default-features = false }
14
 serde = "1.0.115"
15
 serde = "1.0.115"
15
 serde_json = "1.0.57"
16
 serde_json = "1.0.57"

+ 1
- 0
oslo-lib/src/lib.rs View File

5
 mod middleware;
5
 mod middleware;
6
 mod post;
6
 mod post;
7
 mod routes;
7
 mod routes;
8
+mod templates;
8
 mod util;
9
 mod util;
9
 
10
 
10
 use middleware::*;
11
 use middleware::*;

+ 3
- 3
oslo-lib/src/middleware.rs View File

7
 use std::{env, future::Future, io::ErrorKind, pin::Pin};
7
 use std::{env, future::Future, io::ErrorKind, pin::Pin};
8
 use tera::Context;
8
 use tera::Context;
9
 
9
 
10
-use crate::{routes, State};
10
+use crate::{templates, State};
11
 
11
 
12
 pub fn session() -> SessionMiddleware<CookieStore> {
12
 pub fn session() -> SessionMiddleware<CookieStore> {
13
     SessionMiddleware::new(
13
     SessionMiddleware::new(
61
             } else {
61
             } else {
62
                 StatusCode::InternalServerError
62
                 StatusCode::InternalServerError
63
             };
63
             };
64
-            let body: String = routes::render_template("error.html", &context)?;
64
+            let body: String = templates::render_template("error.html", &context)?;
65
             res.set_body(body);
65
             res.set_body(body);
66
             res.set_status(status);
66
             res.set_status(status);
67
             res.set_content_type(mime::HTML);
67
             res.set_content_type(mime::HTML);
69
         None => match res.status() {
69
         None => match res.status() {
70
             StatusCode::NotFound => {
70
             StatusCode::NotFound => {
71
                 context.insert("error", "Page not found");
71
                 context.insert("error", "Page not found");
72
-                res.set_body(routes::render_template("error.html", &context)?);
72
+                res.set_body(templates::render_template("error.html", &context)?);
73
                 res.set_content_type(mime::HTML);
73
                 res.set_content_type(mime::HTML);
74
             }
74
             }
75
             _ => {}
75
             _ => {}

+ 2
- 8
oslo-lib/src/routes.rs View File

1
 use chrono::prelude::Local;
1
 use chrono::prelude::Local;
2
 use serde::{Deserialize, Serialize};
2
 use serde::{Deserialize, Serialize};
3
-use tera::{Context, Tera};
3
+use tera::Context;
4
 use tide::convert::json;
4
 use tide::convert::json;
5
 use tide::http::mime;
5
 use tide::http::mime;
6
 use tide::{Redirect, Request, Response, Result, StatusCode};
6
 use tide::{Redirect, Request, Response, Result, StatusCode};
7
 
7
 
8
 use std::env;
8
 use std::env;
9
 
9
 
10
-use crate::{fs, post::Post, util, State};
10
+use crate::{fs, post::Post, templates::render_template, util, State};
11
 
11
 
12
 #[derive(Debug, Serialize, Deserialize)]
12
 #[derive(Debug, Serialize, Deserialize)]
13
 struct User {
13
 struct User {
138
     Ok(Redirect::new(path).into())
138
     Ok(Redirect::new(path).into())
139
 }
139
 }
140
 
140
 
141
-pub fn render_template(template: &str, context: &Context) -> Result<String> {
142
-    let tera = Tera::new("templates/**/*.html")?;
143
-    let html = tera.render(template, &context)?;
144
-    Ok(html)
145
-}
146
-
147
 fn prepare_flash_messages(mut req: Request<State>) -> Context {
141
 fn prepare_flash_messages(mut req: Request<State>) -> Context {
148
     let mut context = Context::new();
142
     let mut context = Context::new();
149
     context.insert("flash_error", &false);
143
     context.insert("flash_error", &false);

+ 22
- 0
oslo-lib/src/templates.rs View File

1
+use lazy_static::lazy_static;
2
+use tera::{Context, Tera};
3
+use tide::Result;
4
+
5
+lazy_static! {
6
+    pub static ref TEMPLATES: Tera = {
7
+        let mut tera = match Tera::new("templates/**/*.html") {
8
+            Ok(t) => t,
9
+            Err(e) => {
10
+                println!("Parsing error(s): {}", e);
11
+                ::std::process::exit(1);
12
+            }
13
+        };
14
+        tera.autoescape_on(vec!["html", ".sql"]);
15
+        tera
16
+    };
17
+}
18
+
19
+pub fn render_template(template: &str, context: &Context) -> Result<String> {
20
+    let html = TEMPLATES.render(template, &context)?;
21
+    Ok(html)
22
+}

Loading…
Cancel
Save