-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (53 loc) · 1.27 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
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"embed"
"flag"
"github.com/chowder/jam/pkg"
"io/fs"
"log"
"net/http"
"time"
)
var (
//go:embed static/dist
static embed.FS
)
var addr = flag.String("addr", ":8080", "http service address")
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "home.html")
}
func main() {
flag.Parse()
hub := pkg.NewHub()
go hub.Run()
staticRoot, err := fs.Sub(static, "static/dist")
if err != nil {
log.Fatal(err)
}
http.Handle("/", http.FileServer(http.FS(staticRoot)))
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
log.Print("Incoming connection from ", r.RemoteAddr)
pkg.ServeWs(hub, w, r)
})
server := &http.Server{
Addr: *addr,
ReadHeaderTimeout: 3 * time.Second,
}
log.Println("Serving on", server.Addr)
err = server.ListenAndServe()
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}