summaryrefslogtreecommitdiff
path: root/app/config.go
blob: 2ba39a99983d84ed3ea65cc53cfb1f163325c014 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package app

import (
	"encoding/json"
	"os"
	"regexp"
	"skunkyart/static"
	"strconv"
	"time"

	"git.macaw.me/skunky/devianter"
)

var Release struct {
	Version string
	Description string
}

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

type config struct {
	cfg           string
	Listen        string
	URI           string `json:"uri"`
	Cache         cache_config
	Proxy, Nsfw   bool
	UserAgent     string `json:"user-agent"`
	DownloadProxy string `json:"download-proxy"`
	StaticPath    string `json:"static-path"`
}

var CFG = config{
	cfg:    "config.json",
	Listen: "127.0.0.1:3003",
	URI:    "/",
	Cache: cache_config{
		Enabled:        false,
		Path:           "cache",
		UpdateInterval: 1,
	},
	StaticPath: "static",
	UserAgent:  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
	Proxy:      true,
	Nsfw:       true,
}

var lifetimeParsed int64

func ExecuteConfig() {
	if CFG.cfg != "" {
		f, err := os.ReadFile(CFG.cfg)
		tryWithExitStatus(err, 1)
		tryWithExitStatus(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.Enabled {
			if CFG.Cache.Lifetime != "" {
				var duration int64
				day := 24 * time.Hour.Milliseconds()
				numstr := regexp.MustCompile("[0-9]+").FindAllString(CFG.Cache.Lifetime, -1)
				num, _ := strconv.Atoi(numstr[len(numstr)-1])

				switch unit := CFG.Cache.Lifetime[len(CFG.Cache.Lifetime)-1:]; unit {
				case "i":
					duration = time.Minute.Milliseconds()
				case "h":
					duration = time.Hour.Milliseconds()
				case "d":
					duration = day
				case "w":
					duration = day * 7
				case "m":
					duration = day * 30
				case "y":
					duration = day * 360
				default:
					exit("Invalid unit specified: "+unit, 1)
				}

				lifetimeParsed = duration * int64(num)
			}
			CFG.Cache.MaxSize *= 1024 ^ 2
			go InitCacheSystem()
		}

		About = instanceAbout{
			Proxy: CFG.Proxy,
			Nsfw: CFG.Nsfw,
		}
		
		static.StaticPath = CFG.StaticPath
		devianter.UserAgent = CFG.UserAgent
	}
}