Init v3.2
This commit is contained in:
25
controller/article.go
Normal file
25
controller/article.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Article(w http.ResponseWriter, r *http.Request) {
|
||||
articleTemplate := models.Template.Article
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
articleTemplate.WriteError(w, err)
|
||||
}
|
||||
key := r.Form.Get("key")
|
||||
|
||||
path := models.ArticleShortUrlMap[key]
|
||||
|
||||
articleDetail, err := models.ReadArticleDetail(path)
|
||||
|
||||
if err != nil {
|
||||
articleTemplate.WriteError(w, err)
|
||||
}
|
||||
|
||||
articleTemplate.WriteData(w, models.BuildViewData("Article", articleDetail))
|
||||
}
|
16
controller/category.go
Normal file
16
controller/category.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/config"
|
||||
"blog/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Category(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
categoriesTemplate := models.Template.Categories
|
||||
|
||||
result := models.GroupByCategory(&models.ArticleList, config.Cfg.CategoryDisplayQuantity)
|
||||
|
||||
categoriesTemplate.WriteData(w, models.BuildViewData("Categories", result))
|
||||
}
|
35
controller/dashboard.go
Normal file
35
controller/dashboard.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/config"
|
||||
"blog/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var dashboardMsg []string
|
||||
dashboardTemplate := models.Template.Dashboard
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
dashboardTemplate.WriteError(w, err)
|
||||
}
|
||||
|
||||
index, err := strconv.Atoi(r.Form.Get("theme"))
|
||||
if err == nil && index < len(config.Cfg.ThemeOption) {
|
||||
config.Cfg.ThemeColor = config.Cfg.ThemeOption[index]
|
||||
dashboardMsg = append(dashboardMsg, "颜色切换成功!")
|
||||
}
|
||||
|
||||
action := r.Form.Get("action")
|
||||
if "updateArticle" == action {
|
||||
models.CompiledContent()
|
||||
dashboardMsg = append(dashboardMsg, "文章更新成功!")
|
||||
}
|
||||
|
||||
dashboardTemplate.WriteData(w, models.BuildViewData("Dashboard", map[string]interface{}{
|
||||
"msg": dashboardMsg,
|
||||
}))
|
||||
|
||||
}
|
26
controller/extraNav.go
Normal file
26
controller/extraNav.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ExtraNav(w http.ResponseWriter, r *http.Request) {
|
||||
extraNavTemplate := models.Template.ExtraNav
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
extraNavTemplate.WriteError(w, err)
|
||||
}
|
||||
|
||||
name := r.Form.Get("name")
|
||||
for _, nav := range models.Navigation {
|
||||
if nav.Title == name {
|
||||
articleDetail, err := models.ReadArticleDetail(nav.Path)
|
||||
if err != nil {
|
||||
extraNavTemplate.WriteError(w, err)
|
||||
}
|
||||
extraNavTemplate.WriteData(w, models.BuildViewData(nav.Title, articleDetail))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
35
controller/index.go
Normal file
35
controller/index.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/config"
|
||||
"blog/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
indexTemplate := models.Template.Index
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
indexTemplate.WriteError(w, err)
|
||||
}
|
||||
|
||||
page, err := strconv.Atoi(r.Form.Get("page"))
|
||||
if err != nil {
|
||||
page = 1
|
||||
}
|
||||
articles := models.ArticleList
|
||||
|
||||
search := r.Form.Get("search")
|
||||
category := r.Form.Get("category")
|
||||
tag := r.Form.Get("tag")
|
||||
|
||||
if search != "" || category != "" || tag != "" {
|
||||
articles = models.ArticleSearch(&articles, search, category, tag)
|
||||
}
|
||||
|
||||
result := models.Pagination(&articles, page, config.Cfg.PageSize)
|
||||
|
||||
indexTemplate.WriteData(w, models.BuildViewData("Blog", result))
|
||||
}
|
61
controller/webhook.go
Normal file
61
controller/webhook.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"blog/models"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GithubHook(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
//err := r.ParseForm()
|
||||
//if err != nil {
|
||||
// SedResponse(w, err.Error())
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//if "" == config.Cfg.WebHookSecret || "push" != r.Header.Get("x-github-event") {
|
||||
// SedResponse(w, "No Configuration WebHookSecret Or Not Pushing Events")
|
||||
// log.Println("No Configuration WebHookSecret Or Not Pushing Events")
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//sign := r.Header.Get("X-Hub-Signature")
|
||||
//
|
||||
//bodyContent, err := ioutil.ReadAll(r.Body)
|
||||
//
|
||||
//if err != nil {
|
||||
// SedResponse(w, err.Error())
|
||||
// log.Println("WebHook err:" + err.Error())
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//if err = r.Body.Close(); err != nil {
|
||||
// SedResponse(w, err.Error())
|
||||
// log.Println("WebHook err:" + err.Error())
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//mac := hmac.New(sha1.New, []byte(config.Cfg.WebHookSecret))
|
||||
//mac.Write(bodyContent)
|
||||
//expectedHash := "sha1=" + hex.EncodeToString(mac.Sum(nil))
|
||||
//
|
||||
//if sign != expectedHash {
|
||||
// SedResponse(w, "WebHook err:Signature does not match")
|
||||
// log.Println("WebHook err:Signature does not match")
|
||||
// return
|
||||
//}
|
||||
|
||||
SedResponse(w, "ok")
|
||||
|
||||
models.CompiledContent()
|
||||
}
|
||||
|
||||
func SedResponse(w http.ResponseWriter, msg string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, err := w.Write([]byte(`{"msg": "` + msg + `"}`))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user