/
server.go
91 lines (84 loc) · 2.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
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
package webclient
import (
"bytes"
"encoding/json"
"github.com/celskeggs/mediator/resourcepack"
"github.com/celskeggs/mediator/util"
"github.com/pkg/errors"
"net/http"
"path"
"time"
)
func StaticHandler(modTime time.Time, nameForType string, data []byte) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
http.ServeContent(writer, request, nameForType, modTime, bytes.NewReader(data))
})
}
func ExactMatchChecker(path string, handler http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == path {
handler.ServeHTTP(writer, request)
} else {
http.Error(writer, "Not Found", 404)
}
})
}
func AttachResource(mux *http.ServeMux, path string, resource resourcepack.Resource) error {
handler := StaticHandler(resource.Modified, resource.Name, resource.Data)
if path == "/" {
handler = ExactMatchChecker("/", handler)
}
mux.Handle(path, handler)
return nil
}
func AttachResources(mux *http.ServeMux, path string, resources []string) error {
modTime := time.Now()
resourcesJson, err := json.Marshal(resources)
if err != nil {
return err
}
resourcesJs := []byte("resources = " + string(resourcesJson) + ";")
mux.HandleFunc(path, func(writer http.ResponseWriter, request *http.Request) {
http.ServeContent(writer, request, path, modTime, bytes.NewReader(resourcesJs))
})
return nil
}
func CreateMux(api ServerAPI) (*http.ServeMux, error) {
mux := http.NewServeMux()
pack := api.ResourcePack()
clientResource, err := pack.Resource("client.html")
if err != nil {
return nil, err
}
if err := AttachResource(mux, "/", clientResource); err != nil {
return nil, err
}
for _, resource := range pack.Resources {
util.FIXME("clean up this resource handling code")
if resource.IsMap() {
continue
}
basepath := "/resource/"
if resource.IsWeb() {
basepath = "/"
}
if err := AttachResource(mux, path.Join(basepath, resource.Name), resource); err != nil {
return nil, err
}
}
err = AttachResources(mux, "/resources.js", pack.Icons())
if err != nil {
return nil, err
}
wss := NewWebSocketServer(api)
mux.Handle("/websocket", wss)
return mux, nil
}
func LaunchHTTP(api ServerAPI) error {
mux, err := CreateMux(api)
if err != nil {
return errors.Wrap(err, "preparing server")
}
println("launching server... visit http://localhost:8080/")
return http.ListenAndServe(":8080", mux)
}