feat(backend): implement task creation and admin web
This commit is contained in:
@@ -10,11 +10,13 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
HTTPAddressEnvironment = "CMROUBAO_HTTP_ADDR"
|
||||
DatabasePathEnvironment = "CMROUBAO_DATABASE_PATH"
|
||||
HTTPAddressEnvironment = "CMROUBAO_HTTP_ADDR"
|
||||
DatabasePathEnvironment = "CMROUBAO_DATABASE_PATH"
|
||||
AssetDirectoryEnvironment = "CMROUBAO_ASSET_DIR"
|
||||
|
||||
defaultHTTPAddress = "127.0.0.1:8080"
|
||||
defaultDatabasePath = "var/cmroubao.db"
|
||||
defaultHTTPAddress = "127.0.0.1:8080"
|
||||
defaultDatabasePath = "var/cmroubao.db"
|
||||
defaultAssetDirectory = "var/assets"
|
||||
)
|
||||
|
||||
type LookupEnvironment func(string) (string, bool)
|
||||
@@ -22,6 +24,7 @@ type LookupEnvironment func(string) (string, bool)
|
||||
type Config struct {
|
||||
HTTPAddress string
|
||||
DatabasePath string
|
||||
AssetDirectory string
|
||||
ReadHeaderTimeout time.Duration
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
@@ -53,9 +56,23 @@ func Load(lookup LookupEnvironment) (Config, error) {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
assetDirectory, err := environmentValue(
|
||||
lookup,
|
||||
AssetDirectoryEnvironment,
|
||||
defaultAssetDirectory,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
assetDirectory, err = validatedAssetDirectory(assetDirectory)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
return Config{
|
||||
HTTPAddress: httpAddress,
|
||||
DatabasePath: filepath.Clean(databasePath),
|
||||
AssetDirectory: assetDirectory,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
@@ -65,6 +82,24 @@ func Load(lookup LookupEnvironment) (Config, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func validatedAssetDirectory(path string) (string, error) {
|
||||
if strings.ContainsRune(path, '\x00') {
|
||||
return "", errors.New(
|
||||
AssetDirectoryEnvironment + " contains an invalid character",
|
||||
)
|
||||
}
|
||||
cleanPath := filepath.Clean(path)
|
||||
volumeRoot := filepath.VolumeName(cleanPath) + string(filepath.Separator)
|
||||
if cleanPath == "." ||
|
||||
cleanPath == string(filepath.Separator) ||
|
||||
cleanPath == volumeRoot {
|
||||
return "", errors.New(
|
||||
AssetDirectoryEnvironment + " must be a dedicated directory",
|
||||
)
|
||||
}
|
||||
return cleanPath, nil
|
||||
}
|
||||
|
||||
func LoadDatabasePath(lookup LookupEnvironment) (string, error) {
|
||||
databasePath, err := environmentValue(
|
||||
lookup,
|
||||
|
||||
Reference in New Issue
Block a user