2024-06-13 20:56:03 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-07-05 21:46:25 +00:00
|
|
|
"encoding/json"
|
2024-06-13 20:56:03 +00:00
|
|
|
"os"
|
2024-07-29 22:06:40 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2024-07-13 18:32:04 +00:00
|
|
|
"time"
|
2024-07-29 22:06:40 +00:00
|
|
|
|
|
|
|
"git.macaw.me/skunky/devianter"
|
2024-06-13 20:56:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cache_config struct {
|
2024-07-06 17:06:04 +00:00
|
|
|
Enabled bool
|
|
|
|
Path string
|
|
|
|
MaxSize int64 `json:"max-size"`
|
2024-07-29 22:06:40 +00:00
|
|
|
Lifetime string
|
2024-07-06 17:06:04 +00:00
|
|
|
UpdateInterval int64 `json:"update-interval"`
|
2024-06-13 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2024-07-13 18:32:04 +00:00
|
|
|
cfg string
|
|
|
|
Listen string
|
2024-08-01 19:48:05 +00:00
|
|
|
URI string `json:"uri"`
|
2024-07-13 18:32:04 +00:00
|
|
|
Cache cache_config
|
|
|
|
Proxy, Nsfw bool
|
2024-07-29 22:06:40 +00:00
|
|
|
UserAgent string `json:"user-agent"`
|
2024-07-13 18:32:04 +00:00
|
|
|
DownloadProxy string `json:"download-proxy"`
|
|
|
|
Dirs []string `json:"dirs-to-memory"`
|
2024-06-13 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var CFG = config{
|
2024-08-01 19:48:05 +00:00
|
|
|
cfg: "config.json",
|
|
|
|
Listen: "127.0.0.1:3003",
|
|
|
|
URI: "/",
|
2024-06-13 20:56:03 +00:00
|
|
|
Cache: cache_config{
|
2024-08-01 19:48:05 +00:00
|
|
|
Enabled: false,
|
2024-07-06 17:06:04 +00:00
|
|
|
Path: "cache",
|
|
|
|
UpdateInterval: 1,
|
2024-06-13 20:56:03 +00:00
|
|
|
},
|
2024-08-01 19:48:05 +00:00
|
|
|
Dirs: []string{"html", "css", "misc"},
|
2024-07-29 22:06:40 +00:00
|
|
|
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,
|
2024-06-13 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 22:06:40 +00:00
|
|
|
var lifetimeParsed int64
|
|
|
|
|
2024-07-05 21:46:25 +00:00
|
|
|
func ExecuteConfig() {
|
2024-07-13 18:32:04 +00:00
|
|
|
if CFG.cfg != "" {
|
|
|
|
f, err := os.ReadFile(CFG.cfg)
|
2024-07-29 22:06:40 +00:00
|
|
|
tryWithExitStatus(err, 1)
|
2024-07-13 18:32:04 +00:00
|
|
|
|
2024-07-29 22:06:40 +00:00
|
|
|
tryWithExitStatus(json.Unmarshal(f, &CFG), 1)
|
2024-07-13 18:32:04 +00:00
|
|
|
if CFG.Cache.Enabled && !CFG.Proxy {
|
|
|
|
exit("Incompatible settings detected: cannot use caching media content without proxy", 1)
|
|
|
|
}
|
|
|
|
|
2024-07-29 22:06:40 +00:00
|
|
|
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
|
2024-07-13 18:32:04 +00:00
|
|
|
go InitCacheSystem()
|
2024-06-13 20:56:03 +00:00
|
|
|
}
|
2024-07-29 22:06:40 +00:00
|
|
|
devianter.UserAgent = CFG.UserAgent
|
2024-06-13 20:56:03 +00:00
|
|
|
}
|
|
|
|
}
|