@@ -13,6 +13,7 @@ import (
13
13
"regexp"
14
14
"strconv"
15
15
"time"
16
+ "io/ioutil"
16
17
)
17
18
18
19
func getRequestParams (r * http.Request , urlParams map [string ]interface {}) (map [string ]interface {}, error ) {
@@ -130,6 +131,20 @@ func handler(api *Api, route *Route, version int) func(http.ResponseWriter, *htt
130
131
}
131
132
}
132
133
134
+ var indexHtml []byte
135
+
136
+ func Index (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
137
+ if len (indexHtml ) == 0 {
138
+ content , err := ioutil .ReadFile ("index.html" )
139
+ if err != nil {
140
+ indexHtml = []byte (err .Error ())
141
+ } else {
142
+ indexHtml = content
143
+ }
144
+ }
145
+ w .Write (indexHtml )
146
+ }
147
+
133
148
var db * sql.DB
134
149
135
150
func main () {
@@ -146,6 +161,9 @@ func main() {
146
161
}
147
162
defer db .Close ()
148
163
router := httprouter .New ()
164
+ if _ , err := os .Stat ("./index.html" ); err == nil {
165
+ router .GET ("/" , Index )
166
+ }
149
167
for _ , route := range api .Routes {
150
168
if route .Method == "GET" {
151
169
router .GET (route .Path , handler (api , route , 0 ))
@@ -184,6 +202,9 @@ func main() {
184
202
if len (os .Args ) > 1 {
185
203
port = os .Args [1 ]
186
204
}
205
+ if _ , err := os .Stat ("./static" ); err == nil {
206
+ router .ServeFiles ("/static/*filepath" , http .Dir ("static" ))
207
+ }
187
208
log .Fatal (http .ListenAndServe (":" + port , router ))
188
209
}
189
210
0 commit comments