Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37b2f6e

Browse files
committed
域名:配置文件中优先级最高;控制master等
1 parent 4a5dc33 commit 37b2f6e

File tree

8 files changed

+149
-31
lines changed

8 files changed

+149
-31
lines changed

config/env.sample.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
env = dev
33

44
log_level = DEBUG
5+
domain = xxx
6+
57
cookie_secret = 9HFEp6b2DMn^fRduZc
68
data_path = data/max_online_num
79

10+
; 多台部署时,master 的执行 后台定时任务
11+
is_master = 1
12+
813
; 搜索引擎,避免阅读次数非人为增加
914
spider = spider,bot,nutch,yahoo,gougou,scooter,lilina
1015

src/http/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interf
289289

290290
// websocket主机
291291
if global.OnlineEnv() {
292-
data["wshost"] = logic.WebsiteSetting.Domain
292+
data["wshost"] = config.ConfigFile.MustValue("global", "domain", logic.WebsiteSetting.Domain)
293293
} else {
294294
data["wshost"] = global.App.Host + ":" + global.App.Port
295295
}

src/logic/article.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/PuerkitoBio/goquery"
2020
"github.com/polaris1119/goutils"
2121
"github.com/polaris1119/logger"
22+
"github.com/polaris1119/set"
2223
"github.com/polaris1119/times"
2324
"golang.org/x/net/context"
2425
"golang.org/x/text/encoding/simplifiedchinese"
@@ -326,7 +327,7 @@ func (ArticleLogic) Total() int64 {
326327
}
327328

328329
// FindBy 获取抓取的文章列表(分页)
329-
func (ArticleLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*model.Article {
330+
func (self ArticleLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*model.Article {
330331
objLog := GetLogger(ctx)
331332

332333
dbSession := MasterDB.Where("status IN(?,?)", model.ArticleStatusNew, model.ArticleStatusOnline)
@@ -352,6 +353,8 @@ func (ArticleLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*mo
352353
articles = append(topArticles, articles...)
353354
}
354355

356+
self.fillUser(articles)
357+
355358
return articles
356359
}
357360

@@ -386,7 +389,7 @@ func (ArticleLogic) FindArticleByPage(ctx context.Context, conds map[string]stri
386389
}
387390

388391
// FindByIds 获取多个文章详细信息
389-
func (ArticleLogic) FindByIds(ids []int) []*model.Article {
392+
func (self ArticleLogic) FindByIds(ids []int) []*model.Article {
390393
if len(ids) == 0 {
391394
return nil
392395
}
@@ -396,9 +399,52 @@ func (ArticleLogic) FindByIds(ids []int) []*model.Article {
396399
logger.Errorln("ArticleLogic FindByIds error:", err)
397400
return nil
398401
}
402+
403+
self.fillUser(articles)
404+
399405
return articles
400406
}
401407

408+
func (ArticleLogic) fillUser(articles []*model.Article) {
409+
usernameSet := set.New(set.NonThreadSafe)
410+
uidSet := set.New(set.NonThreadSafe)
411+
for _, article := range articles {
412+
if article.IsSelf {
413+
usernameSet.Add(article.Author)
414+
}
415+
416+
if article.Lastreplyuid != 0 {
417+
uidSet.Add(article.Lastreplyuid)
418+
}
419+
}
420+
if !usernameSet.IsEmpty() {
421+
userMap := DefaultUser.FindUserInfos(nil, set.StringSlice(usernameSet))
422+
for _, article := range articles {
423+
if !article.IsSelf {
424+
continue
425+
}
426+
427+
for _, user := range userMap {
428+
if article.Author == user.Username {
429+
article.User = user
430+
break
431+
}
432+
}
433+
}
434+
}
435+
436+
if !uidSet.IsEmpty() {
437+
replyUserMap := DefaultUser.FindUserInfos(nil, set.IntSlice(uidSet))
438+
for _, article := range articles {
439+
if article.Lastreplyuid == 0 {
440+
continue
441+
}
442+
443+
article.LastReplyUser = replyUserMap[article.Lastreplyuid]
444+
}
445+
}
446+
}
447+
402448
// findByIds 获取多个文章详细信息 包内使用
403449
func (ArticleLogic) findByIds(ids []int) map[int]*model.Article {
404450
if len(ids) == 0 {

src/logic/index.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ func (IndexLogic) FindData(ctx context.Context, tab string) map[string]interface
6363
paginator := NewPaginator(1)
6464
data["topics"] = DefaultTopic.FindAll(ctx, paginator, "topics.mtime DESC", querystring, nids...)
6565
case indexNav.DataSource == "rank":
66-
data["topics"] = DefaultRank.FindDayRank(ctx, model.TypeTopic, times.Format("ymd"), 50, true)
66+
articles := DefaultRank.FindDayRank(ctx, model.TypeArticle, times.Format("ymd"), 25)
67+
articleNum := 0
68+
if articles != nil {
69+
articleNum = len(articles.([]*model.Article))
70+
}
71+
data["articles"] = articles
72+
data["topics"] = DefaultRank.FindDayRank(ctx, model.TypeTopic, times.Format("ymd"), 50-articleNum, true)
6773

6874
newIndexNav := &model.IndexNav{
6975
Tab: indexNav.Tab,
@@ -81,6 +87,8 @@ func (IndexLogic) FindData(ctx context.Context, tab string) map[string]interface
8187
}
8288

8389
data["cur_nav"] = newIndexNav
90+
case indexNav.DataSource == "article":
91+
data["articles"] = DefaultArticle.FindBy(ctx, 50)
8492
}
8593

8694
return data

src/logic/user.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,24 @@ func (UserLogic) EmailOrUsernameExists(ctx context.Context, email, username stri
175175
return true
176176
}
177177

178-
func (self UserLogic) FindUserInfos(ctx context.Context, uids []int) map[int]*model.User {
178+
// FindUserInfos 获得用户信息,uniq 可能是 uid slice 或 username slice
179+
func (self UserLogic) FindUserInfos(ctx context.Context, uniq interface{}) map[int]*model.User {
179180
objLog := GetLogger(ctx)
180-
if len(uids) == 0 {
181-
return nil
181+
182+
field := "uid"
183+
if uids, ok := uniq.([]int); ok {
184+
if len(uids) == 0 {
185+
return nil
186+
}
187+
} else if usernames, ok := uniq.([]string); ok {
188+
if len(usernames) == 0 {
189+
return nil
190+
}
191+
field = "username"
182192
}
183193

184194
usersMap := make(map[int]*model.User)
185-
if err := MasterDB.In("uid", uids).Find(&usersMap); err != nil {
195+
if err := MasterDB.In(field, uniq).Find(&usersMap); err != nil {
186196
objLog.Errorln("user logic FindUserInfos not record found:", err)
187197
return nil
188198
}

src/model/article.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ type Article struct {
5252
Mtime OftenTime `json:"mtime" xorm:"<-"`
5353

5454
IsSelf bool `json:"is_self" xorm:"-"`
55-
User *User `json:"user" xorm:"-"`
55+
User *User `json:"-" xorm:"-"`
5656
// 排行榜阅读量
57-
RankView int `json:"rank_view" xorm:"-"`
57+
RankView int `json:"rank_view" xorm:"-"`
58+
LastReplyUser *User `json:"-" xorm:"-"`
5859
}
5960

6061
func (this *Article) AfterSet(name string, cell xorm.Cell) {

src/server/studygolang/background.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"model"
1414
"time"
1515

16+
"github.com/polaris1119/config"
1617
"github.com/polaris1119/logger"
1718
"github.com/robfig/cron"
1819
)
@@ -32,23 +33,25 @@ func ServeBackGround() {
3233

3334
c := cron.New()
3435

35-
// 每天对非活跃用户降频
36-
c.AddFunc("@daily", decrUserActiveWeight)
36+
if config.ConfigFile.MustBool("global", "is_master", true) {
37+
// 每天对非活跃用户降频
38+
c.AddFunc("@daily", decrUserActiveWeight)
3739

38-
// 生成阅读排行榜
39-
c.AddFunc("@daily", genViewRank)
40+
// 生成阅读排行榜
41+
c.AddFunc("@daily", genViewRank)
4042

41-
// 两分钟刷一次浏览数(TODO:重启丢失问题?信号控制重启?)
42-
c.AddFunc("@every 2m", logic.Views.Flush)
43+
if global.OnlineEnv() {
44+
// 每天生成 sitemap 文件
45+
c.AddFunc("@daily", logic.GenSitemap)
4346

44-
if global.OnlineEnv() {
45-
// 每天生成 sitemap 文件
46-
c.AddFunc("@daily", logic.GenSitemap)
47-
48-
// 给用户发邮件,如通知网站最近的动态,每周的晨读汇总等
49-
c.AddFunc("0 0 4 * * 1", logic.DefaultEmail.EmailNotice)
47+
// 给用户发邮件,如通知网站最近的动态,每周的晨读汇总等
48+
c.AddFunc("0 0 4 * * 1", logic.DefaultEmail.EmailNotice)
49+
}
5050
}
5151

52+
// 两分钟刷一次浏览数(TODO:重启丢失问题?信号控制重启?)
53+
c.AddFunc("@every 2m", logic.Views.Flush)
54+
5255
c.Start()
5356
}
5457

template/new_index.html

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
{{end}}
1919
</div>
2020
{{end}}
21+
22+
{{with or .feeds .topics .articles}}
2123

22-
{{range .feeds}}
24+
{{range $.feeds}}
2325
<div class="cell item">
2426
<table cellpadding="0" cellspacing="0" border="0" width="100%">
2527
<tbody><tr>
@@ -69,16 +71,9 @@
6971
</tr>
7072
</tbody></table>
7173
</div>
72-
{{else}}
73-
{{if not .topics}}
74-
<div class="cell item">
75-
<div class="pull-right"><a href="/topics/new">我来发布</a> <i class="fa fa-caret-right gray"></i></div>
76-
<span class="text-center" style="display: inherit">暂无主题</span>
77-
</div>
78-
{{end}}
7974
{{end}}
8075

81-
{{range .topics}}
76+
{{range $.topics}}
8277
<div class="cell item">
8378
<table cellpadding="0" cellspacing="0" border="0" width="100%">
8479
<tbody><tr>
@@ -110,6 +105,56 @@
110105
</tbody></table>
111106
</div>
112107
{{end}}
108+
109+
{{range $.articles}}
110+
<div class="cell item">
111+
<table cellpadding="0" cellspacing="0" border="0" width="100%">
112+
<tbody><tr>
113+
114+
{{if .User}}
115+
<td width="48" valign="top" align="center"><a href="/user/{{.User.Username}}"><img src="{{gravatar .User.Avatar .User.Email 48 $.is_https}}" class="avatar"></a></td>
116+
{{else}}
117+
<td></td>
118+
{{end}}
119+
<td width="10"></td>
120+
121+
<td width="auto" valign="middle">
122+
<span class="item_title"><a href="/articles/{{.Id}}" class="noul">{{.Title}}</a></span>
123+
<div class="sep5"></div>
124+
<span class="small cc">
125+
{{if .Top}}
126+
<span style="color: #ff7700; border: 1px solid #ff7700;">置顶</span>
127+
{{end}}
128+
<a class="node" href="/articles">文章</a> &nbsp;•&nbsp;
129+
{{if .IsSelf}}
130+
<strong><a href="/user/{{.User.Username}}" class="noul">{{.User.Username}}</a></strong>
131+
{{else}}
132+
<span>{{.AuthorTxt}}</span>
133+
{{end}}
134+
&nbsp;•&nbsp;
135+
{{if .Lastreplyuid}}
136+
<span title="{{.Lastreplytime}}" class="timeago"></span>&nbsp;•&nbsp;最后回复来自 <strong><a href="/user/{{.LastReplyUser.Username}}" class="noul">{{.LastReplyUser.Username}}</a></strong>
137+
{{else}}
138+
<span title="{{.Ctime}}" class="timeago"></span>发布
139+
{{end}}
140+
</td>
141+
{{if .Cmtnum}}
142+
<td width="70" align="right" valign="middle">
143+
<a href="/articles/{{.Id}}" class="count_livid" title="{{.Cmtnum}}">{{.Cmtnum}}</a>
144+
</td>
145+
{{end}}
146+
</tr>
147+
</tbody></table>
148+
</div>
149+
{{end}}
150+
151+
{{else}}
152+
153+
<div class="cell item">
154+
<div class="pull-right"><a href="/topics/new">我来发布</a> <i class="fa fa-caret-right gray"></i></div>
155+
<span class="text-center" style="display: inherit">暂无主题</span>
156+
</div>
157+
{{end}}
113158

114159
</div>
115160

0 commit comments

Comments
 (0)