krz/skunky-art
Alternative privacy frontend for DeviantArt.
clone: git clone https://gitbay.org/krz/skunky-art.git
1package app
2
3import (
4 "encoding/json"
5 "os"
6 "regexp"
7 "strconv"
8 "time"
9
10 "git.macaw.me/skunky/devianter"
11)
12
13type cache_config struct {
14 Enabled bool
15 Path string
16 MaxSize int64 `json:"max-size"`
17 Lifetime string
18 UpdateInterval int64 `json:"update-interval"`
19}
20
21type config struct {
22 cfg string
23 Listen string
24 URI string `json:"uri"`
25 Cache cache_config
26 Proxy, Nsfw bool
27 UserAgent string `json:"user-agent"`
28 DownloadProxy string `json:"download-proxy"`
29 Dirs []string `json:"dirs-to-memory"`
30}
31
32var CFG = config{
33 cfg: "config.json",
34 Listen: "127.0.0.1:3003",
35 URI: "/",
36 Cache: cache_config{
37 Enabled: false,
38 Path: "cache",
39 UpdateInterval: 1,
40 },
41 Dirs: []string{"html", "css", "misc"},
42 UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
43 Proxy: true,
44 Nsfw: true,
45}
46
47var lifetimeParsed int64
48
49func ExecuteConfig() {
50 if CFG.cfg != "" {
51 f, err := os.ReadFile(CFG.cfg)
52 tryWithExitStatus(err, 1)
53
54 tryWithExitStatus(json.Unmarshal(f, &CFG), 1)
55 if CFG.Cache.Enabled && !CFG.Proxy {
56 exit("Incompatible settings detected: cannot use caching media content without proxy", 1)
57 }
58
59 if CFG.Cache.Enabled {
60 if CFG.Cache.Lifetime != "" {
61 var duration int64
62 day := 24 * time.Hour.Milliseconds()
63 numstr := regexp.MustCompile("[0-9]+").FindAllString(CFG.Cache.Lifetime, -1)
64 num, _ := strconv.Atoi(numstr[len(numstr)-1])
65
66 switch unit := CFG.Cache.Lifetime[len(CFG.Cache.Lifetime)-1:]; unit {
67 case "i":
68 duration = time.Minute.Milliseconds()
69 case "h":
70 duration = time.Hour.Milliseconds()
71 case "d":
72 duration = day
73 case "w":
74 duration = day * 7
75 case "m":
76 duration = day * 30
77 case "y":
78 duration = day * 360
79 default:
80 exit("Invalid unit specified: "+unit, 1)
81 }
82
83 lifetimeParsed = duration * int64(num)
84 }
85 CFG.Cache.MaxSize /= 1024 ^ 2
86 go InitCacheSystem()
87 }
88 devianter.UserAgent = CFG.UserAgent
89 }
90}