environment_variables.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Environment Variables
  2. //
  3. // PixivFE's behavior is governed by those Environment Variables.
  4. //
  5. // If you don't have time to read all these, set only `PIXIVFE_TOKEN=XXXXX PIXIVFE_PORT=8282`.
  6. // For development, set in addition `PIXIVFE_DEV=true`.
  7. // How to get PIXIVFE_TOKEN is described in How-to-get-the-pixiv-token.md.
  8. package config
  9. import (
  10. "log"
  11. "os"
  12. )
  13. // An environment variable is a KEY=VALUE pair
  14. type EnvVar = struct {
  15. Name string
  16. CommonName string
  17. Value string // available at run-time
  18. Announce bool
  19. }
  20. // All environment variables used by PixivFE
  21. var EnvList []*EnvVar = []*EnvVar{
  22. {
  23. Name: "PIXIVFE_DEV",
  24. CommonName: "development mode",
  25. // **Required**: No
  26. //
  27. // 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`.
  28. },
  29. {
  30. Name: "PIXIVFE_HOST",
  31. CommonName: "TCP hostname",
  32. // **Required**: No (ignored if PIXIVFE_UNIXSOCKET was set)
  33. //
  34. // Hostname/IP address to listen on. For example `PIXIVFE_HOST=localhost`.
  35. },
  36. {
  37. Name: "PIXIVFE_PORT",
  38. CommonName: "TCP port",
  39. // **Required**: Yes (no if PIXIVFE_UNIXSOCKET was set)
  40. //
  41. // Port to listen on. For example `PIXIVFE_PORT=8745`.
  42. },
  43. {
  44. Name: "PIXIVFE_UNIXSOCKET",
  45. CommonName: "UNIX socket path",
  46. // **Required**: Yes (ignored if PIXIVFE_PORT was set)
  47. //
  48. // UNIX socket to listen on. For example `PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe`.
  49. },
  50. {
  51. Name: "PIXIVFE_TOKEN",
  52. CommonName: "Pixiv token",
  53. // **Required**: Yes
  54. //
  55. // 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.
  56. //
  57. // NOTE: See [How to get PIXIVFE_TOKEN](How-to-get-the-pixiv-token.md) for how to obtain your own token.
  58. },
  59. {
  60. Name: "PIXIVFE_REQUESTLIMIT",
  61. CommonName: "Limit number of request per 30 seconds",
  62. // **Required**: No
  63. //
  64. // Set this to a number to enable the built-in rate limiter. For example `PIXIVFE_REQUESTLIMIT=15`.
  65. //
  66. // It might be better to enable rate limiting in the reverse proxy in front of PixivFE rather than using this.
  67. },
  68. {
  69. Name: "PIXIVFE_IMAGEPROXY",
  70. CommonName: "image proxy server",
  71. Value: BuiltinProxyUrl,
  72. Announce: true,
  73. // **Required**: No, defaults to using the built-in proxy
  74. //
  75. // NOTE: The protocol must be included in the URL, for example `https://piximg.example.com`, where `https://` is the protocol used.
  76. //
  77. // The URL of the image proxy server. Pixiv does not allow you to fetch their images directly, requiring `Referer: https://www.pixiv.net/` to be included in the HTTP request headers. For example, trying to directly access this [image](https://i.pximg.net/img-original/img/2023/06/06/20/30/01/108783513_p0.png) returns HTTP 403 Forbidden.
  78. // This can be circumvented by using a reverse proxy that adds the required `Referer` HTTP request header to the HTTP request for the image. You can [host an image proxy server](Hosting-an-image-proxy-server-for-Pixiv.md), or see the [list of public image proxies](Built-in Proxy List.go). If you wish not to, or unable to get images directly from Pixiv, set this variable.
  79. },
  80. {
  81. Name: "PIXIVFE_USERAGENT",
  82. CommonName: "user agent",
  83. Value: "Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0",
  84. // **Required**: No
  85. //
  86. // The value of the `User-Agent` header, used to make requests to Pixiv's API.
  87. },
  88. {
  89. Name: "PIXIVFE_ACCEPTLANGUAGE",
  90. CommonName: "Accept-Language header",
  91. Value: "en-US,en;q=0.5",
  92. // **Required**: No
  93. //
  94. // 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.
  95. },
  96. {
  97. Name: "PIXIVFE_PROXY_CHECK_INTERVAL",
  98. CommonName: "proxy check interval",
  99. Value: "480",
  100. // **Required**: No
  101. //
  102. // The interval in minutes between proxy checks. Defaults to 480 minutes (8 hours) if not set.
  103. // You can disable this by setting the value to 0. Then, proxies will only be checked once at server initialization.
  104. },
  105. }
  106. // ======================================================================
  107. // what lies below is irrelevant to you if you just want to use PixivFE
  108. // ======================================================================
  109. func CollectAllEnv() {
  110. for _, v := range EnvList {
  111. value, hasValue := os.LookupEnv(v.Name)
  112. if hasValue {
  113. v.Value = value
  114. v.Announce = true
  115. }
  116. }
  117. }
  118. func GetEnv(key string) string {
  119. value, _ := LookupEnv(key)
  120. return value
  121. }
  122. func LookupEnv(key string) (string, bool) {
  123. for _, v := range EnvList {
  124. if v.Name == key {
  125. return v.Value, v.Value != ""
  126. }
  127. }
  128. log.Panicf("Environment Variable Name not in `EnvironList`: %s", key)
  129. panic("Go's type system has no Void/noreturn type...")
  130. }
  131. func AnnounceAllEnv() {
  132. for _, v := range EnvList {
  133. if v.Announce {
  134. log.Printf("Set %s to: %s\n", v.CommonName, v.Value)
  135. }
  136. }
  137. }