-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (97 loc) · 2.58 KB
/
main.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"sort"
"strings"
"github.com/gorilla/mux"
)
func rawData(c *config) (map[string]interface{}, error) {
f := c.File
raw, err := ioutil.ReadFile(f)
if err != nil {
msg := fmt.Sprintf("Unable to read '%s'. %s", f, err.Error())
return map[string]interface{}{}, errors.New(msg)
}
var jsonData interface{}
err = json.Unmarshal(raw, &jsonData)
if err != nil {
msg := fmt.Sprintf("Unable to parse JSON. %s", err.Error)
return map[string]interface{}{}, errors.New(msg)
}
return jsonData.(map[string]interface{}), nil
}
func urlData(parent string, data map[string]interface{}) map[string]string {
results := map[string]string{}
if parent == "/" {
keys := []string{}
for k, _ := range data {
keys = append(keys, k)
}
sort.Strings(keys)
results[parent] = strings.Join(keys, "\n") + "\n"
}
for k, v := range data {
val := reflect.Indirect(reflect.ValueOf(v))
if val.Kind() == reflect.Interface {
val = val.Elem()
}
switch val.Kind() {
case reflect.String:
results[parent+k+"/"] = val.String()
case reflect.Map:
mapKeysOutput := []string{}
for subKey, subVal := range val.Interface().(map[string]interface{}) {
subVal := reflect.Indirect(reflect.ValueOf(subVal))
switch subVal.Kind() {
case reflect.Map:
mapKeysOutput = append(mapKeysOutput, subKey+"/")
case reflect.String:
mapKeysOutput = append(mapKeysOutput, subKey)
}
}
sort.Strings(mapKeysOutput)
results[parent+k+"/"] = strings.Join(mapKeysOutput, "\n")
result := urlData(parent+k+"/", val.Interface().(map[string]interface{}))
for resultKey, resultVal := range result {
results[resultKey] = resultVal
}
}
}
return results
}
func setupRouter(urlData map[string]string) http.Handler {
urls := []string{}
for k, _ := range urlData {
urls = append(urls, k)
}
sort.Strings(urls)
router := mux.NewRouter()
for _, k := range urls {
url := k
data := urlData[url]
router.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, data)
}).Methods("GET")
}
return router
}
func main() {
config := parseCliArgs()
data, err := rawData(config)
if err != nil {
fmt.Printf("Unable to load raw data. Error: %s\n", err.Error())
os.Exit(1)
}
bindInfo := config.Address + ":" + config.Port
os.Stdout.Write([]byte("Listening on " + bindInfo + "\n"))
err = http.ListenAndServe(config.Address + ":" + config.Port, setupRouter(urlData("/", data)))
if err != nil {
fmt.Printf("Unable to serve data. Error: %s", err.Error())
}
}