diff --git a/api.go b/api.go index eb72157..ca2969a 100644 --- a/api.go +++ b/api.go @@ -393,6 +393,18 @@ func apiTrashRecover(c *gin.Context) { func apiFileUpload(c *gin.Context) { + // file, header , err := c.Request.FormFile("upload") + // filename := header.Filename + // fmt.Println(header.Filename) + // out, err := os.Create("./tmp/"+filename+".png") + // if err != nil { + // log.Fatal(err) + // } + // defer out.Close() + // _, err = io.Copy(out, file) + // if err != nil { + // log.Fatal(err) + // } } func responseNotice(c *gin.Context, typ, content, hl string) { diff --git a/back.go b/back.go index ff9352c..1112d92 100644 --- a/back.go +++ b/back.go @@ -2,7 +2,9 @@ package main import ( + "bytes" "fmt" + "html/template" "net/http" "strconv" "time" @@ -45,9 +47,8 @@ func HandleLogin(c *gin.Context) { c.Redirect(http.StatusFound, "/admin/profile") return } - c.HTML(http.StatusOK, "login.html", gin.H{ - "BTitle": Ei.BTitle, - }) + c.Status(http.StatusOK) + RenderHTMLBack(c, "login.html", gin.H{"BTitle": Ei.BTitle}) } func HandleLoginPost(c *gin.Context) { @@ -55,12 +56,12 @@ func HandleLoginPost(c *gin.Context) { pwd := c.PostForm("password") // code := c.PostForm("code") // 二次验证 if user == "" || pwd == "" { - logd.Info("参数错误", user, pwd) + logd.Print("参数错误", user, pwd) c.Redirect(http.StatusFound, "/admin/login") return } if Ei.Username != user || !VerifyPasswd(Ei.Password, user, pwd) { - logd.Info("账号或密码错误", user, pwd) + logd.Print("账号或密码错误", user, pwd) c.Redirect(http.StatusFound, "/admin/login") return } @@ -84,8 +85,8 @@ func HandleProfile(c *gin.Context) { h["Path"] = c.Request.URL.Path h["Title"] = "个人配置 | " + Ei.BTitle h["Account"] = Ei - h["Profile"] = true - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-profile", h) } // 写文章==>Write @@ -107,7 +108,6 @@ func HandlePost(c *gin.Context) { if h["Title"] == "" { h["Title"] = "撰写文章 | " + Ei.BTitle } - h["Post"] = true h["Path"] = c.Request.URL.Path h["Domain"] = setting.Conf.Mode.Domain h["Series"] = Ei.Series @@ -116,7 +116,8 @@ func HandlePost(c *gin.Context) { tags = append(tags, T{tag, tag}) } h["Tags"] = tags - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-post", h) } func HandleDraftDelete(c *gin.Context) { @@ -149,7 +150,6 @@ func HandlePosts(c *gin.Context) { h["Manage"] = true h["Path"] = c.Request.URL.Path h["Title"] = "文章管理 | " + Ei.BTitle - h["Posts"] = true h["Series"] = Ei.Series h["Serie"] = se h["KW"] = kw @@ -169,7 +169,8 @@ func HandlePosts(c *gin.Context) { h["PP"].(map[int]string)[i+1] = vals.Encode() } h["Cur"] = pg - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-posts", h) } // 专题列表 @@ -178,9 +179,9 @@ func HandleSeries(c *gin.Context) { h["Manage"] = true h["Path"] = c.Request.URL.Path h["Title"] = "专题管理 | " + Ei.BTitle - h["Series"] = true h["List"] = Ei.Series - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-series", h) } func HandleSerie(c *gin.Context) { @@ -194,8 +195,8 @@ func HandleSerie(c *gin.Context) { } h["Manage"] = true h["Path"] = c.Request.URL.Path - h["Serie"] = true - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-serie", h) } // 标签列表 @@ -204,9 +205,9 @@ func HandleTags(c *gin.Context) { h["Manage"] = true h["Path"] = c.Request.URL.Path h["Title"] = "标签管理 | " + Ei.BTitle - h["Tags"] = true h["List"] = Ei.Tags - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-tags", h) } // 草稿箱 @@ -215,15 +216,15 @@ func HandleDraft(c *gin.Context) { h["Manage"] = true h["Path"] = c.Request.URL.Path h["Title"] = "草稿箱 | " + Ei.BTitle - h["Draft"] = true var err error h["List"], err = LoadDraft() if err != nil { logd.Error(err) - c.HTML(http.StatusBadRequest, "backLayout.html", h) - return + c.Status(http.StatusBadRequest) + } else { + c.Status(http.StatusOK) } - c.HTML(http.StatusOK, "backLayout.html", h) + RenderHTMLBack(c, "admin-draft", h) } // 回收箱 @@ -232,7 +233,6 @@ func HandleTrash(c *gin.Context) { h["Manage"] = true h["Path"] = c.Request.URL.Path h["Title"] = "回收箱 | " + Ei.BTitle - h["Trash"] = true var err error h["List"], err = LoadTrash() if err != nil { @@ -240,7 +240,8 @@ func HandleTrash(c *gin.Context) { c.HTML(http.StatusBadRequest, "backLayout.html", h) return } - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-trash", h) } // 基本设置==>Setting @@ -249,8 +250,8 @@ func HandleGeneral(c *gin.Context) { h["Setting"] = true h["Path"] = c.Request.URL.Path h["Title"] = "基本设置 | " + Ei.BTitle - h["General"] = true - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-general", h) } // 阅读设置 @@ -259,8 +260,8 @@ func HandleDiscussion(c *gin.Context) { h["Setting"] = true h["Path"] = c.Request.URL.Path h["Title"] = "阅读设置 | " + Ei.BTitle - h["Discussion"] = true - c.HTML(http.StatusOK, "backLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLBack(c, "admin-discussion", h) } // api @@ -274,3 +275,25 @@ func HandleAPI(c *gin.Context) { } api(c) } + +func RenderHTMLBack(c *gin.Context, name string, data gin.H) { + if name == "login.html" { + err := Tmpl.ExecuteTemplate(c.Writer, name, data) + if err != nil { + panic(err) + } + c.Header("Content-Type", "text/html; charset=utf-8") + return + } + var buf bytes.Buffer + err := Tmpl.ExecuteTemplate(&buf, name, data) + if err != nil { + panic(err) + } + data["LayoutContent"] = template.HTML(buf.String()) + err = Tmpl.ExecuteTemplate(c.Writer, "backLayout.html", data) + if err != nil { + panic(err) + } + c.Header("Content-Type", "text/html; charset=utf-8") +} diff --git a/front.go b/front.go index ddfd61d..ccfa1db 100644 --- a/front.go +++ b/front.go @@ -3,7 +3,9 @@ package main import ( + "bytes" "fmt" + "html/template" "io/ioutil" "net/http" "regexp" @@ -31,6 +33,8 @@ func UserCookie(c *gin.Context) { cookie, err := c.Request.Cookie("u") if err != nil || cookie.Value == "" { // TODO cookie操作 + b := []byte(c.ClientIP() + time.Now().String()) + c.SetCookie("u", fmt.Sprintf("%x", SHA1(b)), 86400*999, "/", "", true, true) } } @@ -52,15 +56,6 @@ func StaticVersion(c *gin.Context) (version int) { return 0 } -func HandleNotFound(c *gin.Context) { - h := GetBase() - h["Version"] = StaticVersion(c) - h["Title"] = "Not Found" - h["NotFoundPage"] = true - h["Path"] = "" - c.HTML(http.StatusNotFound, "homeLayout.html", h) -} - func GetBase() gin.H { return gin.H{ "Favicon": setting.Conf.Favicon, @@ -75,19 +70,28 @@ func GetBase() gin.H { } } +func HandleNotFound(c *gin.Context) { + h := GetBase() + h["Version"] = StaticVersion(c) + h["Title"] = "Not Found" + h["Path"] = "" + c.Status(http.StatusNotFound) + RenderHTMLFront(c, "notfound", h) +} + func HandleHomePage(c *gin.Context) { h := GetBase() h["Version"] = StaticVersion(c) h["Title"] = Ei.BTitle + " | " + Ei.SubTitle h["Path"] = c.Request.URL.Path - h["HomePage"] = true h["CurrentPage"] = "blog-home" pn, err := strconv.Atoi(c.Query("pn")) if err != nil || pn < 1 { pn = 1 } h["Prev"], h["Next"], h["List"] = PageList(pn, setting.Conf.PageNum) - c.HTML(http.StatusOK, "homeLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLFront(c, "home", h) } func HandleSeriesPage(c *gin.Context) { @@ -95,10 +99,10 @@ func HandleSeriesPage(c *gin.Context) { h["Version"] = StaticVersion(c) h["Title"] = "专题 | " + Ei.BTitle h["Path"] = c.Request.URL.Path - h["SeriesPage"] = true h["CurrentPage"] = "series" h["Article"] = Ei.PageSeries - c.HTML(http.StatusOK, "homeLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLFront(c, "series", h) } func HandleArchivesPage(c *gin.Context) { @@ -106,10 +110,10 @@ func HandleArchivesPage(c *gin.Context) { h["Version"] = StaticVersion(c) h["Title"] = "归档 | " + Ei.BTitle h["Path"] = c.Request.URL.Path - h["ArchivesPage"] = true h["CurrentPage"] = "archives" h["Article"] = Ei.PageArchives - c.HTML(http.StatusOK, "homeLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLFront(c, "archives", h) } func HandleArticlePage(c *gin.Context) { @@ -124,12 +128,13 @@ func HandleArticlePage(c *gin.Context) { h["Title"] = artc.Title + " | " + Ei.BTitle h["Path"] = c.Request.URL.Path h["CurrentPage"] = "post-" + artc.Slug + var name string if path == "blogroll.html" { - h["BlogrollPage"] = true + name = "blogroll" } else if path == "about.html" { - h["AboutPage"] = true + name = "about" } else { - h["ArticlePage"] = true + name = "article" h["Copyright"] = Ei.Copyright if !artc.UpdateTime.IsZero() { h["Days"] = int(time.Now().Sub(artc.UpdateTime).Hours()) / 24 @@ -141,7 +146,8 @@ func HandleArticlePage(c *gin.Context) { } } h["Article"] = artc - c.HTML(http.StatusOK, "homeLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLFront(c, name, h) } func HandleSearchPage(c *gin.Context) { @@ -149,7 +155,6 @@ func HandleSearchPage(c *gin.Context) { h["Version"] = StaticVersion(c) h["Title"] = "站内搜索 | " + Ei.BTitle h["Path"] = "" - h["SearchPage"] = true h["CurrentPage"] = "search-post" q := c.Query("q") @@ -187,7 +192,8 @@ func HandleSearchPage(c *gin.Context) { } } } - c.HTML(http.StatusOK, "homeLayout.html", h) + c.Status(http.StatusOK) + RenderHTMLFront(c, "search", h) } func HandleFeed(c *gin.Context) { @@ -272,3 +278,17 @@ func HandleDisqus(c *gin.Context) { } c.JSON(http.StatusOK, ss) } + +func RenderHTMLFront(c *gin.Context, name string, data gin.H) { + var buf bytes.Buffer + err := Tmpl.ExecuteTemplate(&buf, name, data) + if err != nil { + panic(err) + } + data["LayoutContent"] = template.HTML(buf.String()) + err = Tmpl.ExecuteTemplate(c.Writer, "homeLayout.html", data) + if err != nil { + panic(err) + } + c.Header("Content-Type", "text/html; charset=utf-8") +} diff --git a/helper.go b/helper.go index 34a2661..133d315 100644 --- a/helper.go +++ b/helper.go @@ -1,9 +1,12 @@ package main import ( + "crypto/sha1" "crypto/sha256" "fmt" "io" + "io/ioutil" + "path" "regexp" ) @@ -26,6 +29,28 @@ func VerifyPasswd(origin, name, input string) bool { return origin == EncryptPasswd(name, input) } +func SHA1(data []byte) [sha1.Size]byte { + return sha1.Sum(data) +} + +func ReadDir(dir string, filter func(name string) bool) (files []string) { + fis, err := ioutil.ReadDir(dir) + if err != nil { + return + } + for _, fi := range fis { + if filter(fi.Name()) { + continue + } + if fi.IsDir() { + files = append(files, ReadDir(path.Join(dir, fi.Name()), filter)...) + continue + } + files = append(files, path.Join(dir, fi.Name())) + } + return +} + func IgnoreHtmlTag(src string) string { //去除所有尖括号内的HTML代码 re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") diff --git a/router.go b/router.go index 0352cba..83b24b7 100644 --- a/router.go +++ b/router.go @@ -13,7 +13,10 @@ import ( "github.com/gin-gonic/gin" ) -var router *gin.Engine +var ( + router *gin.Engine + Tmpl *template.Template +) func init() { if setting.Conf.RunMode == setting.PROD { @@ -31,14 +34,15 @@ func init() { }) 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) + Tmpl = template.New("eiblog").Funcs(tmpl.TplFuncMap) + files := ReadDir("views", func(name string) bool { + if name == ".DS_Store" { + return true } - router.SetHTMLTemplate(tmpl) - } else { + return false + }) + _, err := Tmpl.ParseFiles(files...) + if err != nil { logd.Fatal(err) } // 开启静态文件 diff --git a/views/admin/backLayout.html b/views/admin/backLayout.html index 71daaf8..27f379d 100644 --- a/views/admin/backLayout.html +++ b/views/admin/backLayout.html @@ -65,7 +65,7 @@
- {{if .Profile}}{{template "admin-profile" .}}{{else if .Post}}{{template "admin-post" .}}{{else if .Posts}}{{template "admin-posts" .}}{{else if .Series}}{{template "admin-series" .}}{{else if .Serie}}{{template "admin-serie" .}}{{else if .Tags}}{{template "admin-tags" .}}{{else if .General}}{{template "admin-general" .}}{{else if .Discussion}}{{template "admin-discussion" .}}{{else if .Draft}}{{template "admin-draft" .}}{{else if .Trash}}{{template "admin-trash" .}}{{end}} + {{.LayoutContent}}