1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-01 07:26:23 -07:00

Use constants for environment variables, check GOCHAN_CONFIG value before standard gochan.json locations

This commit is contained in:
Eggbertx 2025-06-26 10:10:17 -07:00
parent 80bc25135a
commit ccad76ff28
4 changed files with 29 additions and 9 deletions

View file

@ -64,9 +64,9 @@ func main() {
fatalEv.Discard()
fatalEv = gcutil.LogFatal() // reset fatalEv to use log file
testIP := os.Getenv("GC_TESTIP")
testIP := os.Getenv(gcutil.TestingIPEnvVar)
if testIP != "" {
gcutil.LogInfo().Str("GC_TESTIP", testIP).
gcutil.LogInfo().Str(gcutil.TestingIPEnvVar, testIP).
Msg("Custom testing IP address set from environment variable")
}

View file

@ -21,14 +21,19 @@ const (
InitialSetupNotStarted
InitialSetupComplete
DirFileMode fs.FileMode = 0775
// DirFileMode is the default file mode for directories created by gochan
DirFileMode fs.FileMode = 0775
// NormalFileMode is the default file mode for files created by gochan
NormalFileMode fs.FileMode = 0664
// ConfigPathEnvVar is the environment variable used to set the path to gochan.js if it is set
ConfigPathEnvVar = "GOCHAN_CONFIG"
)
var (
uid int
gid int
standardConfigSearchPaths = []string{"gochan.json", "/usr/local/etc/gochan/gochan.json", "/etc/gochan/gochan.json"}
standardConfigSearchPaths = []string{"gochan.json", "/usr/local/etc/gochan/gochan.json", "/opt/homebrew/etc/gochan/gochan.json", "/etc/gochan/gochan.json"}
initialSetupStatus InitialSetupStatus = InitialSetupStatusUnknown
)
@ -55,12 +60,24 @@ func (iv *InvalidValueError) Error() string {
return str
}
// GetGochanJSONPath returns the location of gochan.json, searching in the working directory,
// /usr/local/etc/gochan, and /etc/gochan in that order. If it is not found, it returns an empty string.
// GetGochanJSONPath returns the location of gochan.json. If the GOCHAN_CONFIG environment variable is set,
// it returns the value, whether or not a file exists at that location. Otherwise, it searches for gochan.json
// in the following locations, returning the first one found:
//
// ./gochan.json (working directory)
// /usr/local/etc/gochan/gochan.json
// /opt/homebrew/etc/gochan/gochan.json
// /etc/gochan/gochan.json
//
// If gochan.json is not found, it returns an empty string.
func GetGochanJSONPath() string {
if cfgPath != "" {
return cfgPath
}
jsonPath := os.Getenv(ConfigPathEnvVar)
if jsonPath != "" {
return jsonPath
}
return gcutil.FindResource(standardConfigSearchPaths...)
}

View file

@ -94,7 +94,7 @@ func TestGetRealIP(t *testing.T) {
req.Header.Set("HTTP_CF_CONNECTING_IP", cfIP)
assert.Equal(t, cfIP, GetRealIP(req))
t.Setenv("GC_TESTIP", testIP)
t.Setenv(TestingIPEnvVar, testIP)
assert.Equal(t, testIP, GetRealIP(req))
}

View file

@ -24,6 +24,9 @@ import (
const (
// DefaultMaxAge is used for cookies that have an invalid or unset max age (default is 1 month)
DefaultMaxAge = time.Hour * 24 * 30
// TestingIPEnvVar is the environment variable used in development for assining to incoming IP addresses if it is set
TestingIPEnvVar = "GOCHAN_TESTIP"
)
var (
@ -102,11 +105,11 @@ func GetFormattedFilesize(size float64) string {
return fmt.Sprintf("%0.2fGB", size/1024.0/1024.0/1024.0)
}
// GetRealIP checks the GC_TESTIP environment variable as well as HTTP_CF_CONNCTING_IP
// GetRealIP checks the GOCHAN_TESTIP environment variable as well as HTTP_CF_CONNCTING_IP
// and X-Forwarded-For HTTP headers to get a potentially obfuscated IP address, before
// getting the request's reported remote address
func GetRealIP(request *http.Request) string {
ip, ok := os.LookupEnv("GC_TESTIP")
ip, ok := os.LookupEnv(TestingIPEnvVar)
if ok {
return ip
}