Skip to content

Commit

Permalink
rename const variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsongyan committed Oct 30, 2024
1 parent ba21024 commit fd3ebe5
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
4 changes: 2 additions & 2 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func AuthGet(c *gin.Context) {

session := sessions.Default(c)
uuid := helpers.UUID()
session.Delete(SESSION_GITHUB_STATE)
session.Set(SESSION_GITHUB_STATE, uuid)
session.Delete(SessionGithubState)
session.Set(SessionGithubState, uuid)
session.Save()

authurl := "/signin"
Expand Down
4 changes: 2 additions & 2 deletions controllers/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func CaptchaGet(context *gin.Context) {
session := sessions.Default(context)
captchaId := captcha.NewLen(4)
session.Delete(SESSION_CAPTCHA)
session.Set(SESSION_CAPTCHA, captchaId)
session.Delete(SessionCaptcha)
session.Set(SessionCaptcha, captchaId)
session.Save()
captcha.WriteImage(context.Writer, captchaId, 100, 40)
}
8 changes: 4 additions & 4 deletions controllers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func CommentPost(c *gin.Context) {
)
defer writeJSON(c, res)
s := sessions.Default(c)
sessionUserID := s.Get(SESSION_KEY)
sessionUserID := s.Get(SessionKey)
userId, _ := sessionUserID.(uint)

verifyCode := c.PostForm("verifyCode")
captchaId := s.Get(SESSION_CAPTCHA)
s.Delete(SESSION_CAPTCHA)
captchaId := s.Get(SessionCaptcha)
s.Delete(SessionCaptcha)
_captchaId, _ := captchaId.(string)
if !captcha.VerifyString(_captchaId, verifyCode) {
res["message"] = "error verifycode"
Expand Down Expand Up @@ -71,7 +71,7 @@ func CommentDelete(c *gin.Context) {
defer writeJSON(c, res)

s := sessions.Default(c)
sessionUserID := s.Get(SESSION_KEY)
sessionUserID := s.Get(SessionKey)
userId, _ := sessionUserID.(uint)

commentId := c.Param("id")
Expand Down
8 changes: 4 additions & 4 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

const (
SESSION_KEY = "UserID" // session key
CONTEXT_USER_KEY = "User" // context user key
SESSION_GITHUB_STATE = "GITHUB_STATE" // github state session key
SESSION_CAPTCHA = "GIN_CAPTCHA" // captcha session key
SessionKey = "UserID" // session key
ContextUserKey = "User" // context user key
SessionGithubState = "GITHUB_STATE" // GitHub state session key
SessionCaptcha = "GIN_CAPTCHA" // captcha session key
)

func Handle404(c *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions controllers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func IndexGet(c *gin.Context) {
post.Tags, _ = models.ListTagByPostId(strconv.FormatUint(uint64(post.ID), 10))
post.Body = policy.Sanitize(string(blackfriday.MarkdownCommon([]byte(post.Body))))
}
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "index/index.html", gin.H{
"posts": posts,
"tags": models.MustListTag(),
Expand All @@ -58,7 +58,7 @@ func IndexGet(c *gin.Context) {
}

func AdminIndex(c *gin.Context) {
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/index.html", gin.H{
"pageCount": models.CountPage(),
"postCount": models.CountPost(),
Expand Down
2 changes: 1 addition & 1 deletion controllers/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func LinkIndex(c *gin.Context) {
links, _ := models.ListLinks()
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/link.html", gin.H{
"links": links,
"user": user,
Expand Down
10 changes: 5 additions & 5 deletions controllers/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func PageGet(c *gin.Context) {
}
page.View++
page.UpdateView()
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "page/display.html", gin.H{
"page": page,
"user": user,
})
}

func PageNew(c *gin.Context) {
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "page/new.html", gin.H{
"user": user,
})
Expand All @@ -36,7 +36,7 @@ func PageCreate(c *gin.Context) {
body := c.PostForm("body")
isPublished := c.PostForm("isPublished")
published := "on" == isPublished
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)

page := &models.Page{
Title: title,
Expand All @@ -61,7 +61,7 @@ func PageEdit(c *gin.Context) {
if err != nil {
Handle404(c)
}
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "page/modify.html", gin.H{
"page": page,
"user": user,
Expand Down Expand Up @@ -134,7 +134,7 @@ func PageDelete(c *gin.Context) {

func PageIndex(c *gin.Context) {
pages, _ := models.ListAllPage()
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/page.html", gin.H{
"pages": pages,
"user": user,
Expand Down
12 changes: 6 additions & 6 deletions controllers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func PostGet(c *gin.Context) {
post.UpdateView()
post.Tags, _ = models.ListTagByPostId(id)
post.Comments, _ = models.ListCommentByPostID(id)
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "post/display.html", gin.H{
"post": post,
"user": user,
})
}

func PostNew(c *gin.Context) {
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "post/new.html", gin.H{
"user": user,
})
Expand All @@ -40,7 +40,7 @@ func PostCreate(c *gin.Context) {
body := c.PostForm("body")
isPublished := c.PostForm("isPublished")
published := "on" == isPublished
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)

post := &models.Post{
Title: title,
Expand Down Expand Up @@ -83,7 +83,7 @@ func PostEdit(c *gin.Context) {
return
}
post.Tags, _ = models.ListTagByPostId(id)
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "post/modify.html", gin.H{
"post": post,
"user": user,
Expand All @@ -97,7 +97,7 @@ func PostUpdate(c *gin.Context) {
body := c.PostForm("body")
isPublished := c.PostForm("isPublished")
published := "on" == isPublished
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)

pid, err := strconv.ParseUint(id, 10, 64)
if err != nil {
Expand Down Expand Up @@ -187,7 +187,7 @@ func PostDelete(c *gin.Context) {

func PostIndex(c *gin.Context) {
posts, _ := models.ListAllPost("")
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/post.html", gin.H{
"posts": posts,
"Active": "posts",
Expand Down
4 changes: 2 additions & 2 deletions controllers/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func SubscribeGet(c *gin.Context) {
count, _ := models.CountSubscriber()
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "other/subscribe.html", gin.H{
"total": count,
"user": user,
Expand Down Expand Up @@ -170,7 +170,7 @@ func sendEmailToSubscribers(subject, body string) (err error) {

func SubscriberIndex(c *gin.Context) {
subscribers, _ := models.ListSubscriber(false)
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/subscriber.html", gin.H{
"subscribers": subscribers,
"user": user,
Expand Down
22 changes: 11 additions & 11 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func SigninPost(c *gin.Context) {
}
s := sessions.Default(c)
s.Clear()
s.Set(SESSION_KEY, user.ID)
s.Set(SessionKey, user.ID)
s.Save()
if user.IsAdmin {
c.Redirect(http.StatusMovedPermanently, "/admin/index")
Expand All @@ -140,12 +140,12 @@ func Oauth2Callback(c *gin.Context) {

// validate state
session := sessions.Default(c)
if len(state) == 0 || state != session.Get(SESSION_GITHUB_STATE) {
if len(state) == 0 || state != session.Get(SessionGithubState) {
c.Abort()
return
}
// remove state from session
session.Delete(SESSION_GITHUB_STATE)
session.Delete(SessionGithubState)
session.Save()

// exchange accesstoken by code
Expand All @@ -164,7 +164,7 @@ func Oauth2Callback(c *gin.Context) {
return
}

sessionUser, exists := c.Get(CONTEXT_USER_KEY)
sessionUser, exists := c.Get(ContextUserKey)
if exists { // 已登录
user, _ = sessionUser.(*models.User)
_, err1 := models.IsGithubIdExists(userInfo.Login, user.ID)
Expand Down Expand Up @@ -197,7 +197,7 @@ func Oauth2Callback(c *gin.Context) {
if err == nil {
s := sessions.Default(c)
s.Clear()
s.Set(SESSION_KEY, user.ID)
s.Set(SessionKey, user.ID)
s.Save()
if user.IsAdmin {
c.Redirect(http.StatusMovedPermanently, "/admin/index")
Expand Down Expand Up @@ -254,7 +254,7 @@ func getGithubUserInfoByAccessToken(token string) (*GithubUserInfo, error) {
}

func ProfileGet(c *gin.Context) {
sessionUser, exists := c.Get(CONTEXT_USER_KEY)
sessionUser, exists := c.Get(ContextUserKey)
if exists {
c.HTML(http.StatusOK, "admin/profile.html", gin.H{
"user": sessionUser,
Expand All @@ -271,7 +271,7 @@ func ProfileUpdate(c *gin.Context) {
defer writeJSON(c, res)
avatarUrl := c.PostForm("avatarUrl")
nickName := c.PostForm("nickName")
sessionUser, _ := c.Get(CONTEXT_USER_KEY)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
Expand All @@ -293,7 +293,7 @@ func BindEmail(c *gin.Context) {
)
defer writeJSON(c, res)
email := c.PostForm("email")
sessionUser, _ := c.Get(CONTEXT_USER_KEY)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
Expand Down Expand Up @@ -322,7 +322,7 @@ func UnbindEmail(c *gin.Context) {
res = gin.H{}
)
defer writeJSON(c, res)
sessionUser, _ := c.Get(CONTEXT_USER_KEY)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
Expand All @@ -346,7 +346,7 @@ func UnbindGithub(c *gin.Context) {
res = gin.H{}
)
defer writeJSON(c, res)
sessionUser, _ := c.Get(CONTEXT_USER_KEY)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
Expand All @@ -367,7 +367,7 @@ func UnbindGithub(c *gin.Context) {

func UserIndex(c *gin.Context) {
users, _ := models.ListUsers()
user, _ := c.Get(CONTEXT_USER_KEY)
user, _ := c.Get(ContextUserKey)
c.HTML(http.StatusOK, "admin/user.html", gin.H{
"users": users,
"user": user,
Expand Down
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func setTemplate(engine *gin.Engine) {
engine.LoadHTMLGlob(filepath.Join(getCurrentDirectory(), "./views/**/*"))
}

//setSessions initializes sessions & csrf middlewares
// setSessions initializes sessions & csrf middlewares
func setSessions(router *gin.Engine) {
config := system.GetConfiguration()
//https://github.com/gin-gonic/contrib/tree/master/sessions
Expand All @@ -202,14 +202,14 @@ func setSessions(router *gin.Engine) {

//+++++++++++++ middlewares +++++++++++++++++++++++

//SharedData fills in common data, such as user info, etc...
// SharedData fills in common data, such as user info, etc...
func SharedData() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
if uID := session.Get(controllers.SESSION_KEY); uID != nil {
if uID := session.Get(controllers.SessionKey); uID != nil {
user, err := models.GetUser(uID)
if err == nil {
c.Set(controllers.CONTEXT_USER_KEY, user)
c.Set(controllers.ContextUserKey, user)
}
}
if system.GetConfiguration().SignupEnabled {
Expand All @@ -219,10 +219,10 @@ func SharedData() gin.HandlerFunc {
}
}

//AuthRequired grants access to authenticated users, requires SharedData middleware
// AuthRequired grants access to authenticated users, requires SharedData middleware
func AdminScopeRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if user, _ := c.Get(controllers.CONTEXT_USER_KEY); user != nil {
if user, _ := c.Get(controllers.ContextUserKey); user != nil {
if u, ok := user.(*models.User); ok && u.IsAdmin {
c.Next()
return
Expand All @@ -238,7 +238,7 @@ func AdminScopeRequired() gin.HandlerFunc {

func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if user, _ := c.Get(controllers.CONTEXT_USER_KEY); user != nil {
if user, _ := c.Get(controllers.ContextUserKey); user != nil {
if _, ok := user.(*models.User); ok {
c.Next()
return
Expand Down
4 changes: 2 additions & 2 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Configuration struct {
}

const (
DEFAULT_PAGESIZE = 10
DefaultPageSize = 10
)

var configuration *Configuration
Expand All @@ -49,7 +49,7 @@ func LoadConfiguration(path string) error {
return err
}
if config.PageSize <= 0 {
config.PageSize = DEFAULT_PAGESIZE
config.PageSize = DefaultPageSize
}
configuration = &config
return err
Expand Down

0 comments on commit fd3ebe5

Please sign in to comment.