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

Skip to content

Commit eefbea8

Browse files
author
xuxinhua
committed
Merge branch 'master' into develop
2 parents a30f200 + b48e8d8 commit eefbea8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1522
-439
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ studygolang_data.sql
4040

4141
welcome.png
4242

43-
*.json
43+
*.json
44+
45+
.DS_Store

websites/code/gox_dispatcher/install

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
if [ ! -f install ]; then
6+
echo 'install must be run within its container folder' 1>&2
7+
exit 1
8+
fi
9+
10+
CURDIR=`pwd`
11+
OLDGOPATH="$GOPATH"
12+
export GOPATH="$CURDIR"
13+
14+
if [ ! -d "src/github.com/studygolang/mux" ]; then
15+
go get -u -v github.com/studygolang/mux
16+
fi
17+
18+
gofmt -w src
19+
20+
go install gox
21+
22+
export GOPATH="$OLDGOPATH"
23+
export PATH="$OLDPATH"
24+
25+
echo 'finished'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/github.com
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2015 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 http://golang.top
5+
// Author:polaris [email protected]
6+
7+
// golang.org/x/... 系列的包,go get 不下来,原因你懂的
8+
// 该程序解决此问题,要求使用 go get 的客户端,配置如下 host:
9+
// 101.251.196.90 golang.org
10+
package main
11+
12+
import (
13+
"fmt"
14+
"log"
15+
"net/http"
16+
"runtime"
17+
"strings"
18+
"text/template"
19+
20+
"github.com/studygolang/mux"
21+
)
22+
23+
func init() {
24+
runtime.GOMAXPROCS(runtime.NumCPU())
25+
}
26+
27+
func main() {
28+
29+
router := initRouter()
30+
http.Handle("/", router)
31+
log.Fatal(http.ListenAndServe(":9999", nil))
32+
}
33+
34+
// TODO:多人同时请求不同的包,验证可能会失败
35+
var project = ""
36+
37+
func initRouter() *mux.Router {
38+
router := mux.NewRouter()
39+
40+
router.PathPrefix("/x").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
41+
path := req.URL.Path
42+
43+
paths := strings.SplitN(path, "/", 4)
44+
45+
if len(paths) < 3 {
46+
return parseTmpl(rw)
47+
}
48+
project = paths[2]
49+
50+
parseTmpl(rw)
51+
})
52+
53+
return router
54+
}
55+
56+
func parseTmpl(rw http.ResponseWriter) {
57+
tmpl, err := template.New("gox").Parse(tpl)
58+
if err != nil {
59+
fmt.Fprintln(rw, "error:", err)
60+
return
61+
}
62+
63+
err = tmpl.Execute(rw, project)
64+
if err != nil {
65+
fmt.Fprintln(rw, "error:", err)
66+
return
67+
}
68+
}
69+
70+
var tpl = `
71+
<!DOCTYPE html>
72+
<html lang="zh-CN">
73+
<head>
74+
<meta charset="utf-8">
75+
<meta name="go-import" content="golang.org/x/{{.}} git https://github.com/golang/{{.}}">
76+
</head>
77+
<body>
78+
</body>
79+
</html>
80+
`

websites/code/studygolang/conf/config.json.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"data": "data/max_online_num",
1414
"pid": "pid/studygolang.pid",
1515

16+
"unsubscribe_token_key": "xxxxx",
17+
1618
"qiniu_access_key": "xxxxxxxxxxxxx",
1719
"qiniu_secret_key": "xxxxxxxxxxxxxx",
1820
"qiniu_bucket_name": "xxxxxxxxx",

websites/code/studygolang/install

100644100755
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 "$VERSION" -X util/version.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 "%VERSION% ./...
2727

2828
set GOPATH=%OLDGOPATH%
2929

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/github.com
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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 http://golang.top
5+
// Author:polaris [email protected]
6+
7+
package api
8+
9+
import (
10+
"fmt"
11+
"net/http"
12+
"service"
13+
)
14+
15+
func AddRedditResourceHandler(rw http.ResponseWriter, req *http.Request) {
16+
err := service.ParseReddit(req.FormValue("url"))
17+
if err != nil {
18+
fmt.Fprint(rw, err)
19+
return
20+
}
21+
22+
fmt.Fprint(rw, "success")
23+
}

websites/code/studygolang/src/controller/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func RegisterHandler(rw http.ResponseWriter, req *http.Request) {
6565

6666
func sendWelcomeMail(email []string) {
6767
content := `Welcome to Study Golang.<br><br>
68-
欢迎您,成功注册成为 Golang中文社区 | Go语言学习园地 会员<br><br>
68+
欢迎您,成功注册成为 Go语言中文网 | Go语言学习园地 会员<br><br>
6969
Golang中文社区是一个Go语言技术社区,完全用Go语言开发。我们为gopher们提供一个好的学习交流场所。加入到社区中来,参与分享,学习,不断提高吧。前往 <a href="http://studygolang.com">Golang中文社区 | Go语言学习园地</a><br>
70-
<div style="text-align:right;">&copy;2012-2014 studygolang.com Golang中文社区 | Go语言学习园地</div>`
70+
<div style="text-align:right;">&copy;2012-2015 studygolang.com Go语言中文网 | Golang中文社区 | Go语言学习园地</div>`
7171
service.SendMail("Golang中文社区 | Go语言学习园地 注册成功通知", content, email)
7272
}
7373

websites/code/studygolang/src/controller/article.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func ArticleDetailHandler(rw http.ResponseWriter, req *http.Request) {
127127
article.Viewnum++
128128

129129
// 设置内容模板
130-
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html")
130+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html,/template/common/comment.html")
131131
// 设置模板数据
132132
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag, "hadcollect": hadCollect})
133133
}

websites/code/studygolang/src/controller/index.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,11 @@ func WRHandler(rw http.ResponseWriter, req *http.Request) {
111111
// 设置模板数据
112112
filter.SetData(req, map[string]interface{}{"url": tUrl})
113113
}
114+
115+
// PkgdocHandler Go 语言文档中文版
116+
func PkgdocHandler(rw http.ResponseWriter, req *http.Request) {
117+
// 设置内容模板
118+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/pkgdoc.html")
119+
// 设置模板数据
120+
filter.SetData(req, map[string]interface{}{"activeDoc": "active"})
121+
}

websites/code/studygolang/src/controller/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func ProjectDetailHandler(rw http.ResponseWriter, req *http.Request) {
173173
// 为了阅读数即时看到
174174
project.Viewnum++
175175

176-
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/detail.html")
176+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/detail.html,/template/common/comment.html")
177177
filter.SetData(req, map[string]interface{}{"activeProjects": "active", "project": project, "likeflag": likeFlag, "hadcollect": hadCollect})
178178
}
179179

websites/code/studygolang/src/controller/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ResourceDetailHandler(rw http.ResponseWriter, req *http.Request) {
7373

7474
service.Views.Incr(req, model.TYPE_RESOURCE, util.MustInt(vars["id"]))
7575

76-
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/detail.html")
76+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/detail.html,/template/common/comment.html")
7777
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resource": resource, "comments": comments, "likeflag": likeFlag, "hadcollect": hadCollect})
7878
}
7979

websites/code/studygolang/src/controller/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func SearchHandler(rw http.ResponseWriter, req *http.Request) {
2828

2929
pageHtml := ""
3030
respBody, err := service.DoSearch(q, field, (p-1)*rows, rows)
31-
if err == nil {
31+
if err == nil && respBody != nil {
3232
pageHtml = service.GenPageHtml(p, rows, respBody.NumFound, "/search?q="+q+"&f="+field)
3333
}
3434

websites/code/studygolang/src/controller/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
9696
service.Views.Incr(req, model.TYPE_TOPIC, util.MustInt(vars["tid"]))
9797

9898
// 设置内容模板
99-
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html")
99+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html,/template/common/comment.html")
100100
// 设置模板数据
101101
filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect})
102102
}

websites/code/studygolang/src/controller/user.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package controller
88

99
import (
10+
"fmt"
1011
"net/http"
12+
"strconv"
1113

1214
"filter"
1315
"github.com/studygolang/mux"
@@ -57,3 +59,43 @@ func UsersHandler(rw http.ResponseWriter, req *http.Request) {
5759
// 设置模板数据
5860
filter.SetData(req, map[string]interface{}{"activeUsers": "active", "actives": activeUsers, "news": newUsers, "total": total})
5961
}
62+
63+
// 邮件订阅/退订页面
64+
// URI: /user/email/unsubscribe{json:(|.json)}
65+
func EmailUnsubHandler(rw http.ResponseWriter, req *http.Request) {
66+
token := req.FormValue("u")
67+
if token == "" {
68+
util.Redirect(rw, req, "/")
69+
return
70+
}
71+
72+
// 校验 token 的合法性
73+
email := req.FormValue("email")
74+
user := service.FindUserByEmail(email)
75+
if user.Email == "" {
76+
util.Redirect(rw, req, "/")
77+
return
78+
}
79+
80+
realToken := service.GenUnsubscribeToken(user)
81+
if token != realToken {
82+
util.Redirect(rw, req, "/")
83+
return
84+
}
85+
86+
vars := mux.Vars(req)
87+
if req.Method != "POST" || vars["json"] == "" {
88+
filter.SetData(req, map[string]interface{}{
89+
"email": email,
90+
"token": token,
91+
"unsubscribe": user.Unsubscribe,
92+
})
93+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/email_unsub.html")
94+
return
95+
}
96+
97+
unsubscribe, _ := strconv.Atoi(req.PostFormValue("unsubscribe"))
98+
99+
service.EmailSubscribe(user.Uid, unsubscribe)
100+
fmt.Fprint(rw, `{"ok": 1, "msg":"保存成功"}`)
101+
}

websites/code/studygolang/src/controller/websocket.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"sync"
1212
"time"
1313

14-
"go.net/websocket"
14+
"golang.org/x/net/websocket"
1515
"logger"
1616
"service"
1717
"util"
@@ -36,7 +36,7 @@ func WsHandler(wsConn *websocket.Conn) {
3636
}
3737
userData := service.Book.AddUser(user, serverId)
3838
// 给自己发送消息,告诉当前在线用户数、历史最高在线人数
39-
onlineInfo := map[string]int{"online": service.Book.Len() + 50, "maxonline": service.MaxOnlineNum()}
39+
onlineInfo := map[string]int{"online": service.Book.Len(), "maxonline": service.MaxOnlineNum()}
4040
message := service.NewMessage(service.WsMsgOnline, onlineInfo)
4141
err = websocket.JSON.Send(wsConn, message)
4242
if err != nil {
@@ -62,7 +62,7 @@ func WsHandler(wsConn *websocket.Conn) {
6262
}
6363
// 用户退出时需要变更其他用户看到的在线用户数
6464
if !service.Book.UserIsOnline(user) {
65-
message := service.NewMessage(service.WsMsgOnline, map[string]int{"online": service.Book.Len() + 50})
65+
message := service.NewMessage(service.WsMsgOnline, map[string]int{"online": service.Book.Len()})
6666
go service.Book.BroadcastAllUsersMessage(message)
6767
}
6868
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 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 controller
8+
9+
import (
10+
"net/http"
11+
12+
"filter"
13+
)
14+
15+
// Wide 的内嵌 iframe 的 playground
16+
func PlaygroundHandler(rw http.ResponseWriter, req *http.Request) {
17+
// 设置内容模板
18+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/wide/playground.html")
19+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"logger"
2222
"service"
2323
"util"
24+
"util/version"
2425
)
2526

2627
// 自定义模板函数
@@ -192,8 +193,8 @@ func (this *ViewFilter) PostFilter(rw http.ResponseWriter, req *http.Request) bo
192193
// websocket主机
193194
data["wshost"] = config.Config["wshost"]
194195
data["build"] = map[string]string{
195-
"version": util.Version,
196-
"date": util.Date,
196+
"version": version.Version,
197+
"date": version.Date,
197198
}
198199

199200
err = tpl.Execute(rw, data)

0 commit comments

Comments
 (0)