-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (46 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
package main
import (
"flag"
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/rakyll/statik/fs"
_ "github.com/birkelund/vue-go-starter/statik"
)
//go:generate statik -f -src=./ui/dist
var httpAddr = flag.String("http", ":8080", "address to listen on")
func main() {
flag.Parse()
statikfs, err := fs.New()
if err != nil {
log.Fatalf("could not create statik virtual file system: %v", err)
}
// we use a chi router
r := chi.NewRouter()
r.Route("/api", func(r chi.Router) {
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
})
r.Mount("/static/", http.FileServer(statikfs))
// Since our app is a single page client-side app using HTML5 history mode,
// users will get a 404 error if they access urls directly. We fix this by
// adding a catch-all fallback route such that if the request did not match
// anything in /api/ or /static/, we serve the main index.html from the statik
// virtual file system.
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
f, err := statikfs.Open("/index.html")
if err != nil {
log.Printf("could not open index.html: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fi, err := f.Stat()
if err != nil {
log.Printf("could not stat index.html: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
http.ServeContent(w, r, "", fi.ModTime(), f)
})
log.Fatal(http.ListenAndServe(*httpAddr, r))
}