Environment Variables.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Environment Variables
  2. //
  3. // PixivFE's behavior is governed by those Environment Variables.
  4. package doc
  5. import (
  6. "log"
  7. "os"
  8. )
  9. // An environment variable is a KEY=VALUE pair
  10. type EnvVar = struct {
  11. Name string
  12. CommonName string
  13. Value string // available at run-time
  14. Announce bool
  15. }
  16. // All environment variables used by PixivFE
  17. var EnvList []*EnvVar = []*EnvVar{
  18. {
  19. Name: "PIXIVFE_DEV",
  20. CommonName: "development mode",
  21. // **Required**: No
  22. //
  23. // Set this to anything to enable development mode, in which the server will live-reload HTML templates and disable caching. For example, `PIXIVFE_DEV=true`.
  24. },
  25. {
  26. Name: "PIXIVFE_HOST",
  27. CommonName: "TCP hostname",
  28. // **Required**: No (ignored if PIXIVFE_UNIXSOCKET was set)
  29. //
  30. // Hostname/IP address to listen on. For example `PIXIVFE_HOST=localhost`.
  31. },
  32. {
  33. Name: "PIXIVFE_PORT",
  34. CommonName: "TCP port",
  35. // **Required**: Yes (no if PIXIVFE_UNIXSOCKET was set)
  36. //
  37. // Port to listen on. For example `PIXIVFE_PORT=8745`.
  38. },
  39. {
  40. Name: "PIXIVFE_UNIXSOCKET",
  41. CommonName: "UNIX socket path",
  42. // **Required**: Yes (ignored if PIXIVFE_PORT was set)
  43. //
  44. // UNIX socket to listen on. For example `PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe`.
  45. },
  46. {
  47. Name: "PIXIVFE_TOKEN",
  48. CommonName: "Pixiv token",
  49. // **Required**: Yes
  50. //
  51. // Authorization is required to fully access Pixiv's Ajax API. This variable will store your Pixiv's account cookie, which will be used by PixivFE for authorization.
  52. //
  53. // **Notice:** Please read [How to get PIXIVFE_TOKEN](How-to-get-the-pixiv-token.md) to see how can you get your own token and more.
  54. },
  55. {
  56. Name: "PIXIVFE_REQUESTLIMIT",
  57. CommonName: "request limit per 30 seconds",
  58. Value: "15",
  59. // **Required**: No
  60. },
  61. {
  62. Name: "PIXIVFE_IMAGEPROXY",
  63. CommonName: "image proxy server",
  64. Value: "/proxy/i.pximg.net", // built-in proxy route
  65. Announce: true,
  66. // **Required**: No, default to using built-in proxy
  67. //
  68. // See the current [list of image proxies](Built-in Proxy List.go).
  69. //
  70. // The address to proxy images. Pixiv does not allow you to get their images normally. For example, this [image](https://i.pximg.net/img-original/img/2023/06/06/20/30/01/108783513_p0.png). We could bypass this anyway by using NGINX and reverse proxy. [You can host an image proxy server if you want](Hosting-an-image-proxy-server-for-Pixiv.md). If you wish not to, or unable to get images directly from Pixiv, set this variable.
  71. },
  72. {
  73. Name: "PIXIVFE_USERAGENT",
  74. CommonName: "user agent",
  75. Value: "Mozilla/5.0",
  76. // **Required**: No
  77. //
  78. // The value of the `User-Agent` header, used to make requests to Pixiv's API.
  79. },
  80. {
  81. Name: "PIXIVFE_ACCEPTLANGUAGE",
  82. CommonName: "Accept-Language header",
  83. Value: "en-US,en;q=0.5",
  84. // **Required**: No
  85. //
  86. // The value of the `Accept-Language` header, used to make requests to Pixiv's API. You can change the response's language with this one.
  87. },
  88. }
  89. // ======================================================================
  90. // what lies below is irrelevant to you if you just want to use PixivFE
  91. // ======================================================================
  92. func CollectAllEnv() {
  93. for _, v := range EnvList {
  94. value, hasValue := os.LookupEnv(v.Name)
  95. if hasValue {
  96. v.Value = value
  97. v.Announce = true
  98. }
  99. }
  100. }
  101. func GetEnv(key string) string {
  102. value, _ := LookupEnv(key)
  103. return value
  104. }
  105. func LookupEnv(key string) (string, bool) {
  106. for _, v := range EnvList {
  107. if v.Name == key {
  108. return v.Value, v.Value != ""
  109. }
  110. }
  111. log.Panicf("Environment Variable Name not in `EnvironList`: %s", key)
  112. panic("Go's type system has no Void/noreturn type...")
  113. }
  114. func AnnounceAllEnv() {
  115. for _, v := range EnvList {
  116. if v.Announce {
  117. log.Printf("Set %s to: %s\n", v.CommonName, v.Value)
  118. }
  119. }
  120. }