From 487d35dae22b0a7d8f41ef5f2519fa415d953102 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Tue, 8 Aug 2017 20:59:45 +0800 Subject: [PATCH] add comments --- back.go | 1 + db.go | 5 +++++ elasticsearch.go | 11 +++++++++++ front.go | 1 + helper.go | 3 +++ ping.go | 1 + qiniu.go | 2 ++ router.go | 2 ++ 8 files changed, 26 insertions(+) diff --git a/back.go b/back.go index 65416a3..3514a28 100644 --- a/back.go +++ b/back.go @@ -16,6 +16,7 @@ import ( "gopkg.in/mgo.v2/bson" ) +// 是否登录 func isLogin(c *gin.Context) bool { session := sessions.Default(c) v := session.Get("username") diff --git a/db.go b/db.go index 8ffda27..e5ecacd 100644 --- a/db.go +++ b/db.go @@ -377,6 +377,7 @@ func GenerateExcerptAndRender(artc *Article) { artc.Content = artc.Content[index:] } + // 查找目录 content := renderPage([]byte(artc.Content)) index := regH.FindIndex(content) if index != nil { @@ -423,7 +424,9 @@ func AddArticle(artc *Article) error { break } } + if !artc.IsDraft { + // 正式发布文章 defer GenerateExcerptAndRender(artc) Ei.MapArticles[artc.Slug] = artc Ei.Articles = append([]*Article{artc}, Ei.Articles...) @@ -463,6 +466,7 @@ func DelArticles(ids ...int32) error { return nil } +// 从链表里删除文章 func DelFromLinkedList(artc *Article) { if artc.Prev == nil && artc.Next != nil { artc.Next.Prev = nil @@ -474,6 +478,7 @@ func DelFromLinkedList(artc *Article) { } } +// 将文章添加到链表 func AddToLinkedList(id int32) { i, artc := GetArticle(id) if i == 0 && Ei.Articles[i+1].ID >= setting.Conf.General.StartID { diff --git a/elasticsearch.go b/elasticsearch.go index ce89096..b4cb1c3 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -25,11 +25,13 @@ const ( var es *ElasticService +// 初始化 Elasticsearch 服务器 func init() { es = &ElasticService{url: "http://elasticsearch:9200", c: new(http.Client)} initIndex() } +// 创建索引 func initIndex() { mappings := fmt.Sprintf(`{"mappings":{"%s":{"properties":{"content":{"analyzer":"ik_syno","search_analyzer":"ik_syno","term_vector":"with_positions_offsets","type":"string"},"date":{"index":"not_analyzed","type":"date"},"slug":{"type":"string"},"tag":{"index":"not_analyzed","type":"string"},"title":{"analyzer":"ik_syno","search_analyzer":"ik_syno","term_vector":"with_positions_offsets","type":"string"}}}}}`, TYPE) err := CreateIndexAndMappings(INDEX, TYPE, []byte(mappings)) @@ -38,6 +40,7 @@ func initIndex() { } } +// 查询 func Elasticsearch(qStr string, size, from int) *ESSearchResult { // 分析查询字符串 reg := regexp.MustCompile(`(tag|slug|date):`) @@ -95,6 +98,7 @@ func Elasticsearch(qStr string, size, from int) *ESSearchResult { return docs } +// 添加或更新索引 func ElasticIndex(artc *Article) error { img := PickFirstImage(artc.Content) mapping := map[string]interface{}{ @@ -109,6 +113,7 @@ func ElasticIndex(artc *Article) error { return IndexOrUpdateDocument(INDEX, TYPE, artc.ID, b) } +// 删除索引 func ElasticDelIndex(ids []int32) error { var target []string for _, id := range ids { @@ -127,10 +132,12 @@ type IndicesCreateResult struct { Acknowledged bool `json:"acknowledged"` } +// 返回 url func (s *ElasticService) ParseURL(format string, params ...interface{}) string { return fmt.Sprintf(s.url+format, params...) } +// Elastic 相关操作请求 func (s *ElasticService) Do(req *http.Request) (interface{}, error) { resp, err := s.c.Do(req) if err != nil { @@ -184,6 +191,7 @@ func CreateIndexAndMappings(index, typ string, mappings []byte) (err error) { return nil } +// 创建或更新索引 func IndexOrUpdateDocument(index, typ string, id int32, doc []byte) (err error) { req, err := http.NewRequest("PUT", es.ParseURL("/%s/%s/%d", index, typ, id), bytes.NewReader(doc)) if err != nil { @@ -210,6 +218,7 @@ type ESDeleteResult struct { } `json:"iterms"` } +// 删除文档 func DeleteDocument(index, typ string, ids []string) error { var buff bytes.Buffer for _, id := range ids { @@ -244,6 +253,7 @@ func DeleteDocument(index, typ string, ids []string) error { return nil } +// 查询结果 type ESSearchResult struct { Took float32 `json:"took"` Hits struct { @@ -265,6 +275,7 @@ type ESSearchResult struct { } `json:"hits"` } +// DSL 语句查询文档 func IndexQueryDSL(index, typ string, size, from int, dsl []byte) (*ESSearchResult, error) { req, err := http.NewRequest("POST", es.ParseURL("/%s/%s/_search?size=%d&from=%d", index, typ, size, from), bytes.NewReader(dsl)) if err != nil { diff --git a/front.go b/front.go index 6cd46a4..c4e3bf0 100644 --- a/front.go +++ b/front.go @@ -437,6 +437,7 @@ func HandleDisqusCreate(c *gin.Context) { } } +// 渲染页面 func RenderHTMLFront(c *gin.Context, name string, data gin.H) { var buf bytes.Buffer err := Tmpl.ExecuteTemplate(&buf, name, data) diff --git a/helper.go b/helper.go index 039706f..e90659e 100644 --- a/helper.go +++ b/helper.go @@ -18,6 +18,7 @@ const ( FAIL ) +// 月份转换 var monthToDays = map[time.Month]int{ time.January: 31, time.February: 28, @@ -129,6 +130,7 @@ func ConvertStr(str string) string { return JUST_NOW } +// 获取天数 func dayIn(year int, m time.Month) int { if m == time.February && isLeap(year) { return 29 @@ -136,6 +138,7 @@ func dayIn(year int, m time.Month) int { return monthToDays[m] } +// 是否是闰年 func isLeap(year int) bool { return year%4 == 0 && (year%100 != 0 || year%400 == 0) } diff --git a/ping.go b/ping.go index 190a7ff..b46dc95 100644 --- a/ping.go +++ b/ping.go @@ -105,6 +105,7 @@ func init() { Pings = append(Pings, pr) } +// ping func DoPings(slug string) { for _, p := range Pings { go p.PingFunc(slug) diff --git a/qiniu.go b/qiniu.go index 553d95c..3b01f18 100644 --- a/qiniu.go +++ b/qiniu.go @@ -30,6 +30,7 @@ type PutRet struct { Key string `json:"key"` } +// 进度条 func onProgress(fsize, uploaded int64) { d := int(float64(uploaded) / float64(fsize) * 100) if fsize == uploaded { @@ -97,6 +98,7 @@ func FileDelete(name string) error { return nil } +// 修复路径 func getKey(name string) string { ext := filepath.Ext(name) var key string diff --git a/router.go b/router.go index bbe3eae..dcb3ecb 100644 --- a/router.go +++ b/router.go @@ -20,10 +20,12 @@ var ( ) func init() { + // 运行模式 if setting.Conf.RunMode == setting.PROD { gin.SetMode(gin.ReleaseMode) logd.SetLevel(logd.Lerror) } + router = gin.Default() store := sessions.NewCookieStore([]byte("eiblog321")) store.Options(sessions.Options{