add comments

This commit is contained in:
henry.chen
2017-08-08 12:45:58 +08:00
parent 3ddd2a0b33
commit 19af9376cb
11 changed files with 57 additions and 7 deletions

13
api.go
View File

@@ -16,11 +16,15 @@ import (
) )
const ( const (
// 成功
NOTICE_SUCCESS = "success" NOTICE_SUCCESS = "success"
NOTICE_NOTICE = "notice" // 注意
NOTICE_ERROR = "error" NOTICE_NOTICE = "notice"
// 错误
NOTICE_ERROR = "error"
) )
// 全局 API
var APIs = make(map[string]func(c *gin.Context)) var APIs = make(map[string]func(c *gin.Context))
func init() { func init() {
@@ -61,6 +65,7 @@ func apiAccount(c *gin.Context) {
responseNotice(c, NOTICE_NOTICE, "参数错误", "") responseNotice(c, NOTICE_NOTICE, "参数错误", "")
return return
} }
err := UpdateAccountField(bson.M{"$set": bson.M{"email": e, "phonen": pn, "address": ad}}) err := UpdateAccountField(bson.M{"$set": bson.M{"email": e, "phonen": pn, "address": ad}})
if err != nil { if err != nil {
responseNotice(c, NOTICE_NOTICE, err.Error(), "") responseNotice(c, NOTICE_NOTICE, err.Error(), "")
@@ -83,6 +88,7 @@ func apiBlog(c *gin.Context) {
responseNotice(c, NOTICE_NOTICE, "参数错误", "") responseNotice(c, NOTICE_NOTICE, "参数错误", "")
return return
} }
err := UpdateAccountField(bson.M{"$set": bson.M{"blogger.blogname": bn, "blogger.btitle": bt, "blogger.beian": ba, "blogger.subtitle": st, "blogger.seriessay": ss, "blogger.archivessay": as}}) err := UpdateAccountField(bson.M{"$set": bson.M{"blogger.blogname": bn, "blogger.btitle": bt, "blogger.beian": ba, "blogger.subtitle": st, "blogger.seriessay": ss, "blogger.archivessay": as}})
if err != nil { if err != nil {
responseNotice(c, NOTICE_NOTICE, err.Error(), "") responseNotice(c, NOTICE_NOTICE, err.Error(), "")
@@ -117,6 +123,7 @@ func apiPassword(c *gin.Context) {
return return
} }
newPwd := EncryptPasswd(Ei.Username, nw) newPwd := EncryptPasswd(Ei.Username, nw)
err := UpdateAccountField(bson.M{"$set": bson.M{"password": newPwd}}) err := UpdateAccountField(bson.M{"$set": bson.M{"password": newPwd}})
if err != nil { if err != nil {
responseNotice(c, NOTICE_NOTICE, err.Error(), "") responseNotice(c, NOTICE_NOTICE, err.Error(), "")
@@ -136,6 +143,7 @@ func apiPostDelete(c *gin.Context) {
} }
responseNotice(c, NOTICE_SUCCESS, "删除成功", "") responseNotice(c, NOTICE_SUCCESS, "删除成功", "")
}() }()
err = c.Request.ParseForm() err = c.Request.ParseForm()
if err != nil { if err != nil {
return return
@@ -187,6 +195,7 @@ func apiPostAdd(c *gin.Context) {
c.Redirect(http.StatusFound, "/admin/manage-posts") c.Redirect(http.StatusFound, "/admin/manage-posts")
} }
}() }()
do = c.PostForm("do") // auto or save or publish do = c.PostForm("do") // auto or save or publish
slug := c.PostForm("slug") slug := c.PostForm("slug")
title := c.PostForm("title") title := c.PostForm("title")

View File

@@ -25,6 +25,7 @@ func isLogin(c *gin.Context) bool {
return true return true
} }
// 登陆过滤
func AuthFilter() gin.HandlerFunc { func AuthFilter() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
if !isLogin(c) { if !isLogin(c) {
@@ -51,6 +52,7 @@ func HandleLogin(c *gin.Context) {
RenderHTMLBack(c, "login.html", gin.H{"BTitle": Ei.BTitle}) RenderHTMLBack(c, "login.html", gin.H{"BTitle": Ei.BTitle})
} }
// 登陆接口
func HandleLoginPost(c *gin.Context) { func HandleLoginPost(c *gin.Context) {
user := c.PostForm("user") user := c.PostForm("user")
pwd := c.PostForm("password") pwd := c.PostForm("password")
@@ -120,6 +122,7 @@ func HandlePost(c *gin.Context) {
RenderHTMLBack(c, "admin-post", h) RenderHTMLBack(c, "admin-post", h)
} }
// 删除草稿
func HandleDraftDelete(c *gin.Context) { func HandleDraftDelete(c *gin.Context) {
id, err := strconv.Atoi(c.Query("cid")) id, err := strconv.Atoi(c.Query("cid"))
if err != nil || id < 1 { if err != nil || id < 1 {
@@ -184,6 +187,7 @@ func HandleSeries(c *gin.Context) {
RenderHTMLBack(c, "admin-series", h) RenderHTMLBack(c, "admin-series", h)
} }
// 编辑专题
func HandleSerie(c *gin.Context) { func HandleSerie(c *gin.Context) {
h := GetBack() h := GetBack()
id, err := strconv.Atoi(c.Query("mid")) id, err := strconv.Atoi(c.Query("mid"))
@@ -276,6 +280,7 @@ func HandleAPI(c *gin.Context) {
api(c) api(c)
} }
// 渲染 html
func RenderHTMLBack(c *gin.Context, name string, data gin.H) { func RenderHTMLBack(c *gin.Context, name string, data gin.H) {
if name == "login.html" { if name == "login.html" {
err := Tmpl.ExecuteTemplate(c.Writer, name, data) err := Tmpl.ExecuteTemplate(c.Writer, name, data)

View File

@@ -6,25 +6,30 @@ import (
"time" "time"
) )
// 检查 email
func CheckEmail(e string) bool { func CheckEmail(e string) bool {
reg := regexp.MustCompile(`^(\w)+([\.\-]\w+)*@(\w)+((\.\w+)+)$`) reg := regexp.MustCompile(`^(\w)+([\.\-]\w+)*@(\w)+((\.\w+)+)$`)
return reg.MatchString(e) return reg.MatchString(e)
} }
// 检查 domain
func CheckDomain(domain string) bool { func CheckDomain(domain string) bool {
reg := regexp.MustCompile(`^(http://|https://)?[0-9a-zA-Z]+[0-9a-zA-Z\.-]*\.[a-zA-Z]{2,4}$`) reg := regexp.MustCompile(`^(http://|https://)?[0-9a-zA-Z]+[0-9a-zA-Z\.-]*\.[a-zA-Z]{2,4}$`)
return reg.MatchString(domain) return reg.MatchString(domain)
} }
// 检查 sms
func CheckSMS(sms string) bool { func CheckSMS(sms string) bool {
reg := regexp.MustCompile(`^\+\d+$`) reg := regexp.MustCompile(`^\+\d+$`)
return reg.MatchString(sms) return reg.MatchString(sms)
} }
// 检查 password
func CheckPwd(pwd string) bool { func CheckPwd(pwd string) bool {
return len(pwd) > 5 && len(pwd) < 19 return len(pwd) > 5 && len(pwd) < 19
} }
// 检查日期
func CheckDate(date string) time.Time { func CheckDate(date string) time.Time {
if t, err := time.ParseInLocation("2006-01-02 15:04", date, time.Local); err == nil { if t, err := time.ParseInLocation("2006-01-02 15:04", date, time.Local); err == nil {
return t return t
@@ -32,6 +37,7 @@ func CheckDate(date string) time.Time {
return time.Now() return time.Now()
} }
// 检查 id
func CheckSerieID(sid string) int32 { func CheckSerieID(sid string) int32 {
if id, err := strconv.Atoi(sid); err == nil { if id, err := strconv.Atoi(sid); err == nil {
return int32(id) return int32(id)
@@ -39,6 +45,7 @@ func CheckSerieID(sid string) int32 {
return 0 return 0
} }
// bool
func CheckBool(str string) bool { func CheckBool(str string) bool {
return str == "true" || str == "1" return str == "true" || str == "1"
} }

3
db.go
View File

@@ -273,6 +273,7 @@ func PageList(p, n int) (prev int, next int, artcs []*Article) {
return return
} }
// 管理 tag
func ManageTagsArticle(artc *Article, s bool, do string) { func ManageTagsArticle(artc *Article, s bool, do string) {
switch do { switch do {
case ADD: case ADD:
@@ -297,6 +298,7 @@ func ManageTagsArticle(artc *Article, s bool, do string) {
} }
} }
// 管理专题
func ManageSeriesArticle(artc *Article, s bool, do string) { func ManageSeriesArticle(artc *Article, s bool, do string) {
switch do { switch do {
case ADD: case ADD:
@@ -325,6 +327,7 @@ func ManageSeriesArticle(artc *Article, s bool, do string) {
} }
} }
// 管理归档
func ManageArchivesArticle(artc *Article, s bool, do string) { func ManageArchivesArticle(artc *Article, s bool, do string) {
switch do { switch do {
case ADD: case ADD:

View File

@@ -152,11 +152,8 @@ func (s *ElasticService) Do(req *http.Request) (interface{}, error) {
return b, nil return b, nil
case "HEAD": case "HEAD":
return resp.StatusCode, nil return resp.StatusCode, nil
default:
return nil, errors.New("unknown methods")
} }
return nil, nil return nil, errors.New("unknown methods")
} }
func CreateIndexAndMappings(index, typ string, mappings []byte) (err error) { func CreateIndexAndMappings(index, typ string, mappings []byte) (err error) {

View File

@@ -91,6 +91,7 @@ func GetBase() gin.H {
} }
} }
// not found
func HandleNotFound(c *gin.Context) { func HandleNotFound(c *gin.Context) {
h := GetBase() h := GetBase()
h["Version"] = StaticVersion(c) h["Version"] = StaticVersion(c)
@@ -101,6 +102,7 @@ func HandleNotFound(c *gin.Context) {
RenderHTMLFront(c, "notfound", h) RenderHTMLFront(c, "notfound", h)
} }
// 首页
func HandleHomePage(c *gin.Context) { func HandleHomePage(c *gin.Context) {
h := GetBase() h := GetBase()
h["Version"] = StaticVersion(c) h["Version"] = StaticVersion(c)
@@ -117,6 +119,7 @@ func HandleHomePage(c *gin.Context) {
RenderHTMLFront(c, "home", h) RenderHTMLFront(c, "home", h)
} }
// 专题页
func HandleSeriesPage(c *gin.Context) { func HandleSeriesPage(c *gin.Context) {
h := GetBase() h := GetBase()
h["Version"] = StaticVersion(c) h["Version"] = StaticVersion(c)
@@ -129,6 +132,7 @@ func HandleSeriesPage(c *gin.Context) {
RenderHTMLFront(c, "series", h) RenderHTMLFront(c, "series", h)
} }
// 归档页
func HandleArchivesPage(c *gin.Context) { func HandleArchivesPage(c *gin.Context) {
h := GetBase() h := GetBase()
h["Version"] = StaticVersion(c) h["Version"] = StaticVersion(c)
@@ -141,6 +145,7 @@ func HandleArchivesPage(c *gin.Context) {
RenderHTMLFront(c, "archives", h) RenderHTMLFront(c, "archives", h)
} }
// 文章
func HandleArticlePage(c *gin.Context) { func HandleArticlePage(c *gin.Context) {
path := c.Param("slug") path := c.Param("slug")
if !strings.HasSuffix(path, ".html") || Ei.MapArticles[path[:len(path)-5]] == nil { if !strings.HasSuffix(path, ".html") || Ei.MapArticles[path[:len(path)-5]] == nil {
@@ -178,6 +183,7 @@ func HandleArticlePage(c *gin.Context) {
RenderHTMLFront(c, name, h) RenderHTMLFront(c, name, h)
} }
// 搜索页
func HandleSearchPage(c *gin.Context) { func HandleSearchPage(c *gin.Context) {
h := GetBase() h := GetBase()
h["Version"] = StaticVersion(c) h["Version"] = StaticVersion(c)
@@ -220,6 +226,7 @@ func HandleSearchPage(c *gin.Context) {
RenderHTMLFront(c, "search", h) RenderHTMLFront(c, "search", h)
} }
// 评论页
func HandleDisqusFrom(c *gin.Context) { func HandleDisqusFrom(c *gin.Context) {
params := strings.Split(c.Param("slug"), "|") params := strings.Split(c.Param("slug"), "|")
if len(params) != 4 || params[1] == "" { if len(params) != 4 || params[1] == "" {
@@ -240,26 +247,32 @@ func HandleDisqusFrom(c *gin.Context) {
c.Header("Content-Type", "text/html; charset=utf-8") c.Header("Content-Type", "text/html; charset=utf-8")
} }
// feed
func HandleFeed(c *gin.Context) { func HandleFeed(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/feed.xml") http.ServeFile(c.Writer, c.Request, "static/feed.xml")
} }
// opensearch
func HandleOpenSearch(c *gin.Context) { func HandleOpenSearch(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/opensearch.xml") http.ServeFile(c.Writer, c.Request, "static/opensearch.xml")
} }
// robots
func HandleRobots(c *gin.Context) { func HandleRobots(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/robots.txt") http.ServeFile(c.Writer, c.Request, "static/robots.txt")
} }
// sitemap
func HandleSitemap(c *gin.Context) { func HandleSitemap(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/sitemap.xml") http.ServeFile(c.Writer, c.Request, "static/sitemap.xml")
} }
// cross domain
func HandleCrossDomain(c *gin.Context) { func HandleCrossDomain(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/crossdomain.xml") http.ServeFile(c.Writer, c.Request, "static/crossdomain.xml")
} }
// favicon
func HandleFavicon(c *gin.Context) { func HandleFavicon(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "static/favicon.ico") http.ServeFile(c.Writer, c.Request, "static/favicon.ico")
} }

View File

@@ -43,14 +43,17 @@ func EncryptPasswd(name, pass string) string {
return fmt.Sprintf("%x", h.Sum(nil)) return fmt.Sprintf("%x", h.Sum(nil))
} }
// 验证密码
func VerifyPasswd(origin, name, input string) bool { func VerifyPasswd(origin, name, input string) bool {
return origin == EncryptPasswd(name, input) return origin == EncryptPasswd(name, input)
} }
// 随机 uuid
func RandUUIDv4() string { func RandUUIDv4() string {
return uuid.NewV4().String() return uuid.NewV4().String()
} }
// 读取目录
func ReadDir(dir string, filter func(name string) bool) (files []string) { func ReadDir(dir string, filter func(name string) bool) (files []string) {
fis, err := ioutil.ReadDir(dir) fis, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
@@ -69,6 +72,7 @@ func ReadDir(dir string, filter func(name string) bool) (files []string) {
return return
} }
// 去掉 html tag
func IgnoreHtmlTag(src string) string { func IgnoreHtmlTag(src string) string {
// 去除所有尖括号内的HTML代码 // 去除所有尖括号内的HTML代码
re, _ := regexp.Compile(`<[\S\s]+?>`) re, _ := regexp.Compile(`<[\S\s]+?>`)
@@ -79,6 +83,7 @@ func IgnoreHtmlTag(src string) string {
return re.ReplaceAllString(src, "") return re.ReplaceAllString(src, "")
} }
// 获取第一张图片
func PickFirstImage(html string) string { func PickFirstImage(html string) string {
re, _ := regexp.Compile(`data-src="(.*?)"`) re, _ := regexp.Compile(`data-src="(.*?)"`)
sli := re.FindAllStringSubmatch(html, 1) sli := re.FindAllStringSubmatch(html, 1)
@@ -98,6 +103,7 @@ const (
YEARS_AGO = "%d年前" YEARS_AGO = "%d年前"
) )
// 时间转换为间隔
func ConvertStr(str string) string { func ConvertStr(str string) string {
t, err := time.ParseInLocation("2006-01-02T15:04:05", str, time.UTC) t, err := time.ParseInLocation("2006-01-02T15:04:05", str, time.UTC)
if err != nil { if err != nil {

View File

@@ -39,6 +39,7 @@ func onProgress(fsize, uploaded int64) {
} }
} }
// 上传文件
func FileUpload(name string, size int64, data io.Reader) (string, error) { func FileUpload(name string, size int64, data io.Reader) (string, error) {
if setting.Conf.Kodo.AccessKey == "" || setting.Conf.Kodo.SecretKey == "" { if setting.Conf.Kodo.AccessKey == "" || setting.Conf.Kodo.SecretKey == "" {
return "", errors.New("qiniu config error") return "", errors.New("qiniu config error")
@@ -76,6 +77,7 @@ func FileUpload(name string, size int64, data io.Reader) (string, error) {
return url, nil return url, nil
} }
// 删除文件
func FileDelete(name string) error { func FileDelete(name string) error {
// new一个Bucket管理对象 // new一个Bucket管理对象
c := kodo.New(0, qiniu_cfg) c := kodo.New(0, qiniu_cfg)

View File

@@ -7,11 +7,13 @@ import (
) )
func TestUpload(t *testing.T) { func TestUpload(t *testing.T) {
path := "/Users/chen/Desktop/png-MicroService-by-StuQ.png" path := "qiniu.go"
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer file.Close()
info, _ := file.Stat() info, _ := file.Stat()
url, err := FileUpload(info.Name(), info.Size(), file) url, err := FileUpload(info.Name(), info.Size(), file)
if err != nil { if err != nil {

View File

@@ -90,6 +90,7 @@ func init() {
} }
} }
// 开始运行
func Run() { func Run() {
var ( var (
endRunning = make(chan bool, 1) endRunning = make(chan bool, 1)

5
xml.go
View File

@@ -32,6 +32,7 @@ func init() {
doCrossdomain() doCrossdomain()
} }
// 定时更新 feed
func doFeed() { func doFeed() {
tpl := tpls.Lookup("feedTpl.xml") tpl := tpls.Lookup("feedTpl.xml")
if tpl == nil { if tpl == nil {
@@ -63,6 +64,7 @@ func doFeed() {
time.AfterFunc(time.Hour*4, doFeed) time.AfterFunc(time.Hour*4, doFeed)
} }
// 定时更新 sitemap
func doSitemap() { func doSitemap() {
tpl := tpls.Lookup("sitemapTpl.xml") tpl := tpls.Lookup("sitemapTpl.xml")
if tpl == nil { if tpl == nil {
@@ -84,6 +86,7 @@ func doSitemap() {
time.AfterFunc(time.Hour*24, doFeed) time.AfterFunc(time.Hour*24, doFeed)
} }
// 渲染 opensearch
func doOpensearch() { func doOpensearch() {
tpl := tpls.Lookup("opensearchTpl.xml") tpl := tpls.Lookup("opensearchTpl.xml")
if tpl == nil { if tpl == nil {
@@ -108,6 +111,7 @@ func doOpensearch() {
} }
} }
// 渲染 robots
func doRobots() { func doRobots() {
tpl := tpls.Lookup("robotsTpl.xml") tpl := tpls.Lookup("robotsTpl.xml")
if tpl == nil { if tpl == nil {
@@ -130,6 +134,7 @@ func doRobots() {
} }
} }
// 渲染 cross domain
func doCrossdomain() { func doCrossdomain() {
tpl := tpls.Lookup("crossdomainTpl.xml") tpl := tpls.Lookup("crossdomainTpl.xml")
if tpl == nil { if tpl == nil {