config.go 2.2 KB

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