mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 13:45:29 +08:00
Macaron rewrite
This commit is contained in:
52
pkg/middleware/middleware.go
Normal file
52
pkg/middleware/middleware.go
Normal file
@ -0,0 +1,52 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Unknwon/macaron"
|
||||
"github.com/macaron-contrib/session"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/log"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
*macaron.Context
|
||||
Session session.Store
|
||||
|
||||
Account *models.Account
|
||||
UserAccount *models.Account
|
||||
|
||||
IsSigned bool
|
||||
}
|
||||
|
||||
func GetContextHandler() macaron.Handler {
|
||||
return func(c *macaron.Context) {
|
||||
ctx := &Context{
|
||||
Context: c,
|
||||
}
|
||||
|
||||
ctx.Data["PageStartTime"] = time.Now()
|
||||
|
||||
c.Map(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle handles and logs error by given status.
|
||||
func (ctx *Context) Handle(status int, title string, err error) {
|
||||
if err != nil {
|
||||
log.Error(4, "%s: %v", title, err)
|
||||
if macaron.Env != macaron.PROD {
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
}
|
||||
}
|
||||
|
||||
switch status {
|
||||
case 404:
|
||||
ctx.Data["Title"] = "Page Not Found"
|
||||
case 500:
|
||||
ctx.Data["Title"] = "Internal Server Error"
|
||||
}
|
||||
|
||||
ctx.HTML(status, "index")
|
||||
}
|
Reference in New Issue
Block a user