main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "net"
  6. "net/http"
  7. "net/rpc"
  8. "os"
  9. "github.com/codegangsta/negroni"
  10. "github.com/elazarl/go-bindata-assetfs"
  11. "golang.org/x/net/websocket"
  12. )
  13. var (
  14. host = flag.String("host", "localhost", "The hostname/ip to listen on.")
  15. port = flag.String("port", "0", "The port number to listen on.")
  16. )
  17. //go:generate gopherjs build -m -o public/js/app.js github.com/cryptix/exp/humbleRpcTodo/frontend
  18. //go:generate go-bindata -prefix=public public/...
  19. func main() {
  20. flag.Parse()
  21. mux := http.NewServeMux()
  22. // Rpc!
  23. rpc.Register(&TodoService{})
  24. mux.Handle("/rpc-websocket", websocket.Handler(func(conn *websocket.Conn) {
  25. conn.PayloadType = websocket.BinaryFrame
  26. rpc.ServeConn(conn)
  27. }))
  28. mux.Handle("/", http.FileServer(&assetfs.AssetFS{
  29. Asset: Asset,
  30. AssetDir: AssetDir,
  31. }))
  32. n := negroni.New()
  33. n.Use(negroni.NewRecovery())
  34. n.Use(negroni.NewLogger())
  35. n.UseHandler(mux)
  36. // Start the server
  37. if *port == "0" {
  38. *port = os.Getenv("PORT")
  39. }
  40. l, err := net.Listen("tcp", *host+":"+*port)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. log.Printf("Serving at http://%s/", l.Addr())
  45. if err := http.Serve(l, n); err != nil {
  46. log.Fatal(err)
  47. }
  48. }