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

Skip to content

Commit a3d1910

Browse files
committed
sitemap time bugfix; add tool admin
1 parent 05b7c1c commit a3d1910

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
package admin
8+
9+
import (
10+
"net/http"
11+
12+
"filter"
13+
"service"
14+
)
15+
16+
// uri: /admin/tool/sitemap
17+
func GenSitemapHandler(rw http.ResponseWriter, req *http.Request) {
18+
service.GenSitemap()
19+
// 设置内容模板
20+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/tool/sitemap.html")
21+
}

websites/code/studygolang/src/model/reading.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type MorningReading struct {
2323
Id int `json:"id" pk:"1"`
2424
Content string `json:"content"`
2525
Rtype int `json:"rtype"`
26-
Inner int `json:"inner,omitempty"`
26+
Inner int `json:"inner"`
2727
Url string `json:"url"`
2828
Moreurls string `json:"moreurls"`
2929
Username string `json:"username"`

websites/code/studygolang/src/server/studygolang/router.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func initRouter() *mux.Router {
196196
subrouter.HandleFunc("/reading/query.html", admin.ReadingQueryHandler)
197197
subrouter.HandleFunc("/reading/publish", admin.PublishReadingHandler)
198198

199+
// 工具类
200+
subrouter.HandleFunc("/tool/sitemap", admin.GenSitemapHandler)
201+
199202
apirouter := router.PathPrefix("/api").Subrouter()
200203
apirouter.HandleFunc("/user/login", api.LoginHandler)
201204
apirouter.HandleFunc("/blog/category/all", api.BlogCategoryHandler)

websites/code/studygolang/src/service/reading.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ func SaveReading(form url.Values, username string) (errMsg string, err error) {
102102
return
103103
}
104104

105+
reading.Moreurls = strings.TrimSpace(reading.Moreurls)
106+
if strings.Contains(reading.Moreurls, "\n") {
107+
reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",")
108+
}
109+
105110
reading.Username = username
106111

107112
logger.Infoln(reading.Rtype, "id=", reading.Id)

websites/code/studygolang/src/service/sitemap.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package service
88

99
import (
10-
"fmt"
1110
"os"
1211
"strconv"
1312
"text/template"
@@ -19,7 +18,19 @@ import (
1918
"util"
2019
)
2120

22-
var sitemapTpl = template.Must(template.ParseFiles(config.ROOT + "/template/sitemap.xml"))
21+
// 自定义模板函数
22+
var funcMap = template.FuncMap{
23+
"time_format": func(s string) string {
24+
t, err := time.ParseInLocation("2006-01-02 15:04:05", s, time.Local)
25+
if err != nil {
26+
return s
27+
}
28+
29+
return t.Format(time.RFC3339)
30+
},
31+
}
32+
33+
var sitemapTpl = template.Must(template.New("sitemap.xml").Funcs(funcMap).ParseFiles(config.ROOT + "/template/sitemap.xml"))
2334
var sitemapIndexTpl = template.Must(template.ParseFiles(config.ROOT + "/template/sitemapindex.xml"))
2435

2536
var sitemapPath = config.ROOT + "/sitemap/"
@@ -205,8 +216,6 @@ func GenSitemap() {
205216
if err != nil {
206217
logger.Errorln("execute sitemap index template error:", err)
207218
}
208-
209-
fmt.Println("finish")
210219
}
211220

212221
func output(filename string, data map[string]interface{}) (err error) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{define "content"}}
2+
<div class="pageheader notab">
3+
<h1 class="pagetitle">生成sitemap</h1>
4+
</div><!--pageheader-->
5+
6+
<div id="contentwraapper" class="contentwrapper">
7+
<div id="tooltip" class="red">生成成功!<a href="/sitemap/sitemapindex.xml" target="_blank">查看</a></div>
8+
<br/>
9+
</div><!--contentwrapper-->
10+
{{end}}

websites/code/studygolang/template/sitemap.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@
99
</url>{{range .articles}}
1010
<url>
1111
<loc>{{$.home.loc}}/articles/{{.Id}}</loc>
12-
<lastmod>{{.Mtime}}</lastmod>
12+
<lastmod>{{time_format .Mtime}}</lastmod>
1313
<changefreq>weekly</changefreq>
1414
<priority>0.8</priority>
1515
</url>{{end}}{{range .topics}}
1616
<url>
1717
<loc>{{$.home.loc}}/topics/{{.Tid}}</loc>
18-
<lastmod>{{.Mtime}}</lastmod>
18+
<lastmod>{{time_format .Mtime}}</lastmod>
1919
<changefreq>weekly</changefreq>
2020
<priority>0.8</priority>
2121
</url>{{end}}{{range .resources}}
2222
<url>
2323
<loc>{{$.home.loc}}/resources/{{.Id}}</loc>
24-
<lastmod>{{.Mtime}}</lastmod>
24+
<lastmod>{{time_format .Mtime}}</lastmod>
2525
<changefreq>monthly</changefreq>
2626
<priority>0.6</priority>
2727
</url>{{end}}{{range .projects}}
2828
<url>
2929
<loc>{{$.home.loc}}/p/{{if .Uri}}{{.Uri}}{{else}}{{.Id}}{{end}}</loc>
30-
<lastmod>{{.Mtime}}</lastmod>
30+
<lastmod>{{time_format .Mtime}}</lastmod>
3131
<changefreq>monthly</changefreq>
3232
<priority>0.6</priority>
3333
</url>{{end}}{{range .wiki}}
3434
<url>
3535
<loc>{{$.home.loc}}/wiki/{{.Uri}}</loc>
36-
<lastmod>{{.Mtime}}</lastmod>
36+
<lastmod>{{time_format .Mtime}}</lastmod>
3737
<changefreq>monthly</changefreq>
3838
<priority>0.4</priority>
3939
</url>{{end}}

0 commit comments

Comments
 (0)