mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 20:02:09 +00:00
move templateFuncs to one file, add middleware context.
This commit is contained in:
parent
06deed820d
commit
fa5ad1e465
10 changed files with 173 additions and 95 deletions
|
@ -9,7 +9,6 @@ import (
|
|||
"reflect"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
"github.com/martini-contrib/render"
|
||||
"github.com/martini-contrib/sessions"
|
||||
|
||||
"github.com/gogits/binding"
|
||||
|
@ -62,39 +61,6 @@ func IsSignedIn(session sessions.Session) bool {
|
|||
return SignedInId(session) > 0
|
||||
}
|
||||
|
||||
// SignInRequire checks user status from session.
|
||||
// It will assign correspoding values to
|
||||
// template data map if user has signed in.
|
||||
func SignInRequire(redirect bool) martini.Handler {
|
||||
return func(r render.Render, data base.TmplData, session sessions.Session) {
|
||||
if !IsSignedIn(session) {
|
||||
if redirect {
|
||||
r.Redirect("/")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
user := SignedInUser(session)
|
||||
if user == nil {
|
||||
r.Redirect("/")
|
||||
return
|
||||
}
|
||||
|
||||
data["IsSigned"] = true
|
||||
data["SignedUser"] = user
|
||||
data["SignedUserId"] = user.Id
|
||||
data["SignedUserName"] = user.LowerName
|
||||
}
|
||||
}
|
||||
|
||||
func SignOutRequire() martini.Handler {
|
||||
return func(r render.Render, session sessions.Session) {
|
||||
if IsSignedIn(session) {
|
||||
r.Redirect("/")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type FeedsForm struct {
|
||||
UserId int64 `form:"userid" binding:"Required"`
|
||||
Offset int64 `form:"offset"`
|
||||
|
|
|
@ -4,17 +4,9 @@
|
|||
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/codegangsta/martini"
|
||||
)
|
||||
import ()
|
||||
|
||||
type (
|
||||
// Type TmplData represents data in the templates.
|
||||
TmplData map[string]interface{}
|
||||
)
|
||||
|
||||
func InitContext() martini.Handler {
|
||||
return func(context martini.Context) {
|
||||
context.Map(TmplData{})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,11 @@ import (
|
|||
"github.com/Unknwon/goconfig"
|
||||
)
|
||||
|
||||
var Cfg *goconfig.ConfigFile
|
||||
var (
|
||||
AppVer string
|
||||
AppName string
|
||||
Cfg *goconfig.ConfigFile
|
||||
)
|
||||
|
||||
func exeDir() (string, error) {
|
||||
file, err := exec.LookPath(os.Args[0])
|
||||
|
@ -52,4 +56,6 @@ func init() {
|
|||
}
|
||||
}
|
||||
Cfg.BlockMode = false
|
||||
|
||||
AppName = Cfg.MustValue("", "APP_NAME")
|
||||
}
|
||||
|
|
28
modules/base/template.go
Normal file
28
modules/base/template.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package base
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
)
|
||||
|
||||
func Str2html(raw string) template.HTML {
|
||||
return template.HTML(raw)
|
||||
}
|
||||
|
||||
var TemplateFuncs template.FuncMap = map[string]interface{}{
|
||||
"AppName": func() string {
|
||||
return AppName
|
||||
},
|
||||
"AppVer": func() string {
|
||||
return AppVer
|
||||
},
|
||||
"str2html": Str2html,
|
||||
"TimeSince": TimeSince,
|
||||
"Subtract": Subtract,
|
||||
"ActionIcon": ActionIcon,
|
||||
"ActionDesc": ActionDesc,
|
||||
"DateFormat": DateFormat,
|
||||
}
|
|
@ -8,7 +8,6 @@ import (
|
|||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -30,10 +29,6 @@ const (
|
|||
Year = 12 * Month
|
||||
)
|
||||
|
||||
func Str2html(raw string) template.HTML {
|
||||
return template.HTML(raw)
|
||||
}
|
||||
|
||||
// TimeSince calculates the time interval and generate user-friendly string.
|
||||
func TimeSince(then time.Time) string {
|
||||
now := time.Now()
|
||||
|
|
28
modules/middleware/auth.go
Normal file
28
modules/middleware/auth.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/codegangsta/martini"
|
||||
)
|
||||
|
||||
func SignInRequire(redirect bool) martini.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.IsSigned {
|
||||
if redirect {
|
||||
ctx.Render.Redirect("/")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SignOutRequire() martini.Handler {
|
||||
return func(ctx *Context) {
|
||||
if ctx.IsSigned {
|
||||
ctx.Render.Redirect("/")
|
||||
}
|
||||
}
|
||||
}
|
75
modules/middleware/context.go
Normal file
75
modules/middleware/context.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
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
|
||||
}
|
||||
|
||||
func (ctx *Context) Query(name string) string {
|
||||
ctx.Req.ParseForm()
|
||||
return ctx.Req.Form.Get(name)
|
||||
}
|
||||
|
||||
// func (ctx *Context) Param(name string) string {
|
||||
// return ctx.p[name]
|
||||
// }
|
||||
|
||||
func (ctx *Context) Log(status int, title string, err error) {
|
||||
log.Handle(status, title, ctx.Data, ctx.Render, err)
|
||||
}
|
||||
|
||||
func InitContext() martini.Handler {
|
||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
||||
session sessions.Session, rd render.Render) {
|
||||
|
||||
data := base.TmplData{}
|
||||
|
||||
ctx := &Context{
|
||||
c: c,
|
||||
// p: p,
|
||||
Req: r,
|
||||
Res: res,
|
||||
Data: data,
|
||||
Render: rd,
|
||||
}
|
||||
|
||||
// Get user from session if logined.
|
||||
user := auth.SignedInUser(session)
|
||||
ctx.User = user
|
||||
ctx.IsSigned = ctx != nil
|
||||
|
||||
data["IsSigned"] = true
|
||||
data["SignedUser"] = user
|
||||
data["SignedUserId"] = user.Id
|
||||
data["SignedUserName"] = user.LowerName
|
||||
|
||||
c.Map(ctx)
|
||||
c.Map(data)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue