web.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if setting.EnforceDomain {
  38. m.Use(middleware.ValidateHostHeader(setting.Domain))
  39. }
  40. m.Use(middleware.GetContextHandler())
  41. m.Use(middleware.Sessioner(&setting.SessionOptions))
  42. m.Use(middleware.RequestMetrics())
  43. return m
  44. }
  45. func mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
  46. headers := func(c *macaron.Context) {
  47. c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
  48. }
  49. if setting.Env == setting.DEV {
  50. headers = func(c *macaron.Context) {
  51. c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
  52. }
  53. }
  54. m.Use(httpstatic.Static(
  55. path.Join(rootDir, dir),
  56. httpstatic.StaticOptions{
  57. SkipLogging: true,
  58. Prefix: prefix,
  59. AddHeaders: headers,
  60. },
  61. ))
  62. }
  63. func StartServer() int {
  64. logger = log.New("server")
  65. var err error
  66. m := newMacaron()
  67. api.Register(m)
  68. listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  69. logger.Info("Server Listening", "address", listenAddr, "protocol", setting.Protocol, "subUrl", 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. logger.Error("Invalid protocol", "protocol", setting.Protocol)
  77. return 1
  78. }
  79. if err != nil {
  80. logger.Error("Fail to start server", "error", err)
  81. return 1
  82. }
  83. return 0
  84. }