diff --git a/controllers/auth.go b/controllers/auth.go index d87ba35..82a82e4 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -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" diff --git a/controllers/captcha.go b/controllers/captcha.go index fad6e2c..34a52bc 100644 --- a/controllers/captcha.go +++ b/controllers/captcha.go @@ -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) } diff --git a/controllers/comment.go b/controllers/comment.go index 5ea079e..476363e 100644 --- a/controllers/comment.go +++ b/controllers/comment.go @@ -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" @@ -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") diff --git a/controllers/common.go b/controllers/common.go index c159057..44040e7 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -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) { diff --git a/controllers/index.go b/controllers/index.go index 54f416f..51eef1c 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -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(), @@ -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(), diff --git a/controllers/link.go b/controllers/link.go index 3ffbb35..e91883c 100644 --- a/controllers/link.go +++ b/controllers/link.go @@ -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, diff --git a/controllers/page.go b/controllers/page.go index fed53d8..85c6f23 100644 --- a/controllers/page.go +++ b/controllers/page.go @@ -17,7 +17,7 @@ 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, @@ -25,7 +25,7 @@ func PageGet(c *gin.Context) { } 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, }) @@ -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, @@ -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, @@ -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, diff --git a/controllers/post.go b/controllers/post.go index 25b3805..b9486c1 100644 --- a/controllers/post.go +++ b/controllers/post.go @@ -20,7 +20,7 @@ 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, @@ -28,7 +28,7 @@ func PostGet(c *gin.Context) { } 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, }) @@ -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, @@ -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, @@ -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 { @@ -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", diff --git a/controllers/subscriber.go b/controllers/subscriber.go index e6ca60a..636f26d 100644 --- a/controllers/subscriber.go +++ b/controllers/subscriber.go @@ -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, @@ -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, diff --git a/controllers/user.go b/controllers/user.go index eef0a46..f6a1d49 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -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") @@ -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 @@ -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) @@ -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") @@ -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, @@ -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" @@ -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" @@ -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" @@ -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" @@ -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, diff --git a/main.go b/main.go index 322f641..21fa392 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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 { @@ -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 @@ -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 diff --git a/system/system.go b/system/system.go index 943dc06..f24515c 100644 --- a/system/system.go +++ b/system/system.go @@ -33,7 +33,7 @@ type Configuration struct { } const ( - DEFAULT_PAGESIZE = 10 + DefaultPageSize = 10 ) var configuration *Configuration @@ -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