Macaron rewrite

This commit is contained in:
Torkel Ödegaard
2014-10-05 16:50:04 +02:00
parent 2c72831be1
commit 201e1d3e6d
9 changed files with 253 additions and 24 deletions

View File

@@ -1,12 +1,19 @@
// Copyright 2014 Unknwon
// Copyright 2014 Torkel Ödegaard
package cmd
import (
"time"
"fmt"
"net/http"
"path"
"github.com/Unknwon/macaron"
"github.com/codegangsta/cli"
"github.com/siddontang/go-log/log"
"github.com/torkelo/grafana-pro/pkg/configuration"
"github.com/torkelo/grafana-pro/pkg/server"
"github.com/torkelo/grafana-pro/pkg/log"
"github.com/torkelo/grafana-pro/pkg/middleware"
"github.com/torkelo/grafana-pro/pkg/routes"
"github.com/torkelo/grafana-pro/pkg/setting"
)
@@ -18,23 +25,66 @@ var CmdWeb = cli.Command{
Flags: []cli.Flag{},
}
func newMacaron() *macaron.Macaron {
m := macaron.New()
m.Use(middleware.Logger())
m.Use(macaron.Recovery())
m.Use(macaron.Static(
path.Join(setting.StaticRootPath, "public"),
macaron.StaticOptions{
SkipLogging: true,
Prefix: "public",
},
))
m.Use(macaron.Static(
path.Join(setting.StaticRootPath, "public/app"),
macaron.StaticOptions{
SkipLogging: true,
Prefix: "app",
},
))
m.Use(macaron.Static(
path.Join(setting.StaticRootPath, "public/img"),
macaron.StaticOptions{
SkipLogging: true,
Prefix: "img",
},
))
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
IndentJSON: macaron.Env != macaron.PROD,
Delims: macaron.Delims{Left: "[[", Right: "]]"},
}))
m.Use(middleware.GetContextHandler())
return m
}
func runWeb(*cli.Context) {
setting.NewConfigContext()
setting.InitServices()
log.Info("Starting Grafana-Pro v.1-alpha")
setting.NewConfigContext()
m := newMacaron()
cfg := configuration.NewCfg(setting.HttpPort)
server, err := server.NewServer(cfg)
if err != nil {
time.Sleep(time.Second)
panic(err)
// index
m.Get("/", routes.Index)
var err error
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)
}
err = server.ListenAndServe()
if err != nil {
log.Error("ListenAndServe failed: ", err)
log.Fatal(4, "Fail to start server: %v", err)
}
time.Sleep(time.Millisecond * 2000)
}