-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter.go
231 lines (191 loc) · 7.81 KB
/
parameter.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2015 realglobe, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/realglobe-Inc/go-lib/erro"
"github.com/realglobe-Inc/go-lib/rglog/level"
)
type parameters struct {
// 画面ログ。
consLv level.Level
// 追加ログ。
logType string
logLv level.Level
// ファイルログ。
logPath string
logSize int64
logNum int
// fluentd ログ。
logAddr string
logTag string
// ソケット。
socType string
// UNIX ソケット。
socPath string
// TCP ソケット。
socPort int
// プロトコル。
protType string
// URI
pathOk string
pathStart string
pathSel string
pathIdp string
// UI 用 HTML を提供する URI。
pathUi string
pathSelUi string
// UI 用 HTML を置くディレクトリパス。
uiDir string
tmplErr string
// セッション。
sessLabel string
sessLen int
sessExpIn time.Duration
sessRefDelay time.Duration
sessDbExpIn time.Duration
// チケット。
ticLen int
ticExpIn time.Duration
// バックエンドの指定。
// redis
redTimeout time.Duration
redPoolSize int
redPoolExpIn time.Duration
// mongodb
monTimeout time.Duration
// web データ DB。
webDbType string
webDbAddr string
webDbTag string
webDbExpIn time.Duration
// IdP 情報 DB。
idpDbType string
idpDbAddr string
idpDbTag string
idpDbTag2 string
// TA 情報 DB。
taDbType string
taDbAddr string
taDbTag string
taDbTag2 string
// セッション DB。
sessDbType string
sessDbAddr string
sessDbTag string
// その他のオプション。
// Set-Cookie の Path。
cookPath string
// Set-Cookie を Secure にするか。
cookSec bool
debug bool
// テスト用。
shutCh chan struct{}
}
func parseParameters(args ...string) (param *parameters, err error) {
const label = "edo-idp-selector"
flags := flag.NewFlagSet(label+" parameters", flag.ExitOnError)
flags.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintln(os.Stderr, " "+args[0]+" [{FLAG}...]")
fmt.Fprintln(os.Stderr, "FLAG:")
flags.PrintDefaults()
}
param = ¶meters{}
flags.Var(level.Var(¶m.consLv, level.INFO), "consLv", "Console log level")
flags.StringVar(¶m.logType, "logType", "", "Extra log: Type")
flags.Var(level.Var(¶m.logLv, level.ALL), "logLv", "Extra log: Level")
flags.StringVar(¶m.logPath, "logPath", filepath.Join(filepath.Dir(os.Args[0]), "log", label+".log"), "Extra log: File path")
flags.Int64Var(¶m.logSize, "logSize", 10*(1<<20) /* 10 MB */, "Extra log: File size limit")
flags.IntVar(¶m.logNum, "logNum", 10, "Extra log: File number limit")
flags.StringVar(¶m.logAddr, "logAddr", "localhost:24224", "Extra log: Fluentd address")
flags.StringVar(¶m.logTag, "logTag", label, "Extra log: Fluentd tag")
flags.StringVar(¶m.socType, "socType", "tcp", "Socket type")
flags.StringVar(¶m.socPath, "socPath", filepath.Join(filepath.Dir(os.Args[0]), "run", label+".soc"), "Unix socket path")
flags.IntVar(¶m.socPort, "socPort", 1603, "TCP socket port")
flags.StringVar(¶m.protType, "protType", "http", "Protocol type")
flags.StringVar(¶m.pathOk, "pathOk", "/ok", "OK URI")
flags.StringVar(¶m.pathStart, "pathStart", "/start", "Start URI")
flags.StringVar(¶m.pathSel, "pathSel", "/select", "Account select URI")
flags.StringVar(¶m.pathIdp, "pathIdp", "/api/info/issuer", "ID provider info URI")
flags.StringVar(¶m.pathUi, "pathUi", "/ui", "UI URI")
flags.StringVar(¶m.pathSelUi, "pathSelUi", "/ui/select.html", "Account selection UI URI")
flags.StringVar(¶m.uiDir, "uiDir", "", "UI file directory")
flags.StringVar(¶m.tmplErr, "tmplErr", "", "Error UI template")
flags.StringVar(¶m.sessLabel, "sessLabel", "Idp-Selector", "Session ID label")
flags.IntVar(¶m.sessLen, "sessLen", 30, "Session ID length")
flags.DurationVar(¶m.sessExpIn, "sessExpIn", 7*24*time.Hour, "Session expiration duration")
flags.DurationVar(¶m.sessRefDelay, "sessRefDelay", 24*time.Hour, "Session refresh delay")
flags.DurationVar(¶m.sessDbExpIn, "sessDbExpIn", 14*24*time.Hour, "Session keep duration")
flags.IntVar(¶m.ticLen, "ticLen", 10, "Ticket length")
flags.DurationVar(¶m.ticExpIn, "ticExpIn", 30*time.Minute, "Ticket expiration duration")
flags.DurationVar(¶m.redTimeout, "redTimeout", 30*time.Second, "redis timeout duration")
flags.IntVar(¶m.redPoolSize, "redPoolSize", 10, "redis pool size")
flags.DurationVar(¶m.redPoolExpIn, "redPoolExpIn", time.Minute, "redis connection keep duration")
flags.DurationVar(¶m.monTimeout, "monTimeout", 30*time.Second, "mongodb timeout duration")
flags.StringVar(¶m.webDbType, "webDbType", "redis", "Web data DB type")
flags.StringVar(¶m.webDbAddr, "webDbAddr", "localhost:6379", "Web data DB address")
flags.StringVar(¶m.webDbTag, "webDbTag", "web", "Web data DB tag")
flags.DurationVar(¶m.webDbExpIn, "webDbExpIn", 7*24*time.Hour, "Web data keep duration")
flags.StringVar(¶m.idpDbType, "idpDbType", "mongo", "IdP DB type")
flags.StringVar(¶m.idpDbAddr, "idpDbAddr", "localhost", "IdP DB address")
flags.StringVar(¶m.idpDbTag, "idpDbTag", "edo", "IdP DB tag")
flags.StringVar(¶m.idpDbTag2, "idpDbTag2", "idp", "IdP DB sub tag")
flags.StringVar(¶m.taDbType, "taDbType", "mongo", "TA DB type")
flags.StringVar(¶m.taDbAddr, "taDbAddr", "localhost", "TA DB address")
flags.StringVar(¶m.taDbTag, "taDbTag", "edo", "TA DB tag")
flags.StringVar(¶m.taDbTag2, "taDbTag2", "ta", "TA DB sub tag")
flags.StringVar(¶m.sessDbType, "sessDbType", "redis", "Session DB type")
flags.StringVar(¶m.sessDbAddr, "sessDbAddr", "localhost:6379", "Session DB address")
flags.StringVar(¶m.sessDbTag, "sessDbTag", "session", "Session DB tag")
flags.StringVar(¶m.cookPath, "cookPath", "/", "Path in Set-Cookie")
flags.BoolVar(¶m.cookSec, "cookSec", true, "Secure flag in Set-Cookie")
flags.BoolVar(¶m.debug, "debug", true, "Debug mode")
var config string
flags.StringVar(&config, "c", "", "Config file path")
// 実行引数を読んで、設定ファイルを指定させてから、
// 設定ファイルを読んで、また実行引数を読む。
flags.Parse(args[1:])
if config != "" {
if buff, err := ioutil.ReadFile(config); err != nil {
if !os.IsNotExist(err) {
return nil, erro.Wrap(err)
}
log.Warn("Config file " + config + " is not exist")
} else {
flags.Parse(strings.Fields(string(buff)))
}
}
flags.Parse(args[1:])
if l := len(flags.Args()); l > 0 {
log.Warn("Ignore extra parameters ", flags.Args())
}
return param, nil
}
func (param *parameters) LogFilePath() string { return param.logPath }
func (param *parameters) LogFileLimit() int64 { return param.logSize }
func (param *parameters) LogFileNumber() int { return param.logNum }
func (param *parameters) LogFluentdAddress() string { return param.logAddr }
func (param *parameters) LogFluentdTag() string { return param.logTag }
func (param *parameters) SocketPort() int { return param.socPort }
func (param *parameters) SocketPath() string { return param.socPath }
// テスト用。
// 使うときは手動で param.shutCh = make(chan struct{}, 5) する。
func (param *parameters) ShutdownChannel() chan struct{} { return param.shutCh }