Clean code
This commit is contained in:
parent
b3919c577f
commit
f174633b36
|
@ -6,13 +6,7 @@
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/martini-contrib/render"
|
|
||||||
|
|
||||||
"github.com/gogits/logs"
|
"github.com/gogits/logs"
|
||||||
|
|
||||||
"github.com/gogits/gogs/modules/base"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger *logs.BeeLogger
|
var logger *logs.BeeLogger
|
||||||
|
@ -41,9 +35,3 @@ func Warn(format string, v ...interface{}) {
|
||||||
func Critical(format string, v ...interface{}) {
|
func Critical(format string, v ...interface{}) {
|
||||||
logger.Critical(format, v...)
|
logger.Critical(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Handle(status int, title string, data base.TmplData, r render.Render, err error) {
|
|
||||||
data["ErrorMsg"] = err
|
|
||||||
Error("%s: %v", title, err)
|
|
||||||
r.HTML(status, fmt.Sprintf("status/%d", status), data)
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/codegangsta/martini"
|
"github.com/codegangsta/martini"
|
||||||
|
@ -17,6 +18,7 @@ import (
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Context represents context of a request.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
c martini.Context
|
c martini.Context
|
||||||
p martini.Params
|
p martini.Params
|
||||||
|
@ -29,6 +31,7 @@ type Context struct {
|
||||||
IsSigned bool
|
IsSigned bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Query querys form parameter.
|
||||||
func (ctx *Context) Query(name string) string {
|
func (ctx *Context) Query(name string) string {
|
||||||
ctx.Req.ParseForm()
|
ctx.Req.ParseForm()
|
||||||
return ctx.Req.Form.Get(name)
|
return ctx.Req.Form.Get(name)
|
||||||
|
@ -38,10 +41,14 @@ func (ctx *Context) Query(name string) string {
|
||||||
// return ctx.p[name]
|
// return ctx.p[name]
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (ctx *Context) Log(status int, title string, err error) {
|
// Handle handles and logs error by given status.
|
||||||
log.Handle(status, title, ctx.Data, ctx.Render, err)
|
func (ctx *Context) Handle(status int, title string, err error) {
|
||||||
|
ctx.Data["ErrorMsg"] = err
|
||||||
|
log.Error("%s: %v", title, err)
|
||||||
|
ctx.Render.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InitContext initializes a classic context for a request.
|
||||||
func InitContext() martini.Handler {
|
func InitContext() martini.Handler {
|
||||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
||||||
session sessions.Session, rd render.Render) {
|
session sessions.Session, rd render.Render) {
|
||||||
|
|
|
@ -10,12 +10,13 @@ import (
|
||||||
|
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
|
"github.com/gogits/gogs/modules/middleware"
|
||||||
"github.com/gogits/gogs/routers/user"
|
"github.com/gogits/gogs/routers/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Home(r render.Render, data base.TmplData, session sessions.Session) {
|
func Home(ctx *middleware.Context, r render.Render, data base.TmplData, session sessions.Session) {
|
||||||
if auth.IsSignedIn(session) {
|
if auth.IsSignedIn(session) {
|
||||||
user.Dashboard(r, data, session)
|
user.Dashboard(ctx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data["PageIsHome"] = true
|
data["PageIsHome"] = true
|
||||||
|
|
|
@ -13,10 +13,10 @@ import (
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
func Create(form auth.CreateRepoForm, ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||||
data["Title"] = "Create repository"
|
data["Title"] = "Create repository"
|
||||||
|
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
|
@ -61,10 +61,10 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Handle(200, "repo.Create", data, r, err)
|
ctx.Handle(200, "repo.Create", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
func Delete(form auth.DeleteRepoForm, ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||||
data["Title"] = "Delete repository"
|
data["Title"] = "Delete repository"
|
||||||
|
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
|
@ -73,14 +73,14 @@ func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data b
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.DeleteRepository(form.UserId, form.RepoId, form.UserName); err != nil {
|
if err := models.DeleteRepository(form.UserId, form.RepoId, form.UserName); err != nil {
|
||||||
log.Handle(200, "repo.Delete", data, r, err)
|
ctx.Handle(200, "repo.Delete", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Redirect("/", 302)
|
r.Redirect("/", 302)
|
||||||
}
|
}
|
||||||
|
|
||||||
func List(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
func List(ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
|
||||||
u := auth.SignedInUser(session)
|
u := auth.SignedInUser(session)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
r.Redirect("/")
|
r.Redirect("/")
|
||||||
|
@ -90,7 +90,7 @@ func List(req *http.Request, r render.Render, data base.TmplData, session sessio
|
||||||
data["Title"] = "Repositories"
|
data["Title"] = "Repositories"
|
||||||
repos, err := models.GetRepositories(u)
|
repos, err := models.GetRepositories(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Handle(200, "repo.List", data, r, err)
|
ctx.Handle(200, "repo.List", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Single(params martini.Params, r render.Render, data base.TmplData) {
|
func Single(params martini.Params, ctx *middleware.Context, r render.Render, data base.TmplData) {
|
||||||
if !data["IsRepositoryValid"].(bool) {
|
if !data["IsRepositoryValid"].(bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func Single(params martini.Params, r render.Render, data base.TmplData) {
|
||||||
files, err := models.GetReposFiles(params["username"], params["reponame"],
|
files, err := models.GetReposFiles(params["username"], params["reponame"],
|
||||||
params["branchname"], treename)
|
params["branchname"], treename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Handle(200, "repo.Single", data, r, err)
|
ctx.Handle(200, "repo.Single", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data["Username"] = params["username"]
|
data["Username"] = params["username"]
|
||||||
|
|
|
@ -15,9 +15,10 @@ import (
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/log"
|
||||||
|
"github.com/gogits/gogs/modules/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
|
func Setting(form auth.UpdateProfileForm, ctx *middleware.Context, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
|
||||||
data["Title"] = "Setting"
|
data["Title"] = "Setting"
|
||||||
data["PageIsUserSetting"] = true
|
data["PageIsUserSetting"] = true
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, r
|
||||||
user.Avatar = base.EncodeMd5(form.Avatar)
|
user.Avatar = base.EncodeMd5(form.Avatar)
|
||||||
user.AvatarEmail = form.Avatar
|
user.AvatarEmail = form.Avatar
|
||||||
if err := models.UpdateUser(user); err != nil {
|
if err := models.UpdateUser(user); err != nil {
|
||||||
log.Handle(200, "setting.Setting", data, r, err)
|
ctx.Handle(200, "setting.Setting", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +49,7 @@ func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, r
|
||||||
r.HTML(200, "user/setting", data)
|
r.HTML(200, "user/setting", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.TmplData, session sessions.Session, req *http.Request) {
|
func SettingPassword(form auth.UpdatePasswdForm, ctx *middleware.Context, r render.Render, data base.TmplData, session sessions.Session, req *http.Request) {
|
||||||
data["Title"] = "Password"
|
data["Title"] = "Password"
|
||||||
data["PageIsUserSetting"] = true
|
data["PageIsUserSetting"] = true
|
||||||
|
|
||||||
|
@ -60,7 +61,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl
|
||||||
user := auth.SignedInUser(session)
|
user := auth.SignedInUser(session)
|
||||||
newUser := &models.User{Passwd: form.NewPasswd}
|
newUser := &models.User{Passwd: form.NewPasswd}
|
||||||
if err := newUser.EncodePasswd(); err != nil {
|
if err := newUser.EncodePasswd(); err != nil {
|
||||||
log.Handle(200, "setting.SettingPassword", data, r, err)
|
ctx.Handle(200, "setting.SettingPassword", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +74,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl
|
||||||
} else {
|
} else {
|
||||||
user.Passwd = newUser.Passwd
|
user.Passwd = newUser.Passwd
|
||||||
if err := models.UpdateUser(user); err != nil {
|
if err := models.UpdateUser(user); err != nil {
|
||||||
log.Handle(200, "setting.SettingPassword", data, r, err)
|
ctx.Handle(200, "setting.SettingPassword", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data["IsSuccess"] = true
|
data["IsSuccess"] = true
|
||||||
|
|
|
@ -15,27 +15,26 @@ import (
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/auth"
|
"github.com/gogits/gogs/modules/auth"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/log"
|
|
||||||
"github.com/gogits/gogs/modules/middleware"
|
"github.com/gogits/gogs/modules/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Dashboard(r render.Render, data base.TmplData, session sessions.Session) {
|
func Dashboard(ctx *middleware.Context) {
|
||||||
data["Title"] = "Dashboard"
|
ctx.Data["Title"] = "Dashboard"
|
||||||
data["PageIsUserDashboard"] = true
|
ctx.Data["PageIsUserDashboard"] = true
|
||||||
repos, err := models.GetRepositories(&models.User{Id: auth.SignedInId(session)})
|
repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Handle(200, "user.Dashboard", data, r, err)
|
ctx.Handle(200, "user.Dashboard", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data["MyRepos"] = repos
|
ctx.Data["MyRepos"] = repos
|
||||||
|
|
||||||
feeds, err := models.GetFeeds(auth.SignedInId(session), 0, false)
|
feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Handle(200, "user.Dashboard", data, r, err)
|
ctx.Handle(200, "user.Dashboard", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data["Feeds"] = feeds
|
ctx.Data["Feeds"] = feeds
|
||||||
r.HTML(200, "user/dashboard", data)
|
ctx.Render.HTML(200, "user/dashboard", ctx.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Profile(ctx *middleware.Context, params martini.Params) {
|
func Profile(ctx *middleware.Context, params martini.Params) {
|
||||||
|
@ -44,7 +43,7 @@ func Profile(ctx *middleware.Context, params martini.Params) {
|
||||||
// TODO: Need to check view self or others.
|
// TODO: Need to check view self or others.
|
||||||
user, err := models.GetUserByName(params["username"])
|
user, err := models.GetUserByName(params["username"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Log(200, "user.Profile", err)
|
ctx.Handle(200, "user.Profile", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +56,7 @@ func Profile(ctx *middleware.Context, params martini.Params) {
|
||||||
case "activity":
|
case "activity":
|
||||||
feeds, err := models.GetFeeds(user.Id, 0, true)
|
feeds, err := models.GetFeeds(user.Id, 0, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Log(200, "user.Profile", err)
|
ctx.Handle(200, "user.Profile", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["Feeds"] = feeds
|
ctx.Data["Feeds"] = feeds
|
||||||
|
@ -68,30 +67,30 @@ func Profile(ctx *middleware.Context, params martini.Params) {
|
||||||
ctx.Render.HTML(200, "user/profile", ctx.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) {
|
func SignIn(form auth.LogInForm, ctx *middleware.Context, r render.Render, session sessions.Session) {
|
||||||
data["Title"] = "Log In"
|
ctx.Data["Title"] = "Log In"
|
||||||
|
|
||||||
if req.Method == "GET" {
|
if ctx.Req.Method == "GET" {
|
||||||
r.HTML(200, "user/signin", data)
|
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
|
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
|
||||||
r.HTML(200, "user/signin", data)
|
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := models.LoginUserPlain(form.UserName, form.Password)
|
user, err := models.LoginUserPlain(form.UserName, form.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == models.ErrUserNotExist.Error() {
|
if err.Error() == models.ErrUserNotExist.Error() {
|
||||||
data["HasError"] = true
|
ctx.Data["HasError"] = true
|
||||||
data["ErrorMsg"] = "Username or password is not correct"
|
ctx.Data["ErrorMsg"] = "Username or password is not correct"
|
||||||
auth.AssignForm(form, data)
|
auth.AssignForm(form, ctx.Data)
|
||||||
r.HTML(200, "user/signin", data)
|
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Handle(200, "user.SignIn", data, r, err)
|
ctx.Handle(200, "user.SignIn", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +105,7 @@ func SignOut(r render.Render, session sessions.Session) {
|
||||||
r.Redirect("/")
|
r.Redirect("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) {
|
func SignUp(form auth.RegisterForm, ctx *middleware.Context, data base.TmplData, req *http.Request, r render.Render) {
|
||||||
data["Title"] = "Sign Up"
|
data["Title"] = "Sign Up"
|
||||||
data["PageIsSignUp"] = true
|
data["PageIsSignUp"] = true
|
||||||
|
|
||||||
|
@ -148,7 +147,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren
|
||||||
data["ErrorMsg"] = "E-mail address has been already used"
|
data["ErrorMsg"] = "E-mail address has been already used"
|
||||||
r.HTML(200, "user/signup", data)
|
r.HTML(200, "user/signup", data)
|
||||||
default:
|
default:
|
||||||
log.Handle(200, "user.SignUp", data, r, err)
|
ctx.Handle(200, "user.SignUp", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -156,7 +155,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren
|
||||||
r.Redirect("/user/login")
|
r.Redirect("/user/login")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(data base.TmplData, req *http.Request, session sessions.Session, r render.Render) {
|
func Delete(data base.TmplData, ctx *middleware.Context, req *http.Request, session sessions.Session, r render.Render) {
|
||||||
data["Title"] = "Delete Account"
|
data["Title"] = "Delete Account"
|
||||||
|
|
||||||
if req.Method == "GET" {
|
if req.Method == "GET" {
|
||||||
|
@ -172,7 +171,7 @@ func Delete(data base.TmplData, req *http.Request, session sessions.Session, r r
|
||||||
case models.ErrUserOwnRepos.Error():
|
case models.ErrUserOwnRepos.Error():
|
||||||
data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
|
data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
|
||||||
default:
|
default:
|
||||||
log.Handle(200, "user.Delete", data, r, err)
|
ctx.Handle(200, "user.Delete", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<footer id="footer">
|
<footer id="footer">
|
||||||
<div class="container footer-wrap">
|
<div class="container footer-wrap">
|
||||||
<p>
|
<p>
|
||||||
© 2014 Gogs. ver {{AppVer}}. <a target="_blank" href="https://github.com/gogits/gogs">GitHub</a>
|
© 2014 Gogs · ver {{AppVer}} · <i class="fa fa-github"></i><a target="_blank" href="https://github.com/gogits/gogs">GitHub</a>
|
||||||
</p>
|
</p>
|
||||||
<p class="desc"></p>
|
<p class="desc"></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user