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