-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
65 lines (56 loc) · 2 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2023 Maxime Malgorn. All rights reserved.
// Use of this source code is governed by a MIT-style.
// The license can be found in the LICENSE file.
package main
import (
"flag"
"os"
"strconv"
"strings"
)
var (
portPtr = 8043
basePath = "/svr/http"
pathPrefix = "/"
clientSecret = ""
enableLogging = false
cacheDirList []string
exposeDirList []string
allowOriginList []string
)
func LookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func LookupEnvOrInt(key string, defaultVal int) int {
if val, ok := os.LookupEnv(key); ok {
v, _ := strconv.Atoi(val)
return v
}
return defaultVal
}
func LookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, _ := strconv.ParseBool(val)
return v
}
return defaultVal
}
func Configure() {
var cacheDirString string
var exposeDirString string
var allowOriginString string
flag.IntVar(&portPtr, "port", LookupEnvOrInt("PORT", portPtr), "listening port")
flag.StringVar(&basePath, "base-path", LookupEnvOrString("BASE_PATH", basePath), "directory where all files are stored")
flag.StringVar(&clientSecret, "client-secret", LookupEnvOrString("CLIENT_SECRET", clientSecret), "secret key used to access privileged routes")
flag.BoolVar(&enableLogging, "enable-logging", LookupEnvOrBool("ENABLE_LOGGING", enableLogging), "enable log request")
flag.StringVar(&cacheDirString, "caching-directories", LookupEnvOrString("CACHING_DIRECTORIES", ""), "list of directories to cache")
flag.StringVar(&exposeDirString, "expose-directories", LookupEnvOrString("EXPOSE_DIRECTORIES", ""), "list of directories to expose")
flag.StringVar(&allowOriginString, "allow-origins", LookupEnvOrString("ALLOW_ORIGINS", ""), "list of origins to allow using CORS")
flag.Parse()
cacheDirList = strings.Split(cacheDirString, ";")
exposeDirList = strings.Split(exposeDirString, ";")
allowOriginList = strings.Split(allowOriginString, ";")
}