From 40aa760532fa079bb74ebbe76f9fe775059edd3e Mon Sep 17 00:00:00 2001 From: iacore Date: Sun, 18 Feb 2024 14:31:19 +0000 Subject: [PATCH] Make environment variable documentation a Go file --- .env.example | 2 +- core/config/config.go | 35 +++--- ...t-of-proxies.go => Built-in Proxy List.go} | 0 doc/Environment Variables.go | 112 ++++++++++++++++++ doc/Environment-variables.md | 45 ------- doc/Hosting.md | 2 +- ...OKEN).md => How-to-get-the-pixiv-token.md} | 0 docker-compose.yml | 2 +- run.sh | 2 +- 9 files changed, 130 insertions(+), 70 deletions(-) rename doc/{list-of-proxies.go => Built-in Proxy List.go} (100%) create mode 100644 doc/Environment Variables.go delete mode 100644 doc/Environment-variables.md rename doc/{How-to-get-the-cookie-(PIXIVFE_TOKEN).md => How-to-get-the-pixiv-token.md} (100%) diff --git a/.env.example b/.env.example index 5b87092..d1b1e03 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ # -- PixivFE configuration -# See ./doc/Environment-variables.md for more details +# See ./doc/Environment\ Variables.go for more details # -- Required # PIXIVFE_TOKEN=changethis # Only set here if not using a secret diff --git a/core/config/config.go b/core/config/config.go index 607c267..b31844e 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -4,10 +4,11 @@ import ( "errors" "log" "net/url" - "os" "strconv" "strings" "time" + + "codeberg.org/vnpower/pixivfe/v2/doc" ) var GlobalServerConfig ServerConfig @@ -35,38 +36,40 @@ type ServerConfig struct { } func (s *ServerConfig) InitializeConfig() error { - _, hasDev := os.LookupEnv("PIXIVFE_DEV") + doc.CollectEnv() + + _, hasDev := doc.LookupEnv("PIXIVFE_DEV") s.InDevelopment = hasDev if s.InDevelopment { log.Printf("Set server to development mode\n") } - token, hasToken := os.LookupEnv("PIXIVFE_TOKEN") + token, hasToken := doc.LookupEnv("PIXIVFE_TOKEN") if !hasToken { log.Fatalln("PIXIVFE_TOKEN is required, but was not set.") return errors.New("PIXIVFE_TOKEN is required, but was not set.\n") } s.SetToken(token) - proxyServer, hasProxyServer := os.LookupEnv("PIXIVFE_IMAGEPROXY") + proxyServer, hasProxyServer := doc.LookupEnv("PIXIVFE_IMAGEPROXY") if hasProxyServer { s.SetProxyServer(proxyServer) } else { s.ProxyServer = url.URL{Path: "/proxy/i.pximg.net"} } - hostname, hasHostname := os.LookupEnv("PIXIVFE_HOST") + hostname, hasHostname := doc.LookupEnv("PIXIVFE_HOST") if hasHostname { log.Printf("Set TCP hostname to: %s\n", hostname) s.Host = hostname } - port, hasPort := os.LookupEnv("PIXIVFE_PORT") + port, hasPort := doc.LookupEnv("PIXIVFE_PORT") if hasPort { s.SetPort(port) } - socket, hasSocket := os.LookupEnv("PIXIVFE_UNIXSOCKET") + socket, hasSocket := doc.LookupEnv("PIXIVFE_UNIXSOCKET") if hasSocket { s.SetUnixSocket(socket) } @@ -76,24 +79,14 @@ func (s *ServerConfig) InitializeConfig() error { return errors.New("Either PIXIVFE_PORT or PIXIVFE_UNIXSOCKET has to be set.") } - userAgent, hasUserAgent := os.LookupEnv("PIXIVFE_USERAGENT") - if !hasUserAgent { - userAgent = "Mozilla/5.0" - } + userAgent, _ := doc.LookupEnv("PIXIVFE_USERAGENT") s.SetUserAgent(userAgent) - acceptLanguage, hasAcceptLanguage := os.LookupEnv("PIXIVFE_ACCEPTLANGUAGE") - if !hasAcceptLanguage { - acceptLanguage = "en-US,en;q=0.5" - } + acceptLanguage, _ := doc.LookupEnv("PIXIVFE_ACCEPTLANGUAGE") s.SetAcceptLanguage(acceptLanguage) - requestLimit, hasRequestLimit := os.LookupEnv("PIXIVFE_REQUESTLIMIT") - if hasRequestLimit { - s.SetRequestLimit(requestLimit) - } else { - s.RequestLimit = 15 - } + requestLimit, _ := doc.LookupEnv("PIXIVFE_REQUESTLIMIT") + s.SetRequestLimit(requestLimit) s.setStartingTime() s.setVersion() diff --git a/doc/list-of-proxies.go b/doc/Built-in Proxy List.go similarity index 100% rename from doc/list-of-proxies.go rename to doc/Built-in Proxy List.go diff --git a/doc/Environment Variables.go b/doc/Environment Variables.go new file mode 100644 index 0000000..17a278a --- /dev/null +++ b/doc/Environment Variables.go @@ -0,0 +1,112 @@ +// Environment Variables +// +// PixivFE's behavior is governed by those Environment Variables. + +package doc + +import ( + "log" + "os" +) + +// An environment variable is a KEY=VALUE pair +type EnvVar = struct { + Name string + Value string // available at run-time + CommonName string +} + +// All environment variables used by PixivFE +var EnvironList []EnvVar = []EnvVar{ + { + Name: "PIXIVFE_DEV", + CommonName: "development mode", + // **Required**: No + // + // Set this to anything to enable development mode, in which the server will live-reload HTML templates and disable caching. + }, + + { + Name: "PIXIVFE_HOST", + CommonName: "HOST", + // **Required**: Yes (no if PIXIVFE_UNIXSOCKET was set) + // + // Hostname/IP address to listen on. For example `PIXIVFE_HOST=localhost`. + }, + { + Name: "PIXIVFE_PORT", + CommonName: "PORT", + // **Required**: Yes (no if PIXIVFE_UNIXSOCKET was set) + // + // Port to listen on. For example `PIXIVFE_PORT=8745`. + }, + { + Name: "PIXIVFE_UNIXSOCKET", + CommonName: "UNIXSOCKET", + // **Required**: Yes (ignored if PIXIVFE_PORT was set) + // + // UNIX socket to listen on. For example `PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe`. + + }, + { + Name: "PIXIVFE_TOKEN", + CommonName: "TOKEN", + // **Required**: Yes + // + // 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. + // + // **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. + + }, + { + Name: "PIXIVFE_IMAGEPROXY", + CommonName: "IMAGEPROXY", + // **Required**: Yes + // + // See the current [list of image proxies](Built-in Proxy List.go). + // + // 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. + }, + { + Name: "PIXIVFE_REQUESTLIMIT", + CommonName: "REQUESTLIMIT", + Value: "15", + // **Required**: No + }, + { + Name: "PIXIVFE_USERAGENT", + CommonName: "USERAGENT", + Value: "Mozilla/5.0", + // **Required**: No + // + // The value of the `User-Agent` header, used to make requests to Pixiv's API. + + }, + { + Name: "PIXIVFE_ACCEPTLANGUAGE", + CommonName: "ACCEPTLANGUAGE", + Value: "en-US,en;q=0.5", + // **Required**: No + // + // 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. + }, +} + +func CollectEnv() { + for _, v := range EnvironList { + value, hasValue := os.LookupEnv(v.Name) + if hasValue { + v.Value = value + } + } +} + +func LookupEnv(key string) (string, bool) { + for _, v := range EnvironList { + if v.Name == key { + return v.Value, v.Value == "" + } + } + log.Panicf("Environment Variable Name not in `EnvironList`: %s", key) + panic("Go's type system has no Void/noreturn type...") +} diff --git a/doc/Environment-variables.md b/doc/Environment-variables.md deleted file mode 100644 index 95204fb..0000000 --- a/doc/Environment-variables.md +++ /dev/null @@ -1,45 +0,0 @@ -# Environment Variables - -PixivFE's behavior is governed by those Environment Variables. - -### PIXIVFE_TOKEN -**Required**: Yes - -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. - -**Notice:** Please read [How to get PIXIVFE_TOKEN](How-to-get-the-cookie-(PIXIVFE_TOKEN).md) to see how can you get your own token and more. - -### PIXIVFE_PORT -**Required**: Yes (no if PIXIVFE_UNIXSOCKET was set) - -Port to run on. For example `PIXIVFE_PORT=8745`. - -### PIXIVFE_UNIXSOCKET -**Required**: Yes (ignored if PIXIVFE_PORT was set) - -UNIX socket to run on. For example `PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe`. - -### PIXIVFE_IMAGEPROXY -**Required**: Yes - -See the current [list of image proxies](https://pixivfe.exozy.me/settings). - -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. - -### PIXIVFE_USERAGENT -**Required**: No - -Default: Mozilla/5.0 - -The value of the `User-Agent` header, used to make requests to Pixiv's API. - -### PIXIVFE_BASEURL -**Required**: No - -Used to generate meta tags. - -### PIXIVFE_ACCEPTLANGUAGE -**Required**: No -Default: en-US,en;q=0.5 - -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. \ No newline at end of file diff --git a/doc/Hosting.md b/doc/Hosting.md index ee37e2c..ba47dcb 100644 --- a/doc/Hosting.md +++ b/doc/Hosting.md @@ -8,7 +8,7 @@ This page covers multiple methods to install PixivFE. Using [Docker](#docker) is PixivFE needs an account token to reach the API. -You can check out [this page](./How-to-get-the-cookie-(PIXIVFE_TOKEN).md) for detailed information about how to get the token. +You can check out [this page](How-to-get-the-pixiv-token.md) for detailed information about how to get the token. ## Installation diff --git a/doc/How-to-get-the-cookie-(PIXIVFE_TOKEN).md b/doc/How-to-get-the-pixiv-token.md similarity index 100% rename from doc/How-to-get-the-cookie-(PIXIVFE_TOKEN).md rename to doc/How-to-get-the-pixiv-token.md diff --git a/docker-compose.yml b/docker-compose.yml index eec49a3..1657022 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,5 +29,5 @@ services: secrets: pixivfe_token: # Copy the contents of the `PHPSESSID` cookie into `pixivfe_token.txt` - # See ./doc/How-to-get-the-cookie-(PIXIVFE_TOKEN) for instructions + # See ./doc/How-to-get-the-pixiv-token.md for instructions file: ./docker/pixivfe_token.txt diff --git a/run.sh b/run.sh index 0a25e44..252843b 100644 --- a/run.sh +++ b/run.sh @@ -3,7 +3,7 @@ # Update the program every time you run? # git pull -# Visit ./doc/Environment-variables.md for more details +# Visit ./doc/Environment\ Variables.go for more details export PIXIVFE_TOKEN=token_123456 export PIXIVFE_IMAGEPROXY=pximg.cocomi.cf # export PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe