Make environment variable documentation a Go file

This commit is contained in:
iacore 2024-02-18 14:31:19 +00:00
parent 478e02d29a
commit 40aa760532
Signed by untrusted user who does not match committer: iacore
GPG key ID: F8C16E5157A63006
9 changed files with 130 additions and 70 deletions

View file

@ -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

View file

@ -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()

View file

@ -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...")
}

View file

@ -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.

View file

@ -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

View file

@ -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

2
run.sh
View file

@ -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