2014-11-26 16:58:36 +08:00
|
|
|
// Copyright 2014 Unknwon
|
|
|
|
|
// Copyright 2014 Torkel Ödegaard
|
|
|
|
|
|
2014-11-28 18:51:34 +08:00
|
|
|
package cmd
|
2014-11-26 16:58:36 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path"
|
|
|
|
|
|
|
|
|
|
"github.com/Unknwon/macaron"
|
|
|
|
|
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api"
|
2015-03-24 06:28:59 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/static"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2015-11-19 23:50:17 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-11-26 16:58:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newMacaron() *macaron.Macaron {
|
2015-01-29 22:46:54 +08:00
|
|
|
macaron.Env = setting.Env
|
2014-11-26 16:58:36 +08:00
|
|
|
m := macaron.New()
|
2015-04-09 02:31:42 +08:00
|
|
|
|
2014-11-26 16:58:36 +08:00
|
|
|
m.Use(middleware.Logger())
|
|
|
|
|
m.Use(macaron.Recovery())
|
2015-04-01 15:00:17 +08:00
|
|
|
|
2015-01-14 17:34:14 +08:00
|
|
|
if setting.EnableGzip {
|
2015-04-01 15:00:17 +08:00
|
|
|
m.Use(middleware.Gziper())
|
2015-01-14 17:34:14 +08:00
|
|
|
}
|
2014-11-26 16:58:36 +08:00
|
|
|
|
2015-11-20 01:44:07 +08:00
|
|
|
for _, route := range plugins.StaticRoutes {
|
2016-01-09 15:12:27 +08:00
|
|
|
pluginRoute := path.Join("/public/plugins/", route.PluginId)
|
|
|
|
|
log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Directory)
|
|
|
|
|
mapStatic(m, route.Directory, "", pluginRoute)
|
2015-11-20 01:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
2015-11-19 19:55:13 +08:00
|
|
|
mapStatic(m, setting.StaticRootPath, "", "public")
|
|
|
|
|
mapStatic(m, setting.StaticRootPath, "app", "app")
|
|
|
|
|
mapStatic(m, setting.StaticRootPath, "css", "css")
|
|
|
|
|
mapStatic(m, setting.StaticRootPath, "img", "img")
|
|
|
|
|
mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
|
|
|
|
|
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
|
2015-11-19 23:50:17 +08:00
|
|
|
|
2014-11-26 16:58:36 +08:00
|
|
|
m.Use(macaron.Renderer(macaron.RenderOptions{
|
2015-01-01 02:02:00 +08:00
|
|
|
Directory: path.Join(setting.StaticRootPath, "views"),
|
2014-11-26 16:58:36 +08:00
|
|
|
IndentJSON: macaron.Env != macaron.PROD,
|
|
|
|
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
|
|
|
|
}))
|
|
|
|
|
|
2015-05-05 17:21:06 +08:00
|
|
|
if setting.EnforceDomain {
|
|
|
|
|
m.Use(middleware.ValidateHostHeader(setting.Domain))
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 16:58:36 +08:00
|
|
|
m.Use(middleware.GetContextHandler())
|
2015-05-01 17:55:59 +08:00
|
|
|
m.Use(middleware.Sessioner(&setting.SessionOptions))
|
2015-04-08 01:21:14 +08:00
|
|
|
|
2014-11-26 16:58:36 +08:00
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 19:55:13 +08:00
|
|
|
func mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
|
2015-03-24 06:28:59 +08:00
|
|
|
headers := func(c *macaron.Context) {
|
2015-03-25 08:30:26 +08:00
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
2015-03-24 06:28:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if setting.Env == setting.DEV {
|
|
|
|
|
headers = func(c *macaron.Context) {
|
2015-03-25 08:30:26 +08:00
|
|
|
c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
|
2015-03-24 06:28:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.Use(httpstatic.Static(
|
2015-11-19 19:55:13 +08:00
|
|
|
path.Join(rootDir, dir),
|
2015-03-24 06:28:59 +08:00
|
|
|
httpstatic.StaticOptions{
|
2014-11-26 16:58:36 +08:00
|
|
|
SkipLogging: true,
|
|
|
|
|
Prefix: prefix,
|
2015-03-24 06:28:59 +08:00
|
|
|
AddHeaders: headers,
|
2014-11-26 16:58:36 +08:00
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 02:31:42 +08:00
|
|
|
func StartServer() {
|
2015-02-04 22:37:26 +08:00
|
|
|
|
2015-02-03 23:57:42 +08:00
|
|
|
var err error
|
2014-11-26 16:58:36 +08:00
|
|
|
m := newMacaron()
|
2014-12-16 04:25:02 +08:00
|
|
|
api.Register(m)
|
2014-11-26 16:58:36 +08:00
|
|
|
|
|
|
|
|
listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
|
|
|
|
|
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubUrl)
|
|
|
|
|
switch setting.Protocol {
|
|
|
|
|
case setting.HTTP:
|
|
|
|
|
err = http.ListenAndServe(listenAddr, m)
|
|
|
|
|
case setting.HTTPS:
|
|
|
|
|
err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
|
|
|
|
|
default:
|
|
|
|
|
log.Fatal(4, "Invalid protocol: %s", setting.Protocol)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(4, "Fail to start server: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|