2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2017-02-13 21:21:13 +03:00
|
|
|
"html/template"
|
2013-10-16 20:41:47 +04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":8080", "http service address")
|
2016-05-18 17:56:16 +03:00
|
|
|
var homeTemplate = template.Must(template.ParseFiles("home.html"))
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
2016-07-19 02:08:34 +03:00
|
|
|
log.Println(r.URL)
|
2013-10-16 20:41:47 +04:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.Error(w, "Not found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "GET" {
|
2014-06-28 00:08:22 +04:00
|
|
|
http.Error(w, "Method not allowed", 405)
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2016-05-18 17:56:16 +03:00
|
|
|
homeTemplate.Execute(w, r.Host)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2016-07-19 02:08:34 +03:00
|
|
|
hub := newHub()
|
2016-05-27 07:00:24 +03:00
|
|
|
go hub.run()
|
2013-10-16 20:41:47 +04:00
|
|
|
http.HandleFunc("/", serveHome)
|
2016-07-19 02:08:34 +03:00
|
|
|
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serveWs(hub, w, r)
|
|
|
|
})
|
2013-10-16 20:41:47 +04:00
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
}
|