Add support for environmental vars

Add support for configuring pastad via environmenal variables.
Adds also a test for the usage of the environmental variables.
This commit is contained in:
Felix Niederwanger 2021-12-31 13:36:06 +01:00
parent 7821b31904
commit d70424d08f
Signed by: phoenix
GPG key ID: 6E77A590E3F6D71C
3 changed files with 90 additions and 13 deletions

View file

@ -54,6 +54,22 @@ Then create a `pastad.toml` file using the provided example (`pastad.toml.exampl
./pastad
### environment variables
In addition to the config file, `pastad` can also be configured via environmental variables. This might be useful for running pasta as a container without a dedicated config file. Supported environmental variables are:
| Key | Description |
|-----|-------------|
| `PASTA_BASEURL` | Base URL for the pasta instance |
| `PASTA_PASTADIR` | Data directory for pastas |
| `PASTA_BINDADDR` | Address to bind the server to |
| `PASTA_MAXSIZE` | Maximum size (in Bytes) for new pastas |
| `PASTA_CHARACTERS` | Number of characters for new pastas |
| `PASTA_MIMEFILE` | MIME file |
| `PASTA_EXPIRE` | Default expiration time (in seconds) |
| `PASTA_CLEANUP` | Seconds between cleanup cycles |
| `PASTA_REQUESTDELAY` | Delay between requests from the same host in milliseconds |
### Build docker image
make docker

View file

@ -69,6 +69,67 @@ func CreateDefaultConfigfile(filename string) error {
return file.Close()
}
// SetDefaults sets the default values to a config instance
func (cf *Config) SetDefaults() {
cf.BaseUrl = "http://localhost:8199"
cf.PastaDir = "pastas/"
cf.BindAddr = "127.0.0.1:8199"
cf.MaxPastaSize = 1024 * 1024 * 25 // Default max size: 25 MB
cf.PastaCharacters = 8 // Note: Never use less than 8 characters!
cf.MimeTypesFile = "mime.types"
cf.DefaultExpire = 0
cf.CleanupInterval = 60 * 60 // Default cleanup is once per hour
cf.RequestDelay = 0 // By default not spam protection (Assume we are in safe environment)
}
// getenv reads a given environmental variable and returns it's value if present or defval if not present or empty
func getenv(key string, defval string) string {
val := os.Getenv(key)
if val == "" {
return defval
}
return val
}
// getenv reads a given environmental variable as integer and returns it's value if present or defval if not present or empty
func getenv_i(key string, defval int) int {
val := os.Getenv(key)
if val == "" {
return defval
}
if i32, err := strconv.Atoi(val); err != nil {
return defval
} else {
return i32
}
}
// getenv reads a given environmental variable as integer and returns it's value if present or defval if not present or empty
func getenv_i64(key string, defval int64) int64 {
val := os.Getenv(key)
if val == "" {
return defval
}
if i64, err := strconv.ParseInt(val, 10, 64); err != nil {
return defval
} else {
return i64
}
}
// ReadEnv reads the environmental variables and sets the config accordingly
func (cf *Config) ReadEnv() {
cf.BaseUrl = getenv("PASTA_BASEURL", cf.BaseUrl)
cf.PastaDir = getenv("PASTA_PASTADIR", cf.PastaDir)
cf.BindAddr = getenv("PASTA_BINDADDR", cf.BindAddr)
cf.MaxPastaSize = getenv_i64("PASTA_MAXSIZE", cf.MaxPastaSize)
cf.PastaCharacters = getenv_i("PASTA_CHARACTERS", cf.PastaCharacters)
cf.MimeTypesFile = getenv("PASTA_MIMEFILE", cf.MimeTypesFile)
cf.DefaultExpire = getenv_i64("PASTA_EXPIRE", cf.DefaultExpire)
cf.CleanupInterval = getenv_i("PASTA_CLEANUP", cf.CleanupInterval)
cf.RequestDelay = getenv_i64("PASTA_REQUESTDELAY", cf.RequestDelay)
}
func (pc *ParserConfig) ApplyTo(cf *Config) {
if pc.BaseURL != nil && *pc.BaseURL != "" {
cf.BaseUrl = *pc.BaseURL
@ -609,16 +670,8 @@ func cleanupThread() {
}
func main() {
// Set defaults
cf.BaseUrl = "http://localhost:8199"
cf.PastaDir = "pastas/"
cf.BindAddr = "127.0.0.1:8199"
cf.MaxPastaSize = 1024 * 1024 * 25 // Default max size: 25 MB
cf.PastaCharacters = 8 // Note: Never use less than 8 characters!
cf.MimeTypesFile = "mime.types"
cf.DefaultExpire = 0
cf.CleanupInterval = 60 * 60 // Default cleanup is once per hour
cf.RequestDelay = 0 // By default not spam protection (Assume we are in safe environment)
cf.SetDefaults()
cf.ReadEnv()
delays = make(map[string]int64)
// Parse program arguments for config
parseCf := ParserConfig{}

View file

@ -1,7 +1,5 @@
#!/bin/bash
#
# Summary: Function test for pasta
#
# Summary: Function test for pasta & pastad
PASTAS=~/.pastas.dat # pasta client dat file
PASTAS_TEMP="" # temp file, if present
@ -47,6 +45,16 @@ curl -o testfile2 $link
diff testfile testfile2
echo "Testfile 2 matches"
## Second pasta server with environment variables
echo "Testing environment variables ... "
PASTA_BASEURL=pastas PASTA_BINDADDR=127.0.0.1:8201 PASTA_CHARACTERS=12 ../pastad -m ../mime.types &
SECONDPID=$!
sleep 2 # TODO: Don't do sleep here you lazy ... :-)
link2=`../pasta -r http://127.0.0.1:8201 < testfile`
curl -o testfile_second $link
diff testfile testfile_second
kill $SECONDPID
## Test spam protection
echo "Testing spam protection ... "
../pasta -r http://127.0.0.1:8200 testfile >/dev/null