mirror of
https://github.com/go-gitea/gitea
synced 2024-12-22 02:48:11 +01:00
Clean code
This commit is contained in:
parent
b3919c577f
commit
f174633b36
@ -6,13 +6,7 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/martini-contrib/render"
|
||||
|
||||
"github.com/gogits/logs"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
)
|
||||
|
||||
var logger *logs.BeeLogger
|
||||
@ -41,9 +35,3 @@ func Warn(format string, v ...interface{}) {
|
||||
func Critical(format string, v ...interface{}) {
|
||||
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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
@ -17,6 +18,7 @@ import (
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
// Context represents context of a request.
|
||||
type Context struct {
|
||||
c martini.Context
|
||||
p martini.Params
|
||||
@ -29,6 +31,7 @@ type Context struct {
|
||||
IsSigned bool
|
||||
}
|
||||
|
||||
// Query querys form parameter.
|
||||
func (ctx *Context) Query(name string) string {
|
||||
ctx.Req.ParseForm()
|
||||
return ctx.Req.Form.Get(name)
|
||||
@ -38,10 +41,14 @@ func (ctx *Context) Query(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)
|
||||
// Handle handles and logs error by given status.
|
||||
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 {
|
||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
||||
session sessions.Session, rd render.Render) {
|
||||
|
@ -10,12 +10,13 @@ import (
|
||||
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
"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) {
|
||||
user.Dashboard(r, data, session)
|
||||
user.Dashboard(ctx)
|
||||
return
|
||||
}
|
||||
data["PageIsHome"] = true
|
||||
|
@ -13,10 +13,10 @@ import (
|
||||
"github.com/gogits/gogs/models"
|
||||
"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 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"
|
||||
|
||||
if req.Method == "GET" {
|
||||
@ -61,10 +61,10 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
|
||||
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"
|
||||
|
||||
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 {
|
||||
log.Handle(200, "repo.Delete", data, r, err)
|
||||
ctx.Handle(200, "repo.Delete", err)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
if u != nil {
|
||||
r.Redirect("/")
|
||||
@ -90,7 +90,7 @@ func List(req *http.Request, r render.Render, data base.TmplData, session sessio
|
||||
data["Title"] = "Repositories"
|
||||
repos, err := models.GetRepositories(u)
|
||||
if err != nil {
|
||||
log.Handle(200, "repo.List", data, r, err)
|
||||
ctx.Handle(200, "repo.List", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -7,10 +7,10 @@ import (
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"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) {
|
||||
return
|
||||
}
|
||||
@ -21,7 +21,7 @@ func Single(params martini.Params, r render.Render, data base.TmplData) {
|
||||
files, err := models.GetReposFiles(params["username"], params["reponame"],
|
||||
params["branchname"], treename)
|
||||
if err != nil {
|
||||
log.Handle(200, "repo.Single", data, r, err)
|
||||
ctx.Handle(200, "repo.Single", err)
|
||||
return
|
||||
}
|
||||
data["Username"] = params["username"]
|
||||
|
@ -15,9 +15,10 @@ 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 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["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.AvatarEmail = form.Avatar
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
log.Handle(200, "setting.Setting", data, r, err)
|
||||
ctx.Handle(200, "setting.Setting", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -48,7 +49,7 @@ func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, r
|
||||
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["PageIsUserSetting"] = true
|
||||
|
||||
@ -60,7 +61,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl
|
||||
user := auth.SignedInUser(session)
|
||||
newUser := &models.User{Passwd: form.NewPasswd}
|
||||
if err := newUser.EncodePasswd(); err != nil {
|
||||
log.Handle(200, "setting.SettingPassword", data, r, err)
|
||||
ctx.Handle(200, "setting.SettingPassword", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -73,7 +74,7 @@ func SettingPassword(form auth.UpdatePasswdForm, r render.Render, data base.Tmpl
|
||||
} else {
|
||||
user.Passwd = newUser.Passwd
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
log.Handle(200, "setting.SettingPassword", data, r, err)
|
||||
ctx.Handle(200, "setting.SettingPassword", err)
|
||||
return
|
||||
}
|
||||
data["IsSuccess"] = true
|
||||
|
@ -15,27 +15,26 @@ import (
|
||||
"github.com/gogits/gogs/models"
|
||||
"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) {
|
||||
data["Title"] = "Dashboard"
|
||||
data["PageIsUserDashboard"] = true
|
||||
repos, err := models.GetRepositories(&models.User{Id: auth.SignedInId(session)})
|
||||
func Dashboard(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Dashboard"
|
||||
ctx.Data["PageIsUserDashboard"] = true
|
||||
repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id})
|
||||
if err != nil {
|
||||
log.Handle(200, "user.Dashboard", data, r, err)
|
||||
ctx.Handle(200, "user.Dashboard", err)
|
||||
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 {
|
||||
log.Handle(200, "user.Dashboard", data, r, err)
|
||||
ctx.Handle(200, "user.Dashboard", err)
|
||||
return
|
||||
}
|
||||
data["Feeds"] = feeds
|
||||
r.HTML(200, "user/dashboard", data)
|
||||
ctx.Data["Feeds"] = feeds
|
||||
ctx.Render.HTML(200, "user/dashboard", ctx.Data)
|
||||
}
|
||||
|
||||
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.
|
||||
user, err := models.GetUserByName(params["username"])
|
||||
if err != nil {
|
||||
ctx.Log(200, "user.Profile", err)
|
||||
ctx.Handle(200, "user.Profile", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -57,7 +56,7 @@ func Profile(ctx *middleware.Context, params martini.Params) {
|
||||
case "activity":
|
||||
feeds, err := models.GetFeeds(user.Id, 0, true)
|
||||
if err != nil {
|
||||
ctx.Log(200, "user.Profile", err)
|
||||
ctx.Handle(200, "user.Profile", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Feeds"] = feeds
|
||||
@ -68,30 +67,30 @@ func Profile(ctx *middleware.Context, params martini.Params) {
|
||||
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) {
|
||||
data["Title"] = "Log In"
|
||||
func SignIn(form auth.LogInForm, ctx *middleware.Context, r render.Render, session sessions.Session) {
|
||||
ctx.Data["Title"] = "Log In"
|
||||
|
||||
if req.Method == "GET" {
|
||||
r.HTML(200, "user/signin", data)
|
||||
if ctx.Req.Method == "GET" {
|
||||
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
||||
if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
|
||||
r.HTML(200, "user/signin", data)
|
||||
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
|
||||
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := models.LoginUserPlain(form.UserName, form.Password)
|
||||
if err != nil {
|
||||
if err.Error() == models.ErrUserNotExist.Error() {
|
||||
data["HasError"] = true
|
||||
data["ErrorMsg"] = "Username or password is not correct"
|
||||
auth.AssignForm(form, data)
|
||||
r.HTML(200, "user/signin", data)
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "Username or password is not correct"
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
ctx.Render.HTML(200, "user/signin", ctx.Data)
|
||||
return
|
||||
}
|
||||
|
||||
log.Handle(200, "user.SignIn", data, r, err)
|
||||
ctx.Handle(200, "user.SignIn", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -106,7 +105,7 @@ func SignOut(r render.Render, session sessions.Session) {
|
||||
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["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"
|
||||
r.HTML(200, "user/signup", data)
|
||||
default:
|
||||
log.Handle(200, "user.SignUp", data, r, err)
|
||||
ctx.Handle(200, "user.SignUp", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -156,7 +155,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren
|
||||
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"
|
||||
|
||||
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():
|
||||
data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
|
||||
default:
|
||||
log.Handle(200, "user.Delete", data, r, err)
|
||||
ctx.Handle(200, "user.Delete", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
<footer id="footer">
|
||||
<div class="container footer-wrap">
|
||||
<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 class="desc"></p>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user