-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
104 lines (85 loc) · 2.14 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"github.com/BurntSushi/toml"
)
var (
// this is status code order based on which we will be ordering the
StatusCodeOrder = map[int]int{
http.StatusInternalServerError: 7,
http.StatusUnauthorized: 6,
http.StatusBadRequest: 5,
http.StatusForbidden: 4,
http.StatusNotFound: 3,
http.StatusCreated: 2,
http.StatusOK: 1,
}
)
func LoadConfig(path string, host string, port int) {
Conf.Apps = make(map[string]App)
Conf.Apps["default"] = App{
Host: host,
Port: port,
MockPath: "test",
}
if path != "" {
if _, err := toml.DecodeFile(path, &Conf); err != nil {
log.Fatal("failed to load Config : ", err)
}
}
}
func LoadMockConfig(path string) {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal("failed to load open Mock Directory : ", err)
}
var routes []Route
for _, v := range files {
if filepath.Ext(v.Name()) != ".json" {
log.Println("error: invalid file format: ", v.Name())
continue
}
filePath := path + string(os.PathSeparator) + v.Name()
switch v.Name() {
case CORSFile:
var cors CORSOption
readRequestMockConfig(filePath, &cors)
Mock.CORSOptions = cors
case NotFoundResponseFile:
var notFound Response
readRequestMockConfig(filePath, ¬Found)
Mock.NotFound = notFound
default:
var route Route
readRequestMockConfig(filePath, &route)
routes = append(routes, route)
evals := route.Evaluators
sort.Slice(evals, func(i, j int) bool {
return StatusCodeOrder[evals[i].Response.StatusCode] >
StatusCodeOrder[evals[j].Response.StatusCode]
})
route.Evaluators = evals
}
}
if len(routes) == 0 {
log.Println("failed to load route configurations")
}
Mock.Routes = routes
}
func readRequestMockConfig(filePath string, dest interface{}) {
file, err := ioutil.ReadFile(filePath)
if err != nil {
log.Println("failed to load Mock Config : ", err)
return
}
if err := json.Unmarshal(file, dest); err != nil {
log.Println("failed to un marshal Mock Config : ", err)
return
}
}