From 1e5a3d57b51380b3e0018a83686ff6fadba08ea6 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 12 Dec 2022 19:27:40 +0530 Subject: [PATCH] feat: create site templates --- env/nginx_bind_le/src/templates.rs | 73 +++++++++++++++++++ .../templates/create_cert_le.sh.j2 | 1 + .../templates/nginx/_new_site.fragement.j2 | 29 ++++++++ .../templates/nginx/create-site.j2 | 7 ++ 4 files changed, 110 insertions(+) create mode 100644 env/nginx_bind_le/src/templates.rs create mode 100644 env/nginx_bind_le/templates/create_cert_le.sh.j2 create mode 100644 env/nginx_bind_le/templates/nginx/_new_site.fragement.j2 create mode 100644 env/nginx_bind_le/templates/nginx/create-site.j2 diff --git a/env/nginx_bind_le/src/templates.rs b/env/nginx_bind_le/src/templates.rs new file mode 100644 index 0000000..ef95be7 --- /dev/null +++ b/env/nginx_bind_le/src/templates.rs @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * 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 . + */ +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 { + 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)) + } +} diff --git a/env/nginx_bind_le/templates/create_cert_le.sh.j2 b/env/nginx_bind_le/templates/create_cert_le.sh.j2 new file mode 100644 index 0000000..87dc4ff --- /dev/null +++ b/env/nginx_bind_le/templates/create_cert_le.sh.j2 @@ -0,0 +1 @@ +sudo certbot --nginx -d {{ hostname }} diff --git a/env/nginx_bind_le/templates/nginx/_new_site.fragement.j2 b/env/nginx_bind_le/templates/nginx/_new_site.fragement.j2 new file mode 100644 index 0000000..2459bec --- /dev/null +++ b/env/nginx_bind_le/templates/nginx/_new_site.fragement.j2 @@ -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; + } diff --git a/env/nginx_bind_le/templates/nginx/create-site.j2 b/env/nginx_bind_le/templates/nginx/create-site.j2 new file mode 100644 index 0000000..ebe4499 --- /dev/null +++ b/env/nginx_bind_le/templates/nginx/create-site.j2 @@ -0,0 +1,7 @@ +{% include "new_site_frag" %} + +{% if domains %} + {% for hostname in domains %} + {% include "new_site_frag" %} + {% endfor %} +{% endif %}