feat: create site templates
This commit is contained in:
parent
e9237d3586
commit
1e5a3d57b5
4 changed files with 110 additions and 0 deletions
73
env/nginx_bind_le/src/templates.rs
vendored
Normal file
73
env/nginx_bind_le/src/templates.rs
vendored
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
use lazy_static::lazy_static;
|
||||
use rust_embed::RustEmbed;
|
||||
use tera::*;
|
||||
|
||||
pub const PAYLOAD_KEY: &str = "payload";
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TEMPLATES: Tera = {
|
||||
let mut tera = Tera::default();
|
||||
|
||||
for template in [crate::nginx::CREATE_SITE, crate::nginx::CREATE_SITE_FRAGMENT].iter() {
|
||||
template.register(&mut tera).expect(template.name);
|
||||
}
|
||||
// tera.autoescape_on(vec![".html", ".sql"]);
|
||||
tera
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "templates/"]
|
||||
pub struct Templates;
|
||||
|
||||
impl Templates {
|
||||
pub fn get_template(t: &TemplateFile) -> Option<String> {
|
||||
match Self::get(t.path) {
|
||||
Some(file) => Some(String::from_utf8_lossy(&file.data).into_owned()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn context() -> Context {
|
||||
let mut ctx = Context::new();
|
||||
ctx
|
||||
}
|
||||
|
||||
pub struct TemplateFile {
|
||||
pub name: &'static str,
|
||||
pub path: &'static str,
|
||||
}
|
||||
|
||||
impl TemplateFile {
|
||||
pub const fn new(name: &'static str, path: &'static str) -> Self {
|
||||
Self { name, path }
|
||||
}
|
||||
|
||||
pub fn register(&self, t: &mut Tera) -> std::result::Result<(), tera::Error> {
|
||||
t.add_raw_template(self.name, &Templates::get_template(self).expect(self.name))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
pub fn register_from_file(&self, t: &mut Tera) -> std::result::Result<(), tera::Error> {
|
||||
use std::path::Path;
|
||||
t.add_template_file(Path::new("templates/").join(self.path), Some(self.name))
|
||||
}
|
||||
}
|
1
env/nginx_bind_le/templates/create_cert_le.sh.j2
vendored
Normal file
1
env/nginx_bind_le/templates/create_cert_le.sh.j2
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
sudo certbot --nginx -d {{ hostname }}
|
29
env/nginx_bind_le/templates/nginx/_new_site.fragement.j2
vendored
Normal file
29
env/nginx_bind_le/templates/nginx/_new_site.fragement.j2
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
server {
|
||||
# serve website on port 80
|
||||
listen [::]:80;
|
||||
listen 80;
|
||||
|
||||
# write error logs to file
|
||||
error_log /var/log/nginx/{{ hostname }}.error.log;
|
||||
# write access logs to file
|
||||
access_log /var/log/nginx/{{ hostname }}.access.log;
|
||||
|
||||
# serve only on this domain:
|
||||
server_name {{ hostname }};
|
||||
|
||||
# use files from this directory
|
||||
root {{ path }};
|
||||
|
||||
# remove .html from URL; it is cleaner this way
|
||||
rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
|
||||
|
||||
{% if redirects %}
|
||||
{% for redirect in redirects %}
|
||||
rewrite ^/{{redirect.from}}$ /{{ redirect.to }} redirect;
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
# when a request is received, try the index.html in the directory
|
||||
# or $uri.html
|
||||
try_files $uri/index.html $uri.html $uri/ $uri =404;
|
||||
}
|
7
env/nginx_bind_le/templates/nginx/create-site.j2
vendored
Normal file
7
env/nginx_bind_le/templates/nginx/create-site.j2
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{% include "new_site_frag" %}
|
||||
|
||||
{% if domains %}
|
||||
{% for hostname in domains %}
|
||||
{% include "new_site_frag" %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
Loading…
Reference in a new issue