代码拉取完成,页面将自动刷新
同步操作将从 wangsongyan/wblog 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"flag"
"html/template"
"net/http"
"path/filepath"
"os"
"strings"
"github.com/cihub/seelog"
"github.com/claudiu/gocron"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/wangsongyan/wblog/controllers"
"github.com/wangsongyan/wblog/helpers"
"github.com/wangsongyan/wblog/models"
"github.com/wangsongyan/wblog/system"
)
func main() {
configFilePath := flag.String("C", "conf/conf.yaml", "config file path")
logConfigPath := flag.String("L", "conf/seelog.xml", "log config file path")
flag.Parse()
logger, err := seelog.LoggerFromConfigAsFile(*logConfigPath)
if err != nil {
seelog.Critical("err parsing seelog config file", err)
return
}
seelog.ReplaceLogger(logger)
defer seelog.Flush()
if err := system.LoadConfiguration(*configFilePath); err != nil {
seelog.Critical("err parsing config log file", err)
return
}
db, err := models.InitDB()
if err != nil {
seelog.Critical("err open databases", err)
return
}
defer db.Close()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
setTemplate(router)
setSessions(router)
router.Use(SharedData())
//Periodic tasks
gocron.Every(1).Day().Do(controllers.CreateXMLSitemap)
gocron.Every(7).Days().Do(controllers.Backup)
gocron.Start()
router.Static("/static", filepath.Join(getCurrentDirectory(), "./static"))
router.NoRoute(controllers.Handle404)
router.GET("/", controllers.IndexGet)
router.GET("/index", controllers.IndexGet)
router.GET("/rss", controllers.RssGet)
if system.GetConfiguration().SignupEnabled {
router.GET("/signup", controllers.SignupGet)
router.POST("/signup", controllers.SignupPost)
}
// user signin and logout
router.GET("/signin", controllers.SigninGet)
router.POST("/signin", controllers.SigninPost)
router.GET("/logout", controllers.LogoutGet)
router.GET("/oauth2callback", controllers.Oauth2Callback)
router.GET("/auth/:authType", controllers.AuthGet)
// captcha
router.GET("/captcha", controllers.CaptchaGet)
visitor := router.Group("/visitor")
visitor.Use(AuthRequired())
{
visitor.POST("/new_comment", controllers.CommentPost)
visitor.POST("/comment/:id/delete", controllers.CommentDelete)
}
// subscriber
router.GET("/subscribe", controllers.SubscribeGet)
router.POST("/subscribe", controllers.Subscribe)
router.GET("/active", controllers.ActiveSubscriber)
router.GET("/unsubscribe", controllers.UnSubscribe)
router.GET("/page/:id", controllers.PageGet)
router.GET("/post/:id", controllers.PostGet)
router.GET("/tag/:tag", controllers.TagGet)
router.GET("/archives/:year/:month", controllers.ArchiveGet)
router.GET("/link/:id", controllers.LinkGet)
authorized := router.Group("/admin")
authorized.Use(AdminScopeRequired())
{
// index
authorized.GET("/index", controllers.AdminIndex)
// image upload
authorized.POST("/upload", controllers.Upload)
// page
authorized.GET("/page", controllers.PageIndex)
authorized.GET("/new_page", controllers.PageNew)
authorized.POST("/new_page", controllers.PageCreate)
authorized.GET("/page/:id/edit", controllers.PageEdit)
authorized.POST("/page/:id/edit", controllers.PageUpdate)
authorized.POST("/page/:id/publish", controllers.PagePublish)
authorized.POST("/page/:id/delete", controllers.PageDelete)
// post
authorized.GET("/post", controllers.PostIndex)
authorized.GET("/new_post", controllers.PostNew)
authorized.POST("/new_post", controllers.PostCreate)
authorized.GET("/post/:id/edit", controllers.PostEdit)
authorized.POST("/post/:id/edit", controllers.PostUpdate)
authorized.POST("/post/:id/publish", controllers.PostPublish)
authorized.POST("/post/:id/delete", controllers.PostDelete)
// tag
authorized.POST("/new_tag", controllers.TagCreate)
//
authorized.GET("/user", controllers.UserIndex)
authorized.POST("/user/:id/lock", controllers.UserLock)
// profile
authorized.GET("/profile", controllers.ProfileGet)
authorized.POST("/profile", controllers.ProfileUpdate)
authorized.POST("/profile/email/bind", controllers.BindEmail)
authorized.POST("/profile/email/unbind", controllers.UnbindEmail)
authorized.POST("/profile/github/unbind", controllers.UnbindGithub)
// subscriber
authorized.GET("/subscriber", controllers.SubscriberIndex)
authorized.POST("/subscriber", controllers.SubscriberPost)
// link
authorized.GET("/link", controllers.LinkIndex)
authorized.POST("/new_link", controllers.LinkCreate)
authorized.POST("/link/:id/edit", controllers.LinkUpdate)
authorized.POST("/link/:id/delete", controllers.LinkDelete)
// comment
authorized.POST("/comment/:id", controllers.CommentRead)
authorized.POST("/read_all", controllers.CommentReadAll)
// backup
authorized.POST("/backup", controllers.BackupPost)
authorized.POST("/restore", controllers.RestorePost)
// mail
authorized.POST("/new_mail", controllers.SendMail)
authorized.POST("/new_batchmail", controllers.SendBatchMail)
}
router.Run(system.GetConfiguration().Addr)
}
func setTemplate(engine *gin.Engine) {
funcMap := template.FuncMap{
"dateFormat": helpers.DateFormat,
"substring": helpers.Substring,
"isOdd": helpers.IsOdd,
"isEven": helpers.IsEven,
"truncate": helpers.Truncate,
"add": helpers.Add,
"minus": helpers.Minus,
"listtag": helpers.ListTag,
}
engine.SetFuncMap(funcMap)
engine.LoadHTMLGlob(filepath.Join(getCurrentDirectory(), "./views/**/*"))
}
//setSessions initializes sessions & csrf middlewares
func setSessions(router *gin.Engine) {
config := system.GetConfiguration()
//https://github.com/gin-gonic/contrib/tree/master/sessions
store := sessions.NewCookieStore([]byte(config.SessionSecret))
store.Options(sessions.Options{HttpOnly: true, MaxAge: 7 * 86400, Path: "/"}) //Also set Secure: true if using SSL, you should though
router.Use(sessions.Sessions("gin-session", store))
//https://github.com/utrack/gin-csrf
/*router.Use(csrf.Middleware(csrf.Options{
Secret: config.SessionSecret,
ErrorFunc: func(c *gin.Context) {
c.String(400, "CSRF token mismatch")
c.Abort()
},
}))*/
}
//+++++++++++++ middlewares +++++++++++++++++++++++
//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 {
user, err := models.GetUser(uID)
if err == nil {
c.Set(controllers.CONTEXT_USER_KEY, user)
}
}
if system.GetConfiguration().SignupEnabled {
c.Set("SignupEnabled", true)
}
c.Next()
}
}
//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 u, ok := user.(*models.User); ok && u.IsAdmin {
c.Next()
return
}
}
seelog.Warnf("User not authorized to visit %s", c.Request.RequestURI)
c.HTML(http.StatusForbidden, "errors/error.html", gin.H{
"message": "Forbidden!",
})
c.Abort()
}
}
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if user, _ := c.Get(controllers.CONTEXT_USER_KEY); user != nil {
if _, ok := user.(*models.User); ok {
c.Next()
return
}
}
seelog.Warnf("User not authorized to visit %s", c.Request.RequestURI)
c.HTML(http.StatusForbidden, "errors/error.html", gin.H{
"message": "Forbidden!",
})
c.Abort()
}
}
func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
seelog.Critical(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
//func getCurrentDirectory() string {
// return ""
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。