0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-21 23:51:32 +01:00

move templateFuncs to one file, add middleware context.

This commit is contained in:
slene 2014-03-15 19:01:50 +08:00
parent 06deed820d
commit fa5ad1e465
10 changed files with 173 additions and 95 deletions

View file

@ -23,6 +23,7 @@ const go11tag = true
const APP_VER = "0.0.8.0315"
func init() {
base.AppVer = APP_VER
runtime.GOMAXPROCS(runtime.NumCPU())
}

View 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"`

View 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{})
}
}

View 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")
}

28
modules/base/template.go Normal file
View 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,
}

View 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()

View 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("/")
}
}
}

View 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()
}
}

View 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) {

59
web.go
View file

@ -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)