web.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package cmd
  4. import (
  5. "fmt"
  6. "net/http"
  7. "path"
  8. "gopkg.in/macaron.v1"
  9. "github.com/grafana/grafana/pkg/api"
  10. "github.com/grafana/grafana/pkg/api/static"
  11. "github.com/grafana/grafana/pkg/log"
  12. "github.com/grafana/grafana/pkg/middleware"
  13. "github.com/grafana/grafana/pkg/plugins"
  14. "github.com/grafana/grafana/pkg/setting"
  15. )
  16. func newMacaron() *macaron.Macaron {
  17. macaron.Env = setting.Env
  18. m := macaron.New()
  19. m.Use(middleware.Logger())
  20. m.Use(macaron.Recovery())
  21. if setting.EnableGzip {
  22. m.Use(middleware.Gziper())
  23. }
  24. for _, route := range plugins.StaticRoutes {
  25. pluginRoute := path.Join("/public/plugins/", route.PluginId)
  26. log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Directory)
  27. mapStatic(m, route.Directory, "", pluginRoute)
  28. }
  29. mapStatic(m, setting.StaticRootPath, "", "public")
  30. mapStatic(m, setting.StaticRootPath, "css", "css")
  31. mapStatic(m, setting.StaticRootPath, "img", "img")
  32. mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
  33. mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
  34. m.Use(macaron.Renderer(macaron.RenderOptions{
  35. Directory: path.Join(setting.StaticRootPath, "views"),
  36. IndentJSON: macaron.Env != macaron.PROD,
  37. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  38. }))
  39. if setting.EnforceDomain {
  40. m.Use(middleware.ValidateHostHeader(setting.Domain))
  41. }
  42. m.Use(middleware.GetContextHandler())
  43. m.Use(middleware.Sessioner(&setting.SessionOptions))
  44. return m
  45. }
  46. func mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
  47. headers := func(c *macaron.Context) {
  48. c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
  49. }
  50. if setting.Env == setting.DEV {
  51. headers = func(c *macaron.Context) {
  52. c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
  53. }
  54. }
  55. m.Use(httpstatic.Static(
  56. path.Join(rootDir, dir),
  57. httpstatic.StaticOptions{
  58. SkipLogging: true,
  59. Prefix: prefix,
  60. AddHeaders: headers,
  61. },
  62. ))
  63. }
  64. func StartServer() {
  65. var err error
  66. m := newMacaron()
  67. api.Register(m)
  68. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  69. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
  70. switch setting.Protocol {
  71. case setting.HTTP:
  72. err = http.ListenAndServe(listenAddr, m)
  73. case setting.HTTPS:
  74. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  75. default:
  76. log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
  77. }
  78. if err != nil {
  79. log.Fatal(4, "Fail to start server: %v", err)
  80. }
  81. }