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

Skip to content

Commit 7ae4d76

Browse files
committed
底部增加网站编译信息
1 parent fb230e6 commit 7ae4d76

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

websites/code/studygolang/install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ DATE=`date +%FT%T%z`
2020

2121
gofmt -w src
2222

23-
go install -ldflags "-X util.version "$VERSION" -X util.date "$DATE ./...
23+
go install -ldflags "-X util.Version "$VERSION" -X util.Date "$DATE ./...
2424

2525
export GOPATH="$OLDGOPATH"
2626
export PATH="$OLDPATH"

websites/code/studygolang/install.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ set VERSION=%VERNAME%-%VERCODE%
2323
gofmt -w src
2424

2525
:: -tags "debug" 表示测试
26-
go install -tags "debug" -ldflags "-X util.version "%VERSION%" -X util.date "%date:~3,10%T%time%"" ./...
26+
go install -tags "debug" -ldflags "-X util.Version "%VERSION%" -X util.Date "%date:~3,10%T%time%"" ./...
2727

2828
set GOPATH=%OLDGOPATH%
2929

websites/code/studygolang/src/filter/view.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ func (this *ViewFilter) PostFilter(rw http.ResponseWriter, req *http.Request) bo
212212

213213
// websocket主机
214214
data["wshost"] = config.Config["wshost"]
215+
data["build"] = map[string]string{
216+
"version": util.Version,
217+
"date": util.Date,
218+
}
219+
215220
err = tpl.Execute(rw, data)
216221
if err != nil {
217222
logger.Errorf("执行模板出错(Execute):[%q] %s\n", req.RequestURI, err)

websites/code/studygolang/src/util/version.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
// Author:polaris [email protected]
66

77
/*
8-
Package version sets version information for the binary where it is imported.
8+
sets version information for the binary where it is imported.
99
The version can be retrieved either from the -version command line argument,
1010
or from the /version/ http endpoint.
1111
1212
To include in a project simply import the package and call version.Init().
1313
1414
The version and compile date is stored in version and date variables and
1515
are supposed to be set during compile time. Typically this is done by the
16-
install(bash/bat).
16+
install(bash/bat). Or date is binary modify time.
1717
1818
To set these manually use -ldflags together with -X, like in this example:
1919
@@ -32,32 +32,41 @@ import (
3232
"os"
3333
)
3434

35-
var showVersion = flag.Bool("version", false, "Print version of this binary (only valid if compiled with install(bash/bat))")
35+
var showVersion = flag.Bool("version", false, "Print version of this binary")
3636

3737
var (
38-
version string
39-
date string
38+
Version string
39+
Date string
4040
)
4141

4242
func printVersion(w io.Writer, version string, date string) {
4343
fmt.Fprintf(w, "Version: %s\n", version)
4444
fmt.Fprintf(w, "Binary: %s\n", os.Args[0])
4545
fmt.Fprintf(w, "Compile date: %s\n", date)
46-
fmt.Fprintf(w, "(version and date only valid if compiled with install(bash/bat))\n")
4746
}
4847

49-
// initializes the version flag and /debug/version/ http endpoint.
48+
// initializes the version flag and /version/ http endpoint.
5049
// Note that this method will call flag.Parse if the flags are not already parsed.
5150
func init() {
5251
if !flag.Parsed() {
5352
flag.Parse()
5453
}
54+
55+
if Date == "" {
56+
fileInfo, err := os.Stat(os.Args[0])
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
Date = fileInfo.ModTime().String()
62+
}
63+
5564
if showVersion != nil && *showVersion {
56-
printVersion(os.Stdout, version, date)
65+
printVersion(os.Stdout, Version, Date)
5766
os.Exit(0)
5867
}
5968

6069
http.Handle("/version", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61-
printVersion(w, html.EscapeString(version), html.EscapeString(date))
70+
printVersion(w, html.EscapeString(Version), html.EscapeString(Date))
6271
}))
6372
}

websites/code/studygolang/template/common/layout.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
</p>
111111
<p class="copyright">&copy;2013-2015 studygolang.com 采用 <a href="http://golang.org" target="_blank">Go语言</a> + <a href="http://www.mysql.com/" target="_blank">MYSQL 构建</a>
112112
&nbsp;&nbsp;当前在线:<span id="onlineusers">30</span>人&nbsp;&nbsp;历史最高:<span id="maxonline">300</span>
113+
<br/>
114+
<b>网站编译信息</b>&nbsp;{{if .build.version}}版本:{{.build.version}},{{end}}时间:<attr class="timeago">{{.build.date}}</attr>
113115
<br/>中国 Golang 社区,Go语言学习园地,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。
114116
京ICP备14030343号-1
115117
</p>

0 commit comments

Comments
 (0)