-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
61 lines (55 loc) · 1.49 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
// Copyright (c) 2019 Faye Amacker. All rights reserved.
// Use of this source code is governed by Apache License 2.0 found in the LICENSE file.
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"io"
"os"
"github.com/fxamacker/webauthn"
)
// config has configuration data from config file and environment variables.
type config struct {
WebAuthn *webauthn.Config
Origin string
SessionKey []byte
DBConnString string
RedisNetwork string
RedisAddr string
RedisPwd string
}
func newConfig(configFile io.Reader) (*config, error) {
var err error
c := &config{}
if err := json.NewDecoder(configFile).Decode(c); err != nil {
return nil, errors.New("failed to decode config file: " + err.Error())
}
if err := c.WebAuthn.Valid(); err != nil {
return nil, err
}
if c.Origin == "" {
return nil, errors.New("origin is empty")
}
c.SessionKey, err = base64.RawStdEncoding.DecodeString(os.Getenv("SESSION_KEY"))
if err != nil {
return nil, errors.New("failed to base64 decode session key: " + err.Error())
}
if len(c.SessionKey) == 0 {
return nil, errors.New("SESSION_KEY is empty")
}
c.DBConnString = os.Getenv("DB_CONNSTRING")
if c.DBConnString == "" {
return nil, errors.New("DB_CONNSTRING is empty")
}
c.RedisNetwork = os.Getenv("REDIS_NETWORK")
if c.RedisNetwork == "" {
c.RedisNetwork = "tcp"
}
c.RedisAddr = os.Getenv("REDIS_ADDR")
if c.RedisAddr == "" {
c.RedisAddr = "localhost:6379"
}
c.RedisPwd = os.Getenv("REDIS_PWD")
return c, nil
}