| @@ -2,6 +2,7 @@ package app |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "encoding/json" |
| 5 | "errors" |
| 5 | 6 | "os" |
| 6 | 7 | "regexp" |
| 7 | 8 | "skunkyart/static" |
| @@ -27,19 +28,26 @@ type cacheConfig struct { |
| 27 | 28 | UpdateInterval int64 `json:"update-interval"` |
| 28 | 29 | } |
| 29 | 30 | |
| 31 | type apiCacheConfig struct { |
| 32 | Enabled bool `json:"enabled"` |
| 33 | MaxSize int64 `json:"max-size"` |
| 34 | TTL string `json:"ttl"` |
| 35 | } |
| 36 | |
| 30 | 37 | type config struct { |
| 31 | 38 | cfg string |
| 32 | | Listen string `json:"listen"` |
| 33 | | URI string `json:"uri"` |
| 34 | | Cache cacheConfig `json:"cache"` |
| 35 | | Proxy bool `json:"proxy"` |
| 36 | | Nsfw bool `json:"nsfw"` |
| 37 | | HideAI bool `json:"hide-ai"` |
| 38 | | Theme string `json:"theme"` |
| 39 | | Language string `json:"language"` |
| 40 | | UserAgent string `json:"user-agent"` |
| 41 | | DownloadProxy string `json:"download-proxy"` |
| 42 | | StaticPath string `json:"static-path"` |
| 39 | Listen string `json:"listen"` |
| 40 | URI string `json:"uri"` |
| 41 | Cache cacheConfig `json:"cache"` |
| 42 | APICache apiCacheConfig `json:"api-cache"` |
| 43 | Proxy bool `json:"proxy"` |
| 44 | Nsfw bool `json:"nsfw"` |
| 45 | HideAI bool `json:"hide-ai"` |
| 46 | Theme string `json:"theme"` |
| 47 | Language string `json:"language"` |
| 48 | UserAgent string `json:"user-agent"` |
| 49 | DownloadProxy string `json:"download-proxy"` |
| 50 | StaticPath string `json:"static-path"` |
| 43 | 51 | } |
| 44 | 52 | |
| 45 | 53 | // CFG is the running instance's configuration, holding the defaults below until |
| @@ -55,6 +63,11 @@ var CFG = config{ |
| 55 | 63 | Path: "cache", |
| 56 | 64 | UpdateInterval: 1, |
| 57 | 65 | }, |
| 66 | APICache: apiCacheConfig{ |
| 67 | Enabled: true, |
| 68 | MaxSize: 64, |
| 69 | TTL: "5i", |
| 70 | }, |
| 58 | 71 | StaticPath: "static", |
| 59 | 72 | UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", |
| 60 | 73 | Proxy: true, |
| @@ -63,6 +76,43 @@ var CFG = config{ |
| 63 | 76 | |
| 64 | 77 | var lifetimeParsed int64 |
| 65 | 78 | |
| 79 | // apiCacheTTL is api-cache.ttl parsed, set by ExecuteConfig. |
| 80 | var apiCacheTTL time.Duration |
| 81 | |
| 82 | // parseLifetime reads a duration in the config's unit syntax: a number |
| 83 | // followed by i (minutes), h (hours), d (days), w (weeks), m (30-day |
| 84 | // months) or y (360-day years). |
| 85 | func parseLifetime(s string) (time.Duration, error) { |
| 86 | if s == "" { |
| 87 | return 0, errors.New("empty lifetime") |
| 88 | } |
| 89 | numstr := regexp.MustCompile("[0-9]+").FindAllString(s, -1) |
| 90 | if len(numstr) == 0 { |
| 91 | return 0, errors.New("lifetime has no number: " + s) |
| 92 | } |
| 93 | num, _ := strconv.Atoi(numstr[len(numstr)-1]) |
| 94 | |
| 95 | day := 24 * time.Hour |
| 96 | var unit time.Duration |
| 97 | switch s[len(s)-1:] { |
| 98 | case "i": |
| 99 | unit = time.Minute |
| 100 | case "h": |
| 101 | unit = time.Hour |
| 102 | case "d": |
| 103 | unit = day |
| 104 | case "w": |
| 105 | unit = 7 * day |
| 106 | case "m": |
| 107 | unit = 30 * day |
| 108 | case "y": |
| 109 | unit = 360 * day |
| 110 | default: |
| 111 | return 0, errors.New("invalid unit specified: " + s[len(s)-1:]) |
| 112 | } |
| 113 | return unit * time.Duration(num), nil |
| 114 | } |
| 115 | |
| 66 | 116 | // checkCacheWritable creates the cache directory if it is missing and confirms |
| 67 | 117 | // this process can actually write into it, returning the error that a real cache |
| 68 | 118 | // write would hit. |
| @@ -104,29 +154,11 @@ func ExecuteConfig() { |
| 104 | 154 | } |
| 105 | 155 | |
| 106 | 156 | if CFG.Cache.Lifetime != "" { |
| 107 | | var duration int64 |
| 108 | | day := 24 * time.Hour.Milliseconds() |
| 109 | | numstr := regexp.MustCompile("[0-9]+").FindAllString(CFG.Cache.Lifetime, -1) |
| 110 | | num, _ := strconv.Atoi(numstr[len(numstr)-1]) |
| 111 | | |
| 112 | | switch unit := CFG.Cache.Lifetime[len(CFG.Cache.Lifetime)-1:]; unit { |
| 113 | | case "i": |
| 114 | | duration = time.Minute.Milliseconds() |
| 115 | | case "h": |
| 116 | | duration = time.Hour.Milliseconds() |
| 117 | | case "d": |
| 118 | | duration = day |
| 119 | | case "w": |
| 120 | | duration = day * 7 |
| 121 | | case "m": |
| 122 | | duration = day * 30 |
| 123 | | case "y": |
| 124 | | duration = day * 360 |
| 125 | | default: |
| 126 | | exit("Invalid unit specified: "+unit, 1) |
| 157 | d, err := parseLifetime(CFG.Cache.Lifetime) |
| 158 | if err != nil { |
| 159 | exit("config: cache.lifetime: "+err.Error(), 1) |
| 127 | 160 | } |
| 128 | | |
| 129 | | lifetimeParsed = duration * int64(num) |
| 161 | lifetimeParsed = d.Milliseconds() |
| 130 | 162 | } |
| 131 | 163 | // max-size is documented in megabytes. This was 1024^2, which in Go is |
| 132 | 164 | // XOR (1026), not exponentiation — so the cap was ~1000x too small. |
| @@ -152,6 +184,14 @@ func ExecuteConfig() { |
| 152 | 184 | exit("config: theme must be one of auto, dark, light; got "+CFG.Theme, 1) |
| 153 | 185 | } |
| 154 | 186 | |
| 187 | if CFG.APICache.Enabled { |
| 188 | d, err := parseLifetime(CFG.APICache.TTL) |
| 189 | if err != nil { |
| 190 | exit("config: api-cache.ttl: "+err.Error(), 1) |
| 191 | } |
| 192 | apiCacheTTL = d |
| 193 | } |
| 194 | |
| 155 | 195 | static.StaticPath = CFG.StaticPath |
| 156 | 196 | devianter.UserAgent = CFG.UserAgent |
| 157 | 197 | } |