-
Notifications
You must be signed in to change notification settings - Fork 31
/
config.go
71 lines (58 loc) · 1.99 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
package webgo
import (
"encoding/json"
"os"
"strconv"
"time"
)
// Config is used for reading app's configuration from json file
type Config struct {
// Host is the host on which the server is listening
Host string `json:"host,omitempty"`
// Port is the port number where the server has to listen for the HTTP requests
Port string `json:"port,omitempty"`
// CertFile is the TLS/SSL certificate file path, required for HTTPS
CertFile string `json:"certFile,omitempty"`
// KeyFile is the filepath of private key of the certificate
KeyFile string `json:"keyFile,omitempty"`
// HTTPSPort is the port number where the server has to listen for the HTTP requests
HTTPSPort string `json:"httpsPort,omitempty"`
// ReadTimeout is the maximum duration for which the server would read a request
ReadTimeout time.Duration `json:"readTimeout,omitempty"`
// WriteTimeout is the maximum duration for which the server would try to respond
WriteTimeout time.Duration `json:"writeTimeout,omitempty"`
// InsecureSkipVerify is the HTTP certificate verification
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
// ShutdownTimeout is the duration in which graceful shutdown is completed
ShutdownTimeout time.Duration
// ReverseMiddleware if true, will reverse the order of execution middleware
// from the order of it was added. e.g. router.Use(m1,m2), m2 will execute first
// if ReverseMiddleware is true
ReverseMiddleware bool
}
// Load config file from the provided filepath and validate
func (cfg *Config) Load(filepath string) {
file, err := os.ReadFile(filepath)
if err != nil {
LOGHANDLER.Fatal(err)
}
err = json.Unmarshal(file, cfg)
if err != nil {
LOGHANDLER.Fatal(err)
}
err = cfg.Validate()
if err != nil {
LOGHANDLER.Fatal(ErrInvalidPort)
}
}
// Validate the config parsed into the Config struct
func (cfg *Config) Validate() error {
i, err := strconv.Atoi(cfg.Port)
if err != nil {
return ErrInvalidPort
}
if i <= 0 || i > 65535 {
return ErrInvalidPort
}
return nil
}