grafana/pkg/cmd/web.go

138 lines
3.2 KiB
Go
Raw Normal View History

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"
"io/ioutil"
2014-11-26 16:58:36 +08:00
"net/http"
"os"
2014-11-26 16:58:36 +08:00
"path"
"path/filepath"
"strconv"
2014-11-26 16:58:36 +08:00
"github.com/Unknwon/macaron"
"github.com/codegangsta/cli"
"github.com/macaron-contrib/session"
_ "github.com/macaron-contrib/session/mysql"
_ "github.com/macaron-contrib/session/postgres"
2014-11-26 16:58:36 +08:00
2015-02-05 17:37:13 +08:00
"github.com/grafana/grafana/pkg/api"
"github.com/grafana/grafana/pkg/api/static"
2015-02-05 17:37:13 +08:00
"github.com/grafana/grafana/pkg/log"
2015-03-23 03:14:00 +08:00
"github.com/grafana/grafana/pkg/metrics"
2015-02-05 17:37:13 +08:00
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/plugins"
2015-02-05 17:37:13 +08:00
"github.com/grafana/grafana/pkg/services/eventpublisher"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/social"
2014-11-26 16:58:36 +08:00
)
var Web = cli.Command{
2014-11-26 16:58:36 +08:00
Name: "web",
2015-02-23 21:20:24 +08:00
Usage: "Starts Grafana backend & web server",
Description: "Starts Grafana backend & web server",
2014-11-26 16:58:36 +08:00
Action: runWeb,
}
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()
m.Use(middleware.Logger())
m.Use(macaron.Recovery())
if setting.EnableGzip {
m.Use(macaron.Gziper())
}
2014-11-26 16:58:36 +08:00
mapStatic(m, "", "public")
mapStatic(m, "app", "app")
mapStatic(m, "css", "css")
mapStatic(m, "img", "img")
2015-01-09 00:34:41 +08:00
mapStatic(m, "fonts", "fonts")
2014-11-26 16:58:36 +08:00
2014-12-30 17:28:27 +08:00
m.Use(session.Sessioner(setting.SessionOptions))
2014-11-26 16:58:36 +08:00
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
2014-11-26 16:58:36 +08:00
IndentJSON: macaron.Env != macaron.PROD,
Delims: macaron.Delims{Left: "[[", Right: "]]"},
}))
m.Use(middleware.GetContextHandler())
return m
}
func mapStatic(m *macaron.Macaron, dir string, prefix string) {
headers := func(c *macaron.Context) {
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
}
if setting.Env == setting.DEV {
headers = func(c *macaron.Context) {
c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
}
}
m.Use(httpstatic.Static(
2014-11-26 16:58:36 +08:00
path.Join(setting.StaticRootPath, dir),
httpstatic.StaticOptions{
2014-11-26 16:58:36 +08:00
SkipLogging: true,
Prefix: prefix,
AddHeaders: headers,
2014-11-26 16:58:36 +08:00
},
))
}
2015-01-01 22:29:10 +08:00
func runWeb(c *cli.Context) {
initRuntime(c)
writePIDFile(c)
2014-11-26 16:58:36 +08:00
social.NewOAuthService()
eventpublisher.Init()
plugins.Init()
var err error
2014-11-26 16:58:36 +08:00
m := newMacaron()
api.Register(m)
2014-11-26 16:58:36 +08:00
2015-03-23 03:14:00 +08:00
if setting.ReportingEnabled {
go metrics.StartUsageReportLoop()
}
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)
}
}
func writePIDFile(c *cli.Context) {
path := c.GlobalString("pidfile")
if path == "" {
return
}
// Ensure the required directory structure exists.
err := os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
log.Fatal(3, "Failed to verify pid directory", err)
}
// Retrieve the PID and write it.
pid := strconv.Itoa(os.Getpid())
if err := ioutil.WriteFile(path, []byte(pid), 0644); err != nil {
log.Fatal(3, "Failed to write pidfile", err)
}
}