summaryrefslogtreecommitdiff
path: root/app/config.go
blob: 96aea406c5112abde41f1f59ca845240560712ab (plain) (blame)
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
package app

import (
	"encoding/json"
	"os"
	"time"
)

type cache_config struct {
	Enabled        bool
	Path           string
	MaxSize        int64 `json:"max-size"`
	Lifetime       int64
	UpdateInterval int64 `json:"update-interval"`
}

type config struct {
	cfg           string
	Listen        string
	BasePath      string `json:"base-path"`
	Cache         cache_config
	Proxy, Nsfw   bool
	DownloadProxy string   `json:"download-proxy"`
	Dirs          []string `json:"dirs-to-memory"`
}

var CFG = config{
	cfg:      "config.json",
	Listen:   "127.0.0.1:3003",
	BasePath: "/",
	Cache: cache_config{
		Enabled:        true,
		Path:           "cache",
		UpdateInterval: 1,
	},
	Dirs:  []string{"html", "css"},
	Proxy: true,
	Nsfw:  true,
}

func ExecuteConfig() {
	go func() {
		for {
			Templates["instances.json"] = string(Download("https://git.macaw.me/skunky/SkunkyArt/raw/branch/master/instances.json").Body)
			time.Sleep(1 * time.Hour)
		}
	}()

	const helpmsg = `SkunkyArt v1.3 [refactoring]
Usage:
	- [-c|--config] - path to config
	- [-h|--help]	- returns this message
Example:
	./skunkyart -c config.json
Copyright lost+skunk, X11. https://git.macaw.me/skunky/skunkyart/src/tag/v1.3`

	a := os.Args
	for n, x := range a {
		switch x {
		case "-c", "--config":
			if len(a) >= 3 {
				CFG.cfg = a[n+1]
			} else {
				exit("Not enought arguments", 1)
			}
		case "-h", "--help":
			exit(helpmsg, 0)
		}
	}

	if CFG.cfg != "" {
		f, err := os.ReadFile(CFG.cfg)
		try_with_exitstatus(err, 1)

		try_with_exitstatus(json.Unmarshal(f, &CFG), 1)
		if CFG.Cache.Enabled && !CFG.Proxy {
			exit("Incompatible settings detected: cannot use caching media content without proxy", 1)
		}

		if CFG.Cache.MaxSize != 0 || CFG.Cache.Lifetime != 0 {
			go InitCacheSystem()
		}
	}
}