-
Notifications
You must be signed in to change notification settings - Fork 51
/
config.go
109 lines (98 loc) · 3.36 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"flag"
"fmt"
"html/template"
"log"
"strings"
)
const (
defaultServicePort = 55555
defaultServiceAddr = "127.0.0.1"
defaultPageSize = 20
initBasketCapacity = 200
maxBasketCapacity = 2000
defaultDatabaseType = DbTypeMemory
serviceOldAPIPath = "baskets"
serviceAPIPath = "api"
serviceUIPath = "web"
serviceName = "request-baskets"
basketNamePattern = `^[\w\d\-_\.]{1,250}$`
sourceCodeURL = "https://github.com/darklynx/request-baskets"
)
// ServerConfig describes server configuration.
type ServerConfig struct {
ServerPort int
ServerAddr string
InitCapacity int
MaxCapacity int
PageSize int
MasterToken string
DbType string
DbFile string
DbConnection string
Baskets []string
PathPrefix string
Mode string
Theme string
ThemeCSS template.HTML
}
type arrayFlags []string
func (v *arrayFlags) String() string {
return strings.Join(*v, ",")
}
func (v *arrayFlags) Set(value string) error {
*v = append(*v, value)
return nil
}
// CreateConfig creates server configuration base on application command line arguments
func CreateConfig() *ServerConfig {
var port = flag.Int("p", defaultServicePort, "HTTP service port")
var address = flag.String("l", defaultServiceAddr, "HTTP listen address")
var initCapacity = flag.Int("size", initBasketCapacity, "Initial basket size (capacity)")
var maxCapacity = flag.Int("maxsize", maxBasketCapacity, "Maximum allowed basket size (max capacity)")
var pageSize = flag.Int("page", defaultPageSize, "Default page size")
var masterToken = flag.String("token", "", "Master token, random token is generated if not provided")
var dbType = flag.String("db", defaultDatabaseType, fmt.Sprintf(
"Baskets storage type: \"%s\" - in-memory, \"%s\" - Bolt DB, \"%s\" - SQL database",
DbTypeMemory, DbTypeBolt, DbTypeSQL))
var dbFile = flag.String("file", "./baskets.db", "Database location, only applicable for file or SQL databases")
var dbConnection = flag.String("conn", "", "Database connection string for SQL databases, if undefined \"file\" argument is considered")
var prefix = flag.String("prefix", "", "Service URL path prefix")
var mode = flag.String("mode", ModePublic, fmt.Sprintf(
"Service mode: \"%s\" - any visitor can create a new basket, \"%s\" - baskets creation requires master token",
ModePublic, ModeRestricted))
var theme = flag.String("theme", ThemeStandard, fmt.Sprintf(
"CSS theme for web UI, supported values: %s, %s, %s",
ThemeStandard, ThemeAdaptive, ThemeFlatly))
var baskets arrayFlags
flag.Var(&baskets, "basket", "Name of a basket to auto-create during service startup (can be specified multiple times)")
flag.Parse()
var token = *masterToken
if len(token) == 0 {
token, _ = GenerateToken()
log.Printf("[info] generated master token: %s", token)
}
return &ServerConfig{
ServerPort: *port,
ServerAddr: *address,
InitCapacity: *initCapacity,
MaxCapacity: *maxCapacity,
PageSize: *pageSize,
MasterToken: token,
DbType: *dbType,
DbFile: *dbFile,
DbConnection: *dbConnection,
Baskets: baskets,
PathPrefix: normalizePrefix(*prefix),
Mode: *mode,
Theme: *theme,
ThemeCSS: toThemeCSS(*theme)}
}
func normalizePrefix(prefix string) string {
if (len(prefix) > 0) && (prefix[0] != '/') {
return "/" + prefix
} else {
return prefix
}
}