-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (78 loc) · 2.67 KB
/
main.go
File metadata and controls
93 lines (78 loc) · 2.67 KB
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
package main
import (
"errors"
"flag"
"fmt"
"github.com/spf13/viper"
"net/http"
"os"
"path/filepath"
)
const defaultConfigFileName = "api-proxy.conf"
func main() {
flags := flag.NewFlagSet("flags", flag.PanicOnError)
configPathFlag := flags.String("config", "", "Path to config file")
flagsErr := flags.Parse(os.Args[1:])
if nil != flagsErr {
panic(flagsErr)
}
config, configError := readConfig(*configPathFlag)
if nil != configError {
panic(configError.Error())
}
httpServer := &http.Server{
Addr: fmt.Sprintf(":%s", config.GetString("port")),
Handler: http.NewServeMux(),
}
httpServer.Handler.(*http.ServeMux).Handle("/", http.FileServer(NewWebFileSystem(config.GetString("root"))))
httpServer.Handler.(*http.ServeMux).Handle(
"/api/login",
NewProxyHandler(config.GetString("api.scheme"), config.GetString("api.host"), &LoginProxyStrategy{}),
)
httpServer.Handler.(*http.ServeMux).Handle(
"/api/",
NewProxyHandler(config.GetString("api.scheme"), config.GetString("api.host"), &ApiProxyStrategy{}),
)
fmt.Printf("Starting web server. Port: %s. Root: '%s' \n", config.GetString("port"), config.GetString("root"))
httpListenError := httpServer.ListenAndServe()
if http.ErrServerClosed != httpListenError {
panic(httpListenError)
}
}
func readConfig(configFilePath string) (*viper.Viper, error) {
if "" == configFilePath {
workDir, workDirError := os.Getwd()
if workDirError != nil {
return nil, errors.New("Failed to determine working directory. Error: " + workDirError.Error())
}
configFilePath = workDir + "/" + defaultConfigFileName
}
// Check config file exists
configFileInfo, configFileInfoError := os.Stat(configFilePath)
if nil != configFileInfoError {
return nil, errors.New(fmt.Sprintf(
"Error while reading config file. Path: %s. Operation: %s. Error: %s",
configFileInfoError.(*os.PathError).Path,
configFileInfoError.(*os.PathError).Op,
configFileInfoError.Error(),
))
}
// Check config file is not a directory
if configFileInfo.IsDir() {
return nil, errors.New(fmt.Sprintf("Invalid config file path. %s is a directory", configFilePath))
}
// Check config file has supported format
configFileExt := filepath.Ext(configFileInfo.Name())
if !stringInSlice(configFileExt[1:], viper.SupportedExts) {
return nil, errors.New(fmt.Sprintf("Not supported config file format. %s not supported", configFileExt))
}
configFileName := configFileInfo.Name()[0 : len(configFileInfo.Name())-len(configFileExt)]
config := viper.New()
config.AddConfigPath(filepath.Dir(configFilePath))
config.SetConfigName(configFileName)
configError := config.ReadInConfig()
if nil != configError {
return nil, configError
}
return config, nil
}