krz/skunky-art

Alternative privacy frontend for DeviantArt.

clone: git clone https://gitbay.org/krz/skunky-art.git

v1.3: app/config.go · raw

 1package app
 2
 3import (
 4	"encoding/json"
 5	"os"
 6	"time"
 7)
 8
 9type cache_config struct {
10	Enabled        bool
11	Path           string
12	MaxSize        int64 `json:"max-size"`
13	Lifetime       int64
14	UpdateInterval int64 `json:"update-interval"`
15}
16
17type config struct {
18	cfg           string
19	Listen        string
20	BasePath      string `json:"base-path"`
21	Cache         cache_config
22	Proxy, Nsfw   bool
23	DownloadProxy string   `json:"download-proxy"`
24	Dirs          []string `json:"dirs-to-memory"`
25}
26
27var CFG = config{
28	cfg:      "config.json",
29	Listen:   "127.0.0.1:3003",
30	BasePath: "/",
31	Cache: cache_config{
32		Enabled:        true,
33		Path:           "cache",
34		UpdateInterval: 1,
35	},
36	Dirs:  []string{"html", "css"},
37	Proxy: true,
38	Nsfw:  true,
39}
40
41func ExecuteConfig() {
42	go func() {
43		defer func() {
44			if r := recover(); r != nil {
45				recover()
46			}
47		}()
48		for {
49			Templates["instances.json"] = string(Download("https://git.macaw.me/skunky/SkunkyArt/raw/branch/master/instances.json").Body)
50			time.Sleep(1 * time.Second)
51		}
52	}()
53
54	const helpmsg = `SkunkyArt v1.3 [refactoring]
55Usage:
56	- [-c|--config] - path to config
57	- [-h|--help]	- returns this message
58Example:
59	./skunkyart -c config.json
60Copyright lost+skunk, X11. https://git.macaw.me/skunky/skunkyart/src/tag/v1.3`
61
62	a := os.Args
63	for n, x := range a {
64		switch x {
65		case "-c", "--config":
66			if len(a) >= 3 {
67				CFG.cfg = a[n+1]
68			} else {
69				exit("Not enought arguments", 1)
70			}
71		case "-h", "--help":
72			exit(helpmsg, 0)
73		}
74	}
75
76	if CFG.cfg != "" {
77		f, err := os.ReadFile(CFG.cfg)
78		try_with_exitstatus(err, 1)
79
80		try_with_exitstatus(json.Unmarshal(f, &CFG), 1)
81		if CFG.Cache.Enabled && !CFG.Proxy {
82			exit("Incompatible settings detected: cannot use caching media content without proxy", 1)
83		}
84
85		if CFG.Cache.MaxSize != 0 || CFG.Cache.Lifetime != 0 {
86			go InitCacheSystem()
87		}
88	}
89}