-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
48 lines (40 loc) · 1.09 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
package main
import (
"log"
"net/http"
"strings"
"github.com/tockn/singo/handler"
"github.com/tockn/singo/manager"
"github.com/tockn/singo/repository/mem"
)
func serve(addr string) error {
roomRepo := mem.NewRoomRepository()
man := manager.NewManager(roomRepo)
han := handler.NewHandler(man)
http.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {
han.CreateConnection(w, r)
})
log.Println("running...")
return http.ListenAndServe(addr, nil)
}
func serveWithDemo(addr string) error {
roomRepo := mem.NewRoomRepository()
man := manager.NewManager(roomRepo)
han := handler.NewHandler(man)
fs := http.FileServer(http.Dir("./demo/dist"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path)
if r.URL.Path == "/connect" {
han.CreateConnection(w, r)
return
}
sp := strings.Split(r.URL.Path, "/")
if len(sp) > 2 && (sp[1] == "js" || sp[1] == "css") {
fs.ServeHTTP(w, r)
return
}
http.ServeFile(w, r, "./demo/dist/index.html")
})
log.Println("running...(with demo)")
return http.ListenAndServe(addr, nil)
}