-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
70 lines (63 loc) · 1.49 KB
/
web.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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
)
type W http.HandlerFunc
func (f W) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f(w, r)
}
type WebView struct {
server *http.Server
// TODO: not replaced safely
revision int
source string
}
func NewWebView() *WebView {
wv := &WebView{}
wv.server = &http.Server{
Addr: ":8421",
Handler: W(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/shader/fragment" {
if r.URL.RawQuery != "" {
rev, _ := strconv.Atoi(r.URL.RawQuery)
for i := 0; i < 300 && rev == wv.revision; i++ {
time.Sleep(100 * time.Millisecond)
}
}
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("X-Revision", fmt.Sprintf("%d", wv.revision))
w.WriteHeader(200)
w.Write([]byte(wv.source))
return
} else if page, ok := webSource[r.URL.Path]; ok {
// TODO: store content-type
if r.URL.Path == "" {
w.Header().Set("Content-Type", "text/html")
} else if strings.HasSuffix(r.URL.Path, ".js") {
w.Header().Set("Content-Type", "text/javascript")
}
w.WriteHeader(200)
w.Write([]byte(page))
} else {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(404)
w.Write([]byte("<!DOCTYPE html><p>Not found"))
}
}),
}
log.Println("Starting webserver on port 8421")
go wv.server.ListenAndServe()
return wv
}
func (w *WebView) Update(source string) {
w.source = source
w.revision += 1
if w.revision < 0 {
w.revision = 1
}
}