forked from masanori840816/webappsample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (67 loc) · 2.04 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
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"sync"
)
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
serverUrl string
}
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// "sync.Once" executes only one time.
t.once.Do(func() {
// "Must()" wraps "ParseFiles()" results, so I can put it into "templateHandler.templ" directly
t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
})
t.templ.Execute(w, t.serverUrl)
}
func main() {
settings, err := LoadAppSettings()
if err != nil {
log.Println(err.Error())
}
log.Println(settings.URL)
target := getStrippingTargetPrefix(settings.URL)
hub := *newSSEHub()
go hub.run()
if len(target) > 0 {
http.Handle(fmt.Sprintf("/%s/css/", target), http.StripPrefix(fmt.Sprintf("/%s", target), http.FileServer(http.Dir("templates"))))
http.Handle(fmt.Sprintf("/%s/js/", target), http.StripPrefix(fmt.Sprintf("/%s", target), http.FileServer(http.Dir("templates"))))
http.HandleFunc(fmt.Sprintf("/%s/sse/message", target), func(w http.ResponseWriter, r *http.Request) {
sendSSEMessage(w, r, &hub)
})
http.HandleFunc(fmt.Sprintf("/%s/sse/", target), func(w http.ResponseWriter, r *http.Request) {
registerSSEClient(w, r, &hub)
})
} else {
http.Handle("/css/", http.FileServer(http.Dir("templates")))
http.Handle("/js/", http.FileServer(http.Dir("templates")))
http.HandleFunc("/sse/message", func(w http.ResponseWriter, r *http.Request) {
sendSSEMessage(w, r, &hub)
})
http.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
registerSSEClient(w, r, &hub)
})
}
http.Handle("/", &templateHandler{filename: "index.html", serverUrl: settings.URL})
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
func getStrippingTargetPrefix(url string) string {
sURL := strings.Split(url, "/")
if len(sURL) <= 3 {
return ""
}
for i := len(sURL) - 1; i >= 3; i-- {
if sURL[i] != "" {
return sURL[i]
}
}
return ""
}