-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
85 lines (72 loc) · 1.74 KB
/
app.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/websocket"
"github.com/jagandecapri/vision/server"
"encoding/json"
"github.com/jagandecapri/vision/tree"
)
var upgrader = websocket.Upgrader{} // use default options
type Socket struct{
URI string
}
type Data struct{
points []tree.Point
}
func BootServer(data chan server.HttpData) {
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", serveTemplate)
hub := server.NewHub()
go hub.Run()
go func(data chan server.HttpData, hub *server.Hub){
for{
select {
case tmp := <-data:
json, err := json.Marshal(tmp)
if err != nil{
log.Println("JSON encode err: ", err)
}
hub.Broadcast <- json
default:
}
}
}(data, hub)
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
server.ServeWs(hub, w, r)
})
log.Println("Listening...")
go func() {
http.ListenAndServe(":3001", nil)
}()
}
func serveTemplate(w http.ResponseWriter, r *http.Request) {
lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", filepath.Clean(r.URL.Path))
// Return a 404 if the template doesn't exist
info, err := os.Stat(fp)
if err != nil {
log.Println("err file: ", err)
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
// Return a 404 if the request is for a directory
if info.IsDir() {
log.Println("request to dir")
http.NotFound(w, r)
return
}
tmpl := template.Must(template.ParseFiles(lp, fp))
data := Socket{URI: "ws://"+r.Host+"/echo"}
log.Println(data)
if err := tmpl.ExecuteTemplate(w, "layout", data); err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}