-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (36 loc) · 1023 Bytes
/
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
package main
import (
"github.com/mlctrez/zipbackpack/httpfs"
"log"
"net/http"
)
func main() {
var sf http.FileSystem
var err error
// this error handling is atypical and shown
// only for purposes of how to load the filesystem both
// with web assets attached to a binary and as a directory
if sf, err = httpfs.NewStaticFileSystem(""); err != nil {
sf, err = httpfs.NewStaticFileSystem("web")
if err != nil {
panic(err)
}
}
handler := http.FileServer(sf)
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
// http.FileServer does a permanent redirect of /index.html to /
// so for this path we serve /_index.html to avoid a redirect loop
if r.URL.Path == "/" {
r.URL.Path = "/_index.html"
}
handler.ServeHTTP(rw, r)
})
http.HandleFunc("/api/", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("api handler for " + r.URL.Path))
})
log.Println("listening on :8080")
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}