-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (46 loc) · 1.09 KB
/
main.go
File metadata and controls
52 lines (46 loc) · 1.09 KB
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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"regexp"
"text/template"
)
var addr = flag.String("addr", ":8999", "http service address")
var homeTempl = template.Must(template.ParseFiles("home.html"))
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found", 404)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
homeTempl.Execute(w, r.Host)
}
var validPath = regexp.MustCompile("^/ws/([a-zA-Z0-9]+)$")
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
m := validPath.FindStringSubmatch(r.URL.Path)
fmt.Println(r)
if m == nil {
http.NotFound(w, r)
return
}
serveWs(w, r, m[1])
}
}
func main() {
flag.Parse()
go hub.run()
http.HandleFunc("/", serveHome)
http.HandleFunc("/ws/", makeHandler(serveWs))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}