summaryrefslogtreecommitdiff
path: root/src/render.rs
blob: c1414059c3562171c3385bc8df12d35fc81a882c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2026 Doan Luu Duc Tai <doan@luuduct.ai>
//
// 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 crate::files;
use crate::project::{Mode, Project, Settings};
use anyhow::Result;
use stack_config::Config;
use std::fs;
use std::path::PathBuf;

pub(crate) fn directory(project: &Project, settings: &Settings) -> PathBuf {
    project.root.join(match settings.mode {
        Mode::Local => "var/local",
        Mode::Production => "var/production",
    })
}

pub(crate) fn prepare(project: &Project, settings: &Settings) -> Result<()> {
    let config = Config::load(&project.root)?;
    let local = (settings.mode == Mode::Local).then_some(settings.port);
    let output = directory(project, settings);
    let web = output.join("web");
    if web.exists() {
        files::regular_tree(&web)?;
        fs::remove_dir_all(&web)?;
    }
    files::copy_tree(&project.root.join("web"), &web)?;
    for name in ["index.html", "message/index.html"] {
        let file = web.join(name);
        files::write(
            &file,
            config
                .render(&fs::read_to_string(&file)?, local)?
                .as_bytes(),
            0o644,
        )?;
    }
    let template = project.root.join(if settings.mode == Mode::Local {
        "infra/Caddyfile.local"
    } else {
        "infra/Caddyfile"
    });
    files::write(
        &output.join("Caddyfile"),
        config
            .render(&fs::read_to_string(template)?, local)?
            .as_bytes(),
        0o644,
    )?;
    let cgit = fs::read_to_string(project.root.join("infra/containers/cgit/cgitrc"))?;
    let mut rendered = config.render(&cgit, local)?;
    if let Some(port) = local {
        rendered = rendered.replace(
            "clone-prefix=/git",
            &format!("clone-prefix=http://localhost:{port}/git"),
        );
    }
    files::write(&output.join("cgitrc"), rendered.as_bytes(), 0o644)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn custom_domains_and_ports_reach_public_outputs_without_secrets() -> Result<()> {
        let root = tempfile::tempdir()?;
        for name in ["web", "infra"] {
            files::copy_tree(
                &PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(name),
                &root.path().join(name),
            )?;
        }
        fs::write(
            root.path().join(".env"),
            "SITE_HOST=home.example.net\nGIT_HOST=git.example.net\nHTTP_PORT=8181\nHTTPS_PORT=8443\nSEARXNG_SECRET=do-not-publish-this-sentinel\n",
        )?;
        let project = Project::at(root.path())?;
        let mut settings = project.settings()?;
        settings.mode = Mode::Production;
        prepare(&project, &settings)?;
        let output = directory(&project, &settings);
        let caddy = fs::read_to_string(output.join("Caddyfile"))?;
        assert!(caddy.contains("https://home.example.net:8443"));
        assert!(caddy.contains("http_port 8181"));
        assert!(!caddy.contains("do-not-publish"));
        let homepage = fs::read_to_string(output.join("web/index.html"))?;
        assert!(homepage.contains("https://git.example.net:8443"));
        assert!(homepage.contains("https://chat.doan.luuduct.ai:8443/LIMITATIONS.txt"));
        assert!(homepage.contains("A home-grown experimental messenger. Know its"));
        settings.mode = Mode::Local;
        settings.port = 8180;
        prepare(&project, &settings)?;
        let preview = fs::read_to_string(directory(&project, &settings).join("web/index.html"))?;
        assert!(preview.contains("href=\"/git/\""));
        assert!(preview.contains("http://chat.localhost:8180/LIMITATIONS.txt"));
        assert!(
            fs::read_to_string(directory(&project, &settings).join("cgitrc"))?
                .contains("clone-prefix=http://localhost:8180/git")
        );
        Ok(())
    }
}