forked from forgejo/forgejo
fork render
This commit is contained in:
parent
0da4975f4f
commit
56af7e99a8
13 changed files with 367 additions and 83 deletions
|
@ -7,26 +7,24 @@ package middleware
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
"github.com/martini-contrib/render"
|
||||
"github.com/martini-contrib/sessions"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
// Context represents context of a request.
|
||||
type Context struct {
|
||||
*Render
|
||||
c martini.Context
|
||||
p martini.Params
|
||||
Req *http.Request
|
||||
Res http.ResponseWriter
|
||||
Session sessions.Session
|
||||
Data base.TmplData
|
||||
Render render.Render
|
||||
User *models.User
|
||||
IsSigned bool
|
||||
|
||||
|
@ -62,27 +60,25 @@ func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
|
|||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = msg
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, tpl, ctx.Data)
|
||||
ctx.HTML(200, tpl, ctx.Data)
|
||||
}
|
||||
|
||||
// Handle handles and logs error by given status.
|
||||
func (ctx *Context) Handle(status int, title string, err error) {
|
||||
log.Error("%s: %v", title, err)
|
||||
if martini.Dev == martini.Prod {
|
||||
ctx.Render.HTML(500, "status/500", ctx.Data)
|
||||
ctx.HTML(500, "status/500", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
ctx.Render.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
|
||||
ctx.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
|
||||
}
|
||||
|
||||
// InitContext initializes a classic context for a request.
|
||||
func InitContext() martini.Handler {
|
||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
||||
session sessions.Session, rd render.Render) {
|
||||
|
||||
data := base.TmplData{}
|
||||
session sessions.Session, rd *Render) {
|
||||
|
||||
ctx := &Context{
|
||||
c: c,
|
||||
|
@ -90,7 +86,6 @@ func InitContext() martini.Handler {
|
|||
Req: r,
|
||||
Res: res,
|
||||
Session: session,
|
||||
Data: data,
|
||||
Render: rd,
|
||||
}
|
||||
|
||||
|
@ -99,16 +94,17 @@ func InitContext() martini.Handler {
|
|||
ctx.User = user
|
||||
ctx.IsSigned = user != nil
|
||||
|
||||
data["IsSigned"] = ctx.IsSigned
|
||||
ctx.Data["IsSigned"] = ctx.IsSigned
|
||||
|
||||
if user != nil {
|
||||
data["SignedUser"] = user
|
||||
data["SignedUserId"] = user.Id
|
||||
data["SignedUserName"] = user.LowerName
|
||||
ctx.Data["SignedUser"] = user
|
||||
ctx.Data["SignedUserId"] = user.Id
|
||||
ctx.Data["SignedUserName"] = user.LowerName
|
||||
}
|
||||
|
||||
ctx.Data["PageStartTime"] = time.Now()
|
||||
|
||||
c.Map(ctx)
|
||||
c.Map(data)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue