diff options
author | slene <vslene@gmail.com> | 2014-03-15 19:01:50 +0800 |
---|---|---|
committer | slene <vslene@gmail.com> | 2014-03-15 19:01:50 +0800 |
commit | fa5ad1e46560bd006bccf57e5cac39589d18e344 (patch) | |
tree | 6157a0fb7f2333207d8ba907713be873b3eb0e52 /modules | |
parent | 06deed820d9f48ecf972c8716b17ad58b91ab9b9 (diff) |
move templateFuncs to one file, add middleware context.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/user.go | 34 | ||||
-rw-r--r-- | modules/base/base.go | 10 | ||||
-rw-r--r-- | modules/base/conf.go | 8 | ||||
-rw-r--r-- | modules/base/template.go | 28 | ||||
-rw-r--r-- | modules/base/tool.go | 5 | ||||
-rw-r--r-- | modules/middleware/auth.go | 28 | ||||
-rw-r--r-- | modules/middleware/context.go | 75 |
7 files changed, 139 insertions, 49 deletions
diff --git a/modules/auth/user.go b/modules/auth/user.go index d950b250..9c9ce686 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -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"` diff --git a/modules/base/base.go b/modules/base/base.go index 0d305ab4..967a78bf 100644 --- a/modules/base/base.go +++ b/modules/base/base.go @@ -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{}) - } -} diff --git a/modules/base/conf.go b/modules/base/conf.go index 809b2b8c..f1508d7a 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -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") } diff --git a/modules/base/template.go b/modules/base/template.go new file mode 100644 index 00000000..620bf4a1 --- /dev/null +++ b/modules/base/template.go @@ -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, +} diff --git a/modules/base/tool.go b/modules/base/tool.go index 4b84ac0b..4802c413 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -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() diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go new file mode 100644 index 00000000..743b9829 --- /dev/null +++ b/modules/middleware/auth.go @@ -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("/") + } + } +} diff --git a/modules/middleware/context.go b/modules/middleware/context.go new file mode 100644 index 00000000..5ca726d9 --- /dev/null +++ b/modules/middleware/context.go @@ -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() + } +} |