aboutsummaryrefslogtreecommitdiff
path: root/backend/config.go
blob: 3e605c81308d63ff7327f10e6c155e5ceb2eb560 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
package backend

import (
	"html/template"
	"log/slog"
	"os"

	"git.anhgelus.world/anhgelus/small-web/markdown"
	"github.com/pelletier/go-toml/v2"
)

type Link struct {
	Name string `toml:"name"`
	URL  string `toml:"url"`
}

func (l *Link) Render(url string) template.HTML {
	return renderLink(l.Name, l.URL, url)
}

type Logo struct {
	Header  string `toml:"header"`
	Favicon string `toml:"favicon"`
}

type Replacer struct {
	Symbol  string `toml:"symbol"`
	Replace string `tomle:"replace"`
}

type Config struct {
	Domain        string   `toml:"domain"`
	Name          string   `toml:"name"`
	Description   string   `toml:"description"`
	Quotes        []string `toml:"quotes"`
	Language      string   `toml:"language"`
	Database      string   `toml:"database"`
	AdminPassword string   `toml:"admin_password"`

	Sections []Section `toml:"section"`

	RootFolder   string `toml:"root_folder"`
	PublicFolder string `toml:"public_folder"`

	Links []Link `toml:"links"`
	Logo  Logo   `toml:"logo"`

	Replacers []Replacer `toml:"replacers"`
}

func (c *Config) DefaultValues() {
	c.Domain = "example.org"
	c.Name = "example"
	c.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. Quod idem licet transferre in voluptatem, ut."
	c.Links = []Link{
		{
			Name: "Home",
			URL:  "/",
		},
		{
			Name: "Logs",
			URL:  "/log/",
		},
	}
	c.Logo = Logo{
		Header:  "logo.jpg",
		Favicon: "favicon.jpg",
	}
	c.Sections = []Section{{
		Name:        "logs",
		TitleName:   "log",
		Description: "Aut maxime voluptatibus ut dicta voluptates et ut alias. Sunt et incidunt similique et doloremque nostrum fugit autem. Ut omnis quo nisi. Accusantium voluptas fugit autem maiores numquam doloribus.",
		Folder:      "data/logs",
		URI:         "logs",
	}}
	c.RootFolder = "data"
	c.PublicFolder = "public"
	c.Database = "database.sqlite"
	c.AdminPassword = "Ch@ngeM€Please!"
	c.Quotes = []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do."}
	c.Replacers = []Replacer{{"~", " "}}
}

var defaultMarkdownOption markdown.Option

func LoadConfig(path string) (*Config, bool) {
	b, err := os.ReadFile(path)
	var config Config
	if err != nil {
		if !os.IsNotExist(err) {
			slog.Error("reading config file", "error", err)
			return nil, false
		}
		slog.Warn("config file not found", "path", path)
		slog.Info("creating a new config file", "path", path)
		config.DefaultValues()
		b, err = toml.Marshal(&config)
		if err != nil {
			slog.Error("marshalling config file", "error", err)
			return nil, false
		}
		err = os.WriteFile(path, b, 0660)
		if err != nil {
			slog.Error("writing config file", "error", err, "path", path)
		} else {
			slog.Info("config file created", "path", path)
		}
		return nil, false
	}
	err = toml.Unmarshal(b, &config)
	if err != nil {
		slog.Error("unmarshalling config file", "error", err)
		return nil, false
	}
	if len(config.AdminPassword) == 0 {
		config.AdminPassword = os.Getenv("SW_ADMIN_PASSWORD")
	}
	defaultMarkdownOption.ImageSource = getStatic
	defaultMarkdownOption.Replaces = make(map[rune]string, len(config.Replacers))
	for _, r := range config.Replacers {
		if len(r.Symbol) != 1 {
			slog.Error("invalid symbol in config", "symbol", r.Symbol)
			return nil, false
		}
		defaultMarkdownOption.Replaces[[]rune(r.Symbol)[0]] = r.Replace
	}
	return &config, true
}