/
main.go
66 lines (55 loc) · 1.46 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"path/filepath"
flag "github.com/spf13/pflag"
)
func main() {
var dirPath = flag.StringP("dir", "d", "./", "Please select json files directory.")
var port = flag.IntP("port", "p", 8080, "Please select if you want to use other port number except for 8080.")
var noExt = flag.Bool("noext", false, "Please select if you need not to extenstion(.json).")
flag.Parse()
if len(*dirPath) == 0 {
log.Fatal("You must set json file path.")
}
jsonFiles := getFiles(*dirPath)
log.Printf("http://localhost:%d\n", *port)
var urlPath string
for _, file := range jsonFiles {
bytes, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
if *noExt {
count := len(file[:len(file)-len(filepath.Ext(file))])
file = file[0:count]
}
urlPath = filepath.Join("/", file)
log.Printf("See browse -> http://localhost:%d%s", *port, urlPath)
http.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, string(bytes))
})
}
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}
func getFiles(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, getFiles(filepath.Join(dir, file.Name()))...)
continue
}
if path.Ext(file.Name()) == ".json" {
paths = append(paths, filepath.Join(dir, file.Name()))
}
}
return paths
}