server.go 872 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "log"
  4. "net"
  5. "net/http"
  6. "os"
  7. "github.com/codegangsta/cli"
  8. )
  9. //go:generate -command asset go run asset.go
  10. //go:generate asset index.html
  11. //go:generate asset bundle.js
  12. type Binary struct {
  13. asset
  14. }
  15. func html(a asset) Binary {
  16. return Binary{a}
  17. }
  18. func js(a asset) Binary {
  19. return Binary{a}
  20. }
  21. func main() {
  22. app := cli.NewApp()
  23. app.Name = "p1"
  24. app.Action = run
  25. app.Flags = []cli.Flag{
  26. cli.StringFlag{Name: "port,p", Value: "0", EnvVar: "PORT"},
  27. cli.StringFlag{Name: "host", Value: "localhost"},
  28. }
  29. app.Run(os.Args)
  30. }
  31. func run(ctx *cli.Context) {
  32. l, err := net.Listen("tcp", ctx.String("host")+":"+ctx.String("port"))
  33. check(err)
  34. log.Printf("Serving at http://%s/", l.Addr())
  35. http.Handle("/", index)
  36. http.Handle("/bundle.js", bundle)
  37. check(http.Serve(l, nil))
  38. }
  39. func check(err error) {
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. }