-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (140 loc) · 4.14 KB
/
main.go
File metadata and controls
164 lines (140 loc) · 4.14 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
const apiVersion = "1"
const apiRootDefault = "/api/v" + apiVersion
func main() {
var addr, apiRoot, contentType, ext string
var port int
var help bool
flag.BoolVar(&help, "help", false, "show this usage info")
flag.StringVar(&addr, "addr", "localhost", "server IP addres to listen at")
flag.IntVar(&port, "port", 3000, "server port to listen at")
flag.StringVar(&apiRoot, "apiroot", apiRootDefault, "API base URL, default "+apiRootDefault)
flag.StringVar(&contentType, "contentType", "application/json", "content type for responses")
flag.StringVar(&ext, "ext", "resp", "extension for data files")
flag.Parse()
fmt.Println("HTTP API test server")
if help {
flag.Usage()
os.Exit(2)
}
_, err := url.Parse(apiRoot)
if err != nil {
fmt.Println("-apiroot wrong URL format, should be realtive URL (e.g. /api/v2)")
os.Exit(1)
}
router := setupRouter(apiRoot, contentType, ext)
address := net.JoinHostPort(addr, strconv.Itoa(port))
fmt.Printf("Listening and serving HTTP on %s\n", address)
http.ListenAndServe(address, router)
}
func fetchJSONFileMiddleware(apiRoot, contentType, ext string) gin.HandlerFunc {
return func(c *gin.Context) {
apiRelativePath := strings.Replace(c.Request.URL.Path, apiRoot, "", -1)
if apiRelativePath == "" || !strings.Contains(c.Request.URL.Path, apiRoot) {
c.Next()
return
}
filename := strings.Replace(apiRelativePath, "/", "_", -1) + "." + ext
filename = strings.TrimPrefix(filename, "_")
log.Println("Read filename: " + filename)
dat, err := ioutil.ReadFile(filename)
if err != nil {
c.Error(err)
return
}
text := string(dat)
c.Header("Content-Type", contentType)
c.String(http.StatusOK, text)
c.Next()
}
}
func doAPIWelcome(apiRoot, contentType, ext string) gin.HandlerFunc {
return func(c *gin.Context) {
paths, err2 := listAPIPaths(".", ext)
if err2 != nil {
c.AbortWithError(500, errors.Errorf("Error loading API data: %s\n", err2))
}
apiPaths := []string{}
for _, path := range paths {
apiPath := apiRoot + "/" + path
apiPath = strings.Replace(apiPath, "//", "/", -1)
apiPaths = append(apiPaths, apiPath)
}
// c.String(http.StatusOK, fmt.Sprintf("HTTP API TEST Server: API root at '%s', content type '%s'", apiRoot, contentType))
c.HTML(200, "tpl", gin.H{
"apiRoot": apiRoot,
"apiPaths": apiPaths,
})
// c.JSON(200, apiPaths)
}
}
func showInfo(apiRoot string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, apiRoot)
}
}
func convertFileNameToPath(name, ext string) (path string, err error) {
if !strings.Contains(name, "."+ext) {
return "", errors.Errorf("File name '%s' has invalid extension, must be '.%s'", name, ext)
}
onlyName := strings.Replace(name, "."+ext, "", -1)
path = strings.Replace(onlyName, "_", "/", -1)
return path, nil
}
func listAPIPaths(dir, ext string) (paths []string, err error) {
paths = []string{}
files, err := ioutil.ReadDir(dir)
if err != nil {
return paths, errors.WithMessage(err, "list API paths failed")
}
for _, f := range files {
if f.IsDir() {
continue
}
filename := f.Name()
if strings.Contains(filename, "."+ext) {
convertedName, err2 := convertFileNameToPath(filename, ext)
if err2 != nil {
fmt.Println("ERROR: " + err2.Error())
continue
}
paths = append(paths, convertedName)
}
}
return paths, nil
}
func setupRouter(apiRoot, contentType, ext string) *gin.Engine {
routes := gin.Default()
routes.Use(cors.Default()) // allows all origins
routes.Use(gin.ErrorLogger())
routes.Use(fetchJSONFileMiddleware(apiRoot, contentType, ext))
var html = template.Must(template.New("tpl").Parse(`
<h1>Test API Server <sup><small>{{ .apiRoot }}</small></sup></h1>
<h2>Available paths to GET:</h2>
<ol>
{{ range .apiPaths}}
<li><a href="{{.}}">{{ . }}</a></li>
{{ end }}
</ol>
`))
routes.SetHTMLTemplate(html)
routes.GET("/", showInfo(apiRoot))
routes.GET(apiRoot, doAPIWelcome(apiRoot, contentType, ext))
return routes
}