main.go 844 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "flag"
  7. "log"
  8. "net/http"
  9. )
  10. var addr = flag.String("addr", ":8080", "http service address")
  11. func serveHome(w http.ResponseWriter, r *http.Request) {
  12. log.Println(r.URL)
  13. if r.URL.Path != "/" {
  14. http.Error(w, "Not found", 404)
  15. return
  16. }
  17. if r.Method != "GET" {
  18. http.Error(w, "Method not allowed", 405)
  19. return
  20. }
  21. http.ServeFile(w, r, "home.html")
  22. }
  23. func main() {
  24. flag.Parse()
  25. hub := newHub()
  26. go hub.run()
  27. http.HandleFunc("/", serveHome)
  28. http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
  29. serveWs(hub, w, r)
  30. })
  31. err := http.ListenAndServe(*addr, nil)
  32. if err != nil {
  33. log.Fatal("ListenAndServe: ", err)
  34. }
  35. }