]> source.dussan.org Git - gitea.git/commitdiff
move templateFuncs to one file, add middleware context.
authorslene <vslene@gmail.com>
Sat, 15 Mar 2014 11:01:50 +0000 (19:01 +0800)
committerslene <vslene@gmail.com>
Sat, 15 Mar 2014 11:01:50 +0000 (19:01 +0800)
gogs.go
modules/auth/user.go
modules/base/base.go
modules/base/conf.go
modules/base/template.go [new file with mode: 0644]
modules/base/tool.go
modules/middleware/auth.go [new file with mode: 0644]
modules/middleware/context.go [new file with mode: 0644]
routers/user/user.go
web.go

diff --git a/gogs.go b/gogs.go
index 056fb2db615cc49fadd3f0999a7382751d4dbead..e826ab9f360024bc3a19bf046736b36efb8f8402 100644 (file)
--- a/gogs.go
+++ b/gogs.go
@@ -23,6 +23,7 @@ const go11tag = true
 const APP_VER = "0.0.8.0315"
 
 func init() {
+       base.AppVer = APP_VER
        runtime.GOMAXPROCS(runtime.NumCPU())
 }
 
index d950b25002883311347344d3ba9a8cce83684c1d..9c9ce686df7e81f952a8de333282e95205eb5330 100644 (file)
@@ -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"`
index 0d305ab453b2411ec5574dac61c6631c5ab3b8bf..967a78bfbe56835b31021adee25a9dd1dce2cdc4 100644 (file)
@@ -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{})
-       }
-}
index 809b2b8c18a19a33a752176eadf983a3b0950934..f1508d7a62661770503c0de3b4313c918d424f06 100644 (file)
@@ -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 (file)
index 0000000..620bf4a
--- /dev/null
@@ -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,
+}
index 4b84ac0bdd22b7a8543ee6b57889e2193a1801e1..4802c413edd58ef0203a90c47b05511afc09fed8 100644 (file)
@@ -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 (file)
index 0000000..743b982
--- /dev/null
@@ -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 (file)
index 0000000..5ca726d
--- /dev/null
@@ -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()
+       }
+}
index 680055f6611d41ae1b671991cb1021e2a53d5cc3..450f09b6d3cbbbc2631574f31924a46cdf85ae6b 100644 (file)
@@ -15,6 +15,7 @@ import (
        "github.com/gogits/gogs/modules/auth"
        "github.com/gogits/gogs/modules/base"
        "github.com/gogits/gogs/modules/log"
+       "github.com/gogits/gogs/modules/middleware"
 )
 
 func Dashboard(r render.Render, data base.TmplData, session sessions.Session) {
@@ -29,35 +30,34 @@ func Dashboard(r render.Render, data base.TmplData, session sessions.Session) {
        r.HTML(200, "user/dashboard", data)
 }
 
-func Profile(params martini.Params, r render.Render, req *http.Request, data base.TmplData, session sessions.Session) {
-       data["Title"] = "Profile"
+func Profile(ctx *middleware.Context, params martini.Params) {
+       ctx.Data["Title"] = "Profile"
 
        // TODO: Need to check view self or others.
        user, err := models.GetUserByName(params["username"])
        if err != nil {
-               log.Handle(200, "user.Profile", data, r, err)
+               ctx.Log(200, "user.Profile", err)
                return
        }
 
-       data["Owner"] = user
+       ctx.Data["Owner"] = user
 
-       req.ParseForm()
-       tab := req.Form.Get("tab")
-       data["TabName"] = tab
+       tab := ctx.Query("tab")
+       ctx.Data["TabName"] = tab
 
        switch tab {
        case "activity":
                feeds, err := models.GetFeeds(user.Id, 0, true)
                if err != nil {
-                       log.Handle(200, "user.Profile", data, r, err)
+                       ctx.Log(200, "user.Profile", err)
                        return
                }
-               data["Feeds"] = feeds
+               ctx.Data["Feeds"] = feeds
        default:
 
        }
 
-       r.HTML(200, "user/profile", data)
+       ctx.Render.HTML(200, "user/profile", ctx.Data)
 }
 
 func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render.Render, session sessions.Session) {
diff --git a/web.go b/web.go
index cb6ee4aece14cc4a9294ea1aa34de57634432680..ad19a5da01529872c593025c7f357585369fbea6 100644 (file)
--- a/web.go
+++ b/web.go
@@ -19,6 +19,7 @@ import (
        "github.com/gogits/gogs/modules/auth"
        "github.com/gogits/gogs/modules/base"
        "github.com/gogits/gogs/modules/log"
+       "github.com/gogits/gogs/modules/middleware"
        "github.com/gogits/gogs/routers"
        "github.com/gogits/gogs/routers/repo"
        "github.com/gogits/gogs/routers/user"
@@ -33,60 +34,46 @@ gogs web`,
        Flags:  []cli.Flag{},
 }
 
-var AppHelpers template.FuncMap = map[string]interface{}{
-       "AppName": func() string {
-               return base.Cfg.MustValue("", "APP_NAME")
-       },
-       "AppVer": func() string {
-               return APP_VER
-       },
-       "str2html":   base.Str2html,
-       "TimeSince":  base.TimeSince,
-       "Subtract":   base.Subtract,
-       "ActionIcon": base.ActionIcon,
-       "ActionDesc": base.ActionDesc,
-       "DateFormat": base.DateFormat,
-}
-
 func runWeb(*cli.Context) {
-       log.Info("%s %s", base.Cfg.MustValue("", "APP_NAME"), APP_VER)
+       log.Info("%s %s", base.AppName, base.AppVer)
 
        m := martini.Classic()
 
        // Middlewares.
-       m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
-       m.Use(base.InitContext())
+       m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{base.TemplateFuncs}}))
 
        // TODO: should use other store because cookie store is not secure.
        store := sessions.NewCookieStore([]byte("secret123"))
        m.Use(sessions.Sessions("my_session", store))
 
+       m.Use(middleware.InitContext())
+
        // Routers.
-       m.Get("/", auth.SignInRequire(false), routers.Home)
-       m.Any("/user/login", auth.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
-       m.Any("/user/logout", auth.SignInRequire(true), user.SignOut)
-       m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
-       m.Any("/user/delete", auth.SignInRequire(true), user.Delete)
+       m.Get("/", middleware.SignInRequire(false), routers.Home)
+       m.Any("/user/login", middleware.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
+       m.Any("/user/logout", middleware.SignInRequire(true), user.SignOut)
+       m.Any("/user/sign_up", middleware.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp)
+       m.Any("/user/delete", middleware.SignInRequire(true), user.Delete)
        m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
 
-       m.Any("/user/setting", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
-       m.Any("/user/setting/password", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
-       m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
-       m.Any("/user/setting/notification", auth.SignInRequire(true), user.SettingNotification)
-       m.Any("/user/setting/security", auth.SignInRequire(true), user.SettingSecurity)
+       m.Any("/user/setting", middleware.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
+       m.Any("/user/setting/password", middleware.SignInRequire(true), binding.BindIgnErr(auth.UpdatePasswdForm{}), user.SettingPassword)
+       m.Any("/user/setting/ssh", middleware.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
+       m.Any("/user/setting/notification", middleware.SignInRequire(true), user.SettingNotification)
+       m.Any("/user/setting/security", middleware.SignInRequire(true), user.SettingSecurity)
 
-       m.Get("/user/:username", auth.SignInRequire(false), user.Profile)
+       m.Get("/user/:username", middleware.SignInRequire(false), user.Profile)
 
-       m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
-       m.Any("/repo/delete", auth.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
-       m.Any("/repo/list", auth.SignInRequire(false), repo.List)
+       m.Any("/repo/create", middleware.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
+       m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
+       m.Any("/repo/list", middleware.SignInRequire(false), repo.List)
 
-       m.Get("/:username/:reponame/settings", auth.SignInRequire(false), auth.RepoAssignment(true), repo.Setting)
+       m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Setting)
        m.Get("/:username/:reponame/tree/:branchname/**",
-               auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
+               middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
        m.Get("/:username/:reponame/tree/:branchname",
-               auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
-       m.Get("/:username/:reponame", auth.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
+               middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
+       m.Get("/:username/:reponame", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single)
 
        //m.Get("/:username/:reponame", repo.Repo)