Environment Variables.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // NOTE: See [How to get PIXIVFE_TOKEN](How-to-get-the-pixiv-token.md) for how to obtain your own token.
  54. },
  55. {
  56. Name: "PIXIVFE_REQUESTLIMIT",
  57. CommonName: "limit number of request per 30 seconds",
  58. // **Required**: No
  59. //
  60. // Set this to a number to enable the built-in rate limiter. For example `PIXIVFE_REQUESTLIMIT=15`.
  61. //
  62. // It might be better to enable rate limiting in the reverse proxy in front of PixivFE rather than using this.
  63. },
  64. {
  65. Name: "PIXIVFE_IMAGEPROXY",
  66. CommonName: "image proxy server",
  67. Value: BuiltinProxyUrl,
  68. Announce: true,
  69. // **Required**: No, defaults to using the built-in proxy
  70. //
  71. // NOTE: The protocol must be included in the URL, for example `https://piximg.example.com`, where `https://` is the protocol used.
  72. //
  73. // 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.
  74. // 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.
  75. },
  76. {
  77. Name: "PIXIVFE_USERAGENT",
  78. CommonName: "user agent",
  79. Value: "Mozilla/5.0",
  80. // **Required**: No
  81. //
  82. // The value of the `User-Agent` header, used to make requests to Pixiv's API.
  83. },
  84. {
  85. Name: "PIXIVFE_ACCEPTLANGUAGE",
  86. CommonName: "Accept-Language header",
  87. Value: "en-US,en;q=0.5",
  88. // **Required**: No
  89. //
  90. // 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.
  91. },
  92. }
  93. // ======================================================================
  94. // what lies below is irrelevant to you if you just want to use PixivFE
  95. // ======================================================================
  96. func CollectAllEnv() {
  97. for _, v := range EnvList {
  98. value, hasValue := os.LookupEnv(v.Name)
  99. if hasValue {
  100. v.Value = value
  101. v.Announce = true
  102. }
  103. }
  104. }
  105. func GetEnv(key string) string {
  106. value, _ := LookupEnv(key)
  107. return value
  108. }
  109. func LookupEnv(key string) (string, bool) {
  110. for _, v := range EnvList {
  111. if v.Name == key {
  112. return v.Value, v.Value != ""
  113. }
  114. }
  115. log.Panicf("Environment Variable Name not in `EnvironList`: %s", key)
  116. panic("Go's type system has no Void/noreturn type...")
  117. }
  118. func AnnounceAllEnv() {
  119. for _, v := range EnvList {
  120. if v.Announce {
  121. log.Printf("Set %s to: %s\n", v.CommonName, v.Value)
  122. }
  123. }
  124. }