main.go 858 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2013 Gary Burd. 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. "text/template"
  10. )
  11. var addr = flag.String("addr", ":8080", "http service address")
  12. var homeTempl = template.Must(template.ParseFiles("home.html"))
  13. func serveHome(w http.ResponseWriter, r *http.Request) {
  14. if r.URL.Path != "/" {
  15. http.Error(w, "Not found", 404)
  16. return
  17. }
  18. if r.Method != "GET" {
  19. http.Error(w, "Method nod allowed", 405)
  20. return
  21. }
  22. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  23. homeTempl.Execute(w, r.Host)
  24. }
  25. func main() {
  26. flag.Parse()
  27. go h.run()
  28. http.HandleFunc("/", serveHome)
  29. http.HandleFunc("/ws", serveWs)
  30. err := http.ListenAndServe(*addr, nil)
  31. if err != nil {
  32. log.Fatal("ListenAndServe: ", err)
  33. }
  34. }