ソースを参照

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年前
コミット
8a2f5f884c
6個のファイルの変更30行の追加11行の削除
  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 ファイルの表示

@@ -1006,6 +1006,7 @@ dependencies = [
1006 1006
  "async-std",
1007 1007
  "chrono",
1008 1008
  "dotenv",
1009
+ "lazy_static",
1009 1010
  "pulldown-cmark",
1010 1011
  "serde",
1011 1012
  "serde_json",

+ 1
- 0
oslo-lib/Cargo.toml ファイルの表示

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

+ 1
- 0
oslo-lib/src/lib.rs ファイルの表示

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

+ 3
- 3
oslo-lib/src/middleware.rs ファイルの表示

@@ -7,7 +7,7 @@ use tide::{
7 7
 use std::{env, future::Future, io::ErrorKind, pin::Pin};
8 8
 use tera::Context;
9 9
 
10
-use crate::{routes, State};
10
+use crate::{templates, State};
11 11
 
12 12
 pub fn session() -> SessionMiddleware<CookieStore> {
13 13
     SessionMiddleware::new(
@@ -61,7 +61,7 @@ pub async fn errors(mut res: Response) -> Result<Response> {
61 61
             } else {
62 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 65
             res.set_body(body);
66 66
             res.set_status(status);
67 67
             res.set_content_type(mime::HTML);
@@ -69,7 +69,7 @@ pub async fn errors(mut res: Response) -> Result<Response> {
69 69
         None => match res.status() {
70 70
             StatusCode::NotFound => {
71 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 73
                 res.set_content_type(mime::HTML);
74 74
             }
75 75
             _ => {}

+ 2
- 8
oslo-lib/src/routes.rs ファイルの表示

@@ -1,13 +1,13 @@
1 1
 use chrono::prelude::Local;
2 2
 use serde::{Deserialize, Serialize};
3
-use tera::{Context, Tera};
3
+use tera::Context;
4 4
 use tide::convert::json;
5 5
 use tide::http::mime;
6 6
 use tide::{Redirect, Request, Response, Result, StatusCode};
7 7
 
8 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 12
 #[derive(Debug, Serialize, Deserialize)]
13 13
 struct User {
@@ -138,12 +138,6 @@ fn redirect(path: &str) -> Result<Response> {
138 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 141
 fn prepare_flash_messages(mut req: Request<State>) -> Context {
148 142
     let mut context = Context::new();
149 143
     context.insert("flash_error", &false);

+ 22
- 0
oslo-lib/src/templates.rs ファイルの表示

@@ -0,0 +1,22 @@
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
+}

読み込み中…
キャンセル
保存