Add UploadedImagesDir config setting

This commit is contained in:
Jannis Mattheis 2018-03-30 19:13:09 +02:00 committed by Jannis Mattheis
parent b738115b72
commit dfe242b0cd
3 changed files with 33 additions and 2 deletions

View File

@ -66,6 +66,7 @@ defaultuser: # on database creation, gotify creates an admin user
name: admin # the username of the default user name: admin # the username of the default user
pass: admin # the password of the default user pass: admin # the password of the default user
passstrength: 10 # the bcrypt password strength (higher = better but also slower) passstrength: 10 # the bcrypt password strength (higher = better but also slower)
uploadedimagesdir: images # the directory for storing uploaded images
``` ```
### Environment ### Environment
@ -86,6 +87,7 @@ GOTIFY_DATABASE_CONNECTION=gotify.db
GOTIFY_DEFAULTUSER_NAME=admin GOTIFY_DEFAULTUSER_NAME=admin
GOTIFY_DEFAULTUSER_PASS=admin GOTIFY_DEFAULTUSER_PASS=admin
GOTIFY_PASSSTRENGTH=10 GOTIFY_PASSSTRENGTH=10
GOTIFY_UPLOADEDIMAGESDIR=images
``` ```
### Database ### Database

View File

@ -1,6 +1,11 @@
package config package config
import "github.com/jinzhu/configor" import (
"path/filepath"
"strings"
"github.com/jinzhu/configor"
)
// Configuration is stuff that can be configured externally per env variables or config file (config.yml). // Configuration is stuff that can be configured externally per env variables or config file (config.yml).
type Configuration struct { type Configuration struct {
@ -28,12 +33,20 @@ type Configuration struct {
Name string `default:"admin"` Name string `default:"admin"`
Pass string `default:"admin"` Pass string `default:"admin"`
} }
PassStrength int `default:"10"` PassStrength int `default:"10"`
UploadedImagesDir string `default:"images"`
} }
// Get returns the configuration extracted from env variables or config file. // Get returns the configuration extracted from env variables or config file.
func Get() *Configuration { func Get() *Configuration {
conf := new(Configuration) conf := new(Configuration)
configor.New(&configor.Config{ENVPrefix: "GOTIFY"}).Load(conf, "config.yml", "/etc/gotify/config.yml") configor.New(&configor.Config{ENVPrefix: "GOTIFY"}).Load(conf, "config.yml", "/etc/gotify/config.yml")
addTrailingSlashToPaths(conf)
return conf return conf
} }
func addTrailingSlashToPaths(conf *Configuration) {
if !strings.HasSuffix(conf.UploadedImagesDir, "/") && !strings.HasSuffix(conf.UploadedImagesDir, "\\") {
conf.UploadedImagesDir = conf.UploadedImagesDir + string(filepath.Separator)
}
}

View File

@ -4,6 +4,8 @@ import (
"os" "os"
"testing" "testing"
"path/filepath"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -18,6 +20,20 @@ func TestConfigEnv(t *testing.T) {
os.Unsetenv("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS") os.Unsetenv("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS")
} }
func TestAddSlash(t *testing.T) {
os.Setenv("GOTIFY_UPLOADEDIMAGESDIR", "../data/images")
conf := Get()
assert.Equal(t, "../data/images"+string(filepath.Separator), conf.UploadedImagesDir)
os.Unsetenv("GOTIFY_UPLOADEDIMAGESDIR")
}
func TestNotAddSlash(t *testing.T) {
os.Setenv("GOTIFY_UPLOADEDIMAGESDIR", "../data/")
conf := Get()
assert.Equal(t, "../data/", conf.UploadedImagesDir)
os.Unsetenv("GOTIFY_UPLOADEDIMAGESDIR")
}
func TestConfigFile(t *testing.T) { func TestConfigFile(t *testing.T) {
file, err := os.Create("config.yml") file, err := os.Create("config.yml")
defer func() { defer func() {