web.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 Unknwon
  2. // Copyright 2014 Torkel Ödegaard
  3. package main
  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. var logger log.Logger
  17. func newMacaron() *macaron.Macaron {
  18. macaron.Env = setting.Env
  19. m := macaron.New()
  20. m.Use(middleware.Logger())
  21. m.Use(middleware.Recovery())
  22. if setting.EnableGzip {
  23. m.Use(middleware.Gziper())
  24. }
  25. for _, route := range plugins.StaticRoutes {
  26. pluginRoute := path.Join("/public/plugins/", route.PluginId)
  27. logger.Debug("Plugins: Adding route", "route", pluginRoute, "dir", route.Directory)
  28. mapStatic(m, route.Directory, "", pluginRoute)
  29. }
  30. mapStatic(m, setting.StaticRootPath, "", "public")
  31. mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
  32. m.Use(macaron.Renderer(macaron.RenderOptions{
  33. Directory: path.Join(setting.StaticRootPath, "views"),
  34. IndentJSON: macaron.Env != macaron.PROD,
  35. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  36. }))
  37. m.Use(middleware.GetContextHandler())
  38. m.Use(middleware.Sessioner(&setting.SessionOptions))
  39. m.Use(middleware.RequestMetrics())
  40. // needs to be after context handler
  41. if setting.EnforceDomain {
  42. m.Use(middleware.ValidateHostHeader(setting.Domain))
  43. }
  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() int {
  65. logger = log.New("server")
  66. var err error
  67. m := newMacaron()
  68. api.Register(m)
  69. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  70. logger.Info("Server Listening", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl)
  71. switch setting.Protocol {
  72. case setting.HTTP:
  73. err = http.ListenAndServe(listenAddr, m)
  74. case setting.HTTPS:
  75. err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
  76. default:
  77. logger.Error("Invalid protocol", "protocol", setting.Protocol)
  78. return 1
  79. }
  80. if err != nil {
  81. logger.Error("Fail to start server", "error", err)
  82. return 1
  83. }
  84. return 0
  85. }