mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-16 19:32:27 +08:00
init
This commit is contained in:
112
router.go
Normal file
112
router.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// Package main provides ...
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"github.com/EiBlog/eiblog/setting"
|
||||
"github.com/EiBlog/utils/logd"
|
||||
"github.com/EiBlog/utils/tmpl"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var router *gin.Engine
|
||||
var runmode = setting.Conf.Modes[setting.Conf.RunMode]
|
||||
|
||||
func init() {
|
||||
if setting.Conf.RunMode == setting.PROD {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
router = gin.Default()
|
||||
store := sessions.NewCookieStore([]byte("eiblog321"))
|
||||
store.Options(sessions.Options{
|
||||
MaxAge: 86400 * 999,
|
||||
Path: "/",
|
||||
Secure: runmode.EnableHttps,
|
||||
HttpOnly: true,
|
||||
})
|
||||
router.Use(sessions.Sessions("su", store))
|
||||
// 匹配模版
|
||||
//router.LoadHTMLGlob("views/*.html")
|
||||
if tmpl, err := template.New("").Funcs(tmpl.TplFuncMap).ParseGlob("views/*.*"); err == nil {
|
||||
tmpl, err = tmpl.ParseGlob("views/admin/*.html")
|
||||
if err != nil {
|
||||
logd.Fatal(err)
|
||||
}
|
||||
router.SetHTMLTemplate(tmpl)
|
||||
} else {
|
||||
logd.Fatal(err)
|
||||
}
|
||||
// 测试,开启静态文件
|
||||
router.Static("/static", "./static")
|
||||
router.Use(Filter())
|
||||
router.NoRoute(HandleNotFound)
|
||||
router.GET("/", HandleHomePage)
|
||||
router.GET("/post/:slug", HandleArticlePage)
|
||||
router.GET("/series.html", HandleSeriesPage)
|
||||
router.GET("/archives.html", HandleArchivesPage)
|
||||
router.GET("/search.html", HandleSearchPage)
|
||||
router.GET("/data/comment", HandleComments)
|
||||
router.GET("/feed", HandleFeed)
|
||||
router.GET("/opensearch.xml", HandleOpenSearch)
|
||||
router.GET("/sitemap.xml", HandleSitemap)
|
||||
router.GET("/robots.txt", HandleRobots)
|
||||
// 后台相关
|
||||
admin := router.Group("/admin")
|
||||
admin.GET("/login", HandleLogin)
|
||||
admin.POST("/login", HandleLoginPost)
|
||||
auth := admin.Use(AuthFilter())
|
||||
{
|
||||
// console
|
||||
auth.GET("/profile", HandleProfile)
|
||||
auth.GET("/plugins", HandlePlugins)
|
||||
auth.GET("/themes", HandleThemes)
|
||||
// write
|
||||
auth.GET("/write-post", HandlePost)
|
||||
// manage
|
||||
auth.GET("/manage-posts", HandlePosts)
|
||||
auth.GET("/manage-series", HandleSeries)
|
||||
auth.GET("/add-serie", HandleSerie)
|
||||
auth.GET("/manage-tags", HandleTags)
|
||||
auth.GET("/manage-draft", HandleDraft)
|
||||
auth.GET("/manage-trash", HandleTrash)
|
||||
auth.GET("/options-general", HandleGeneral)
|
||||
auth.GET("/options-discussion", HandleDiscussion)
|
||||
auth.GET("/draft-delete", HandleDraftDelete)
|
||||
// api
|
||||
auth.POST("/api/:action", HandleAPI)
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
var (
|
||||
endRunning = make(chan bool, 1)
|
||||
err error
|
||||
)
|
||||
if runmode.EnableHttp {
|
||||
go func() {
|
||||
logd.Info(fmt.Sprintf("http server Running on %d", runmode.HttpPort))
|
||||
err = router.Run(fmt.Sprintf(":%d", runmode.HttpPort))
|
||||
if err != nil {
|
||||
logd.Info("ListenAndServe: ", err)
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
endRunning <- true
|
||||
}
|
||||
}()
|
||||
}
|
||||
if runmode.EnableHttps {
|
||||
go func() {
|
||||
logd.Info(fmt.Sprintf("https server Running on %d", runmode.HttpsPort))
|
||||
err = router.RunTLS(fmt.Sprintf(":%d", runmode.HttpsPort), runmode.CertFile, runmode.KeyFile)
|
||||
if err != nil {
|
||||
logd.Info("ListenAndServe: ", err)
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
endRunning <- true
|
||||
}
|
||||
}()
|
||||
}
|
||||
<-endRunning
|
||||
}
|
||||
Reference in New Issue
Block a user