-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
57 lines (49 loc) · 1.43 KB
/
server.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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"github.com/krrrr38/gpshow/utils"
"github.com/toqueteos/webbrowser"
)
type staticBinaryHandler struct{}
// Server for picture-show slides
func Server(port int, slidemaker SlideMaker) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write(slidemaker.HTML())
})
for _, path := range []string{"css", "js", "images"} {
dir := fmt.Sprintf("/%s/", path)
http.Handle(dir, http.StripPrefix(dir, http.FileServer(http.Dir(path))))
}
http.Handle("/"+AssetsPath, &staticBinaryHandler{})
utils.Log("info", fmt.Sprintf("starting show on http://localhost:%d", port))
utils.Log("info", "Press ctrl+c to stop")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
utils.Log("info", "thank you for watching...")
os.Exit(1)
}
}()
webbrowser.Open(fmt.Sprintf("http://localhost:%d", port))
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func (h *staticBinaryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
bytes, err := Asset("resources" + path)
if err == nil {
if strings.HasSuffix(path, ".css") {
w.Header().Set("Content-Type", "text/css")
} else if strings.HasSuffix(path, ".js") {
w.Header().Set("Content-Type", "text/javascript")
}
w.WriteHeader(http.StatusOK)
w.Write(bytes)
}
}