-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
216 lines (191 loc) · 5.69 KB
/
main.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
// 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 (
"html/template"
"net/http"
"os"
"strings"
"time"
idpapi "github.com/realglobe-Inc/edo-idp-selector/api/idp"
idpdb "github.com/realglobe-Inc/edo-idp-selector/database/idp"
"github.com/realglobe-Inc/edo-idp-selector/database/session"
tadb "github.com/realglobe-Inc/edo-idp-selector/database/ta"
webdb "github.com/realglobe-Inc/edo-idp-selector/database/web"
idperr "github.com/realglobe-Inc/edo-idp-selector/error"
"github.com/realglobe-Inc/edo-idp-selector/page/idpselect"
"github.com/realglobe-Inc/edo-lib/driver"
logutil "github.com/realglobe-Inc/edo-lib/log"
"github.com/realglobe-Inc/edo-lib/rand"
"github.com/realglobe-Inc/edo-lib/server"
"github.com/realglobe-Inc/go-lib/erro"
"github.com/realglobe-Inc/go-lib/rglog"
)
func main() {
var exitCode = 0
defer func() {
if exitCode != 0 {
os.Exit(exitCode)
}
}()
defer rglog.Flush()
logutil.InitConsole(logRoot)
param, err := parseParameters(os.Args...)
if err != nil {
log.Err(erro.Unwrap(err))
log.Debug(erro.Wrap(err))
exitCode = 1
return
}
logutil.SetupConsole(logRoot, param.consLv)
if err := logutil.Setup(logRoot, param.logType, param.logLv, param); err != nil {
log.Err(erro.Unwrap(err))
log.Debug(erro.Wrap(err))
exitCode = 1
return
}
if err := serve(param); err != nil {
log.Err(erro.Unwrap(err))
log.Debug(erro.Wrap(err))
exitCode = 1
return
}
log.Info("Shut down")
}
func serve(param *parameters) (err error) {
// バックエンドの準備。
stopper := server.NewStopper()
redPools := driver.NewRedisPoolSet(param.redTimeout, param.redPoolSize, param.redPoolExpIn)
defer redPools.Close()
monPools := driver.NewMongoPoolSet(param.monTimeout)
defer monPools.Close()
// web データ。
var webDb webdb.Db
switch param.webDbType {
case "direct":
webDb = webdb.NewDirectDb()
log.Info("Get web data directly")
case "redis":
webDb = webdb.NewRedisCache(webdb.NewDirectDb(), redPools.Get(param.webDbAddr), param.webDbTag, param.webDbExpIn)
log.Info("Get web data with redis " + param.webDbAddr + "<" + param.webDbTag + ">")
default:
return erro.New("invalid web data DB type " + param.webDbType)
}
// IdP 情報。
var idpDb idpdb.Db
switch param.idpDbType {
case "mongo":
pool, err := monPools.Get(param.idpDbAddr)
if err != nil {
return erro.Wrap(err)
}
idpDb = idpdb.NewMongoDb(pool, param.idpDbTag, param.idpDbTag2, webDb)
log.Info("Use IdP info in mongodb " + param.idpDbAddr + "<" + param.idpDbTag + "." + param.idpDbTag2 + ">")
default:
return erro.New("invalid IdP DB type " + param.idpDbType)
}
// TA 情報。
var taDb tadb.Db
switch param.taDbType {
case "mongo":
pool, err := monPools.Get(param.taDbAddr)
if err != nil {
return erro.Wrap(err)
}
taDb = tadb.NewMongoDb(pool, param.taDbTag, param.taDbTag2, webDb)
log.Info("Use TA info in mongodb " + param.taDbAddr + "<" + param.taDbTag + "." + param.taDbTag2 + ">")
default:
return erro.New("invalid TA DB type " + param.taDbType)
}
// セッション。
var sessDb session.Db
switch param.sessDbType {
case "memory":
sessDb = session.NewMemoryDb()
log.Info("Save sessions in memory")
case "redis":
sessDb = session.NewRedisDb(redPools.Get(param.sessDbAddr), param.sessDbTag)
log.Info("Save sessions in redis " + param.sessDbAddr + "<" + param.sessDbTag + ">")
default:
return erro.New("invalid session DB type " + param.sessDbType)
}
var errTmpl *template.Template
if param.tmplErr != "" {
errTmpl, err = template.ParseFiles(param.tmplErr)
if err != nil {
return erro.Wrap(err)
}
}
idGen := rand.New(time.Minute)
// バックエンドの準備完了。
if param.debug {
idperr.Debug = true
}
selPage := idpselect.New(
stopper,
param.pathSelUi,
errTmpl,
param.sessLabel,
param.sessLen,
param.sessExpIn,
param.sessRefDelay,
param.sessDbExpIn,
param.ticLen,
param.ticExpIn,
idpDb,
taDb,
sessDb,
idGen,
param.cookPath,
param.cookSec,
param.debug,
)
mux := http.NewServeMux()
routes := map[string]bool{}
mux.HandleFunc(param.pathOk, idperr.WrapPage(stopper, func(w http.ResponseWriter, r *http.Request) error {
return nil
}, errTmpl))
routes[param.pathOk] = true
mux.HandleFunc(param.pathStart, selPage.HandleStart)
routes[param.pathStart] = true
mux.HandleFunc(param.pathSel, selPage.HandleSelect)
routes[param.pathSel] = true
mux.Handle(param.pathIdp, idpapi.New(
stopper,
idpDb,
param.debug,
))
routes[param.pathIdp] = true
if param.uiDir != "" {
// ファイル配信も自前でやる。
pathUi := strings.TrimRight(param.pathUi, "/") + "/"
mux.Handle(pathUi, http.StripPrefix(pathUi, http.FileServer(http.Dir(param.uiDir))))
routes[param.pathUi] = true
}
if !routes["/"] {
mux.HandleFunc("/", idperr.WrapPage(stopper, func(w http.ResponseWriter, r *http.Request) error {
return erro.Wrap(idperr.New(idperr.Invalid_request, "invalid endpoint", http.StatusNotFound, nil))
}, errTmpl))
}
// サーバー設定完了。
defer func() {
// 処理の終了待ち。
stopper.Lock()
defer stopper.Unlock()
for stopper.Stopped() {
stopper.Wait()
}
}()
return server.Serve(mux, param.socType, param.protType, param)
}