/
config.go
122 lines (98 loc) · 2.81 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
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014-2015 The DevMine authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package config takes care of the configuration file parsing.
package config
import (
"encoding/json"
"errors"
"io/ioutil"
"strings"
)
// sslModes corresponds to the SSL modes available for the connection to the
// PostgreSQL database.
// See http://www.postgresql.org/docs/9.4/static/libpq-ssl.html for details.
var sslModes = map[string]bool{
"disable": true,
"require": true,
"verify-ca": true,
"verify-full": true,
}
// Config is the main configuration structure.
type Config struct {
Database DatabaseConfig `json:"database"`
Server ServerConfig `json:"server"`
}
// DatabaseConfig is a configuration for PostgreSQL database connection
// information
type DatabaseConfig struct {
HostName string `json:"hostname"`
Port int `json:"port"`
UserName string `json:"username"`
Password string `json:"password"`
DBName string `json:"dbname"`
// Can take values: disable, require, verify-ca or verify-full
SSLMode string `json:"ssl_mode"`
}
// ServerConfig is a configuration for the server on which api-server
// is going to run on.
type ServerConfig struct {
HostName string `json:"hostname"`
Port int `json:"port"`
EnableCors bool `json:"enable_cors"`
}
// ReadConfig reads a JSON formatted configuration file, verifies the values
// of the configuration parameters and fills the Config structure.
func ReadConfig(path string) (*Config, error) {
// TODO maybe use a safer function like io.Copy
bs, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cfg := new(Config)
if err := json.Unmarshal(bs, cfg); err != nil {
return nil, err
}
if err := cfg.verify(); err != nil {
return nil, err
}
return cfg, nil
}
func (c Config) verify() error {
err := c.Database.verify()
if err != nil {
return err
}
err = c.Server.verify()
if err != nil {
return err
}
return nil
}
func (dc DatabaseConfig) verify() error {
if len(strings.Trim(dc.HostName, " ")) == 0 {
return errors.New("database hostname cannot be empty")
}
if dc.Port <= 0 {
return errors.New("database port must be greater than 0")
}
if len(strings.Trim(dc.UserName, " ")) == 0 {
return errors.New("database username cannot be empty")
}
if len(strings.Trim(dc.DBName, " ")) == 0 {
return errors.New("database name cannot be empty")
}
if _, ok := sslModes[dc.SSLMode]; !ok {
return errors.New("database can only be disable, require, verify-ca or verify-full")
}
return nil
}
func (sc ServerConfig) verify() error {
if len(strings.Trim(sc.HostName, " ")) == 0 {
return errors.New("server hostname cannot be empty")
}
if sc.Port <= 0 {
return errors.New("server port must be greater than 0")
}
return nil
}