-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.go
135 lines (108 loc) · 2.95 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
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2021.
*
* 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 (
"fmt"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
)
type Config struct {
Server struct {
// Host is the server host name
Host string `yaml:"host"`
// Port is the local machine TCP Port to bind the HTTP Server to
Port string `yaml:"port"`
// SitePath string is the location to serve up static files
SitePath string `yaml:"sitePath"`
// TLS provides the TLS tuning configuration
TLS struct {
CertFile string `yaml:"certificate"`
KeyFile string `yaml:"key"`
} `yaml:"tls"`
} `yaml:"server"`
}
func GetValidatedConfig(configPath string) (*Config, error) {
var config *Config
file, err := os.Open(configPath)
defer func() {
if err := file.Close(); err != nil {
log.Printf("Error closing config file: %s", err)
}
}()
if err == nil {
config, err = ReadConfig(file)
}
if err != nil || config == nil {
log.Printf("Reverting to defaults... error with config file: %s", err)
config = &Config{}
config.Server.Port = "8080"
config.Server.SitePath = "./www"
}
err = ValidateConfig(config)
if err != nil {
return nil, err
}
return config, nil
}
func ReadConfig(reader io.Reader) (*Config, error) {
config := &Config{}
d := yaml.NewDecoder(reader)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func ValidateConfig(config *Config) error {
fileInfo, err := os.Stat(config.Server.SitePath)
if err != nil {
return err
}
if !fileInfo.IsDir() {
return fmt.Errorf("'%s' is not a directory, cannot start server", config.Server.SitePath)
}
tlsCertSpecified := len(config.Server.TLS.CertFile) > 0
tlsKeySpecified := len(config.Server.TLS.KeyFile) > 0
if !tlsCertSpecified && !tlsKeySpecified {
return nil
}
if tlsCertSpecified && !tlsKeySpecified {
return fmt.Errorf("TLS Certificate specified, but no key file was provided")
}
if tlsKeySpecified && !tlsCertSpecified {
return fmt.Errorf("TLS Key specified, but no certificate was provided")
}
if err = ValidateOptionalFile(config.Server.TLS.KeyFile); err != nil {
return err
}
if err = ValidateOptionalFile(config.Server.TLS.CertFile); err != nil {
return err
}
return nil
}
func ValidateOptionalFile(path string) error {
if len(path) > 0 {
fileInfo, err := os.Stat(path)
if err != nil {
return err
}
if fileInfo.IsDir() {
return fmt.Errorf("'%s' is directory, not a file", path)
}
}
return nil
}