Skip to content

Commit 1e5131b

Browse files
committed
Fix failing build for a Chinese version because of template tags
1 parent 1b85586 commit 1e5131b

File tree

8 files changed

+93
-77
lines changed

8 files changed

+93
-77
lines changed

zh/09.1.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% raw %}
12
# 9.1 预防CSRF攻击
23

34
## 什么是CSRF
@@ -91,3 +92,4 @@ CSRF的防御可以从服务端和客户端两方面着手,防御效果是从
9192
* [目录](<preface.md>)
9293
* 上一节: [安全与加密](<09.0.md>)
9394
* 下一节: [确保输入过滤](<09.2.md>)
95+
{% endraw %}

zh/10.2.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% raw %}
12
# 10.2 本地化资源
23
前面小节我们介绍了如何设置Locale,设置好Locale之后我们需要解决的问题就是如何存储相应的Locale对应的信息呢?这里面的信息包括:文本信息、时间和日期、货币值、图片、包含文件以及视图等资源。那么接下来我们将对这些信息一一进行介绍,Go语言中我们把这些格式信息存储在JSON中,然后通过合适的方式展现出来。(接下来以中文和英文两种语言对比举例,存储格式文件en.json和zh-CN.json)
34
## 本地化文本消息
@@ -132,3 +133,4 @@ $GOROOT/lib/time包中的timeinfo.zip含有locale对应的时区的定义,为
132133
* [目录](<preface.md>)
133134
* 上一节: [设置默认地区](<10.1.md>)
134135
* 下一节: [国际化站点](<10.3.md>)
136+
{% endraw %}

zh/10.3.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% raw %}
12
# 10.3 国际化站点
23
前面小节介绍了如何处理本地化资源,即Locale一个相应的配置文件,那么如果处理多个的本地化资源呢?而对于一些我们经常用到的例如:简单的文本翻译、时间日期、数字等如果处理呢?本小节将一一解决这些问题。
34
## 管理多个本地包
@@ -178,3 +179,4 @@
178179
* [目录](<preface.md>)
179180
* 上一节: [本地化资源](<10.2.md>)
180181
* 下一节: [小结](<10.4.md>)
182+
{% endraw %}

zh/12.2.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
{% raw %}
12
# 12.2 网站错误处理
23
我们的Web应用一旦上线之后,那么各种错误出现的概率都有,Web应用日常运行中可能出现多种错误,具体如下所示:
34

45
- 数据库错误:指与访问数据库服务器或数据相关的错误。例如,以下可能出现的一些数据库错误。
5-
6+
67
- 连接错误:这一类错误可能是数据库服务器网络断开、用户名密码不正确、或者数据库不存在。
78
- 查询错误:使用的SQL非法导致错误,这样子SQL错误如果程序经过严格的测试应该可以避免。
89
- 数据错误:数据库中的约束冲突,例如一个唯一字段中插入一条重复主键的值就会报错,但是如果你的应用程序在上线之前经过了严格的测试也是可以避免这类问题。
@@ -27,9 +28,9 @@
2728
错误处理其实我们已经在十一章第一小节里面有过介绍如何设计错误处理,这里我们再从一个例子详细的讲解一下,如何来处理不同的错误:
2829

2930
- 通知用户出现错误:
30-
31+
3132
通知用户在访问页面的时候我们可以有两种错误:404.html和error.html,下面分别显示了错误页面的源码:
32-
33+
3334
<html lang="en">
3435
<head>
3536
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
@@ -51,7 +52,7 @@
5152
</body>
5253
</html>
5354
另一个源码:
54-
55+
5556
<html lang="en">
5657
<head>
5758
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
@@ -72,9 +73,9 @@
7273
</div>
7374
</body>
7475
</html>
75-
76+
7677
404的错误处理逻辑,如果是系统的错误也是类似的操作,同时我们看到在:
77-
78+
7879
func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7980
if r.URL.Path == "/" {
8081
sayhelloName(w, r)
@@ -90,7 +91,7 @@
9091
ErrorInfo := "文件找不到" //获取当前用户信息
9192
t.Execute(w, ErrorInfo) //执行模板的merger操作
9293
}
93-
94+
9495
func SystemError(w http.ResponseWriter, r *http.Request) {
9596
log.Critical("系统错误") //系统错误触发了Critical,那么不仅会记录日志还会发送邮件
9697
t, _ = t.ParseFiles("tmpl/error.html", nil) //解析模板文件
@@ -109,7 +110,7 @@
109110
username = ""
110111
}
111112
}()
112-
113+
113114
username = User[uid]
114115
return
115116
}
@@ -122,3 +123,4 @@
122123
* [目录](<preface.md>)
123124
* 上一章: [应用日志](<12.1.md>)
124125
* 下一节: [应用部署](<12.3.md>)
126+
{% endraw %}

zh/13.3.md

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% raw %}
12
# 13.3 controller设计
23

34
传统的MVC框架大多数是基于Action设计的后缀式映射,然而,现在Web流行REST风格的架构。尽管使用Filter或者rewrite能够通过URL重写实现REST风格的URL,但是为什么不直接设计一个全新的REST风格的 MVC框架呢?本小节就是基于这种思路来讲述如何从头设计一个基于REST风格的MVC框架中的controller,最大限度地简化Web应用的开发,甚至编写一行代码就可以实现“Hello, world”。
@@ -17,7 +18,7 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
1718
Layout []string
1819
TplExt string
1920
}
20-
21+
2122
type ControllerInterface interface {
2223
Init(ct *Context, cn string) //初始化上下文和子类名称
2324
Prepare() //开始执行之前的一些处理
@@ -31,7 +32,7 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
3132
Finish() //执行完成之后的处理
3233
Render() error //执行完method对应的方法之后渲染页面
3334
}
34-
35+
3536
那么前面介绍的路由add函数的时候是定义了ControllerInterface类型,因此,只要我们实现这个接口就可以,所以我们的基类Controller实现如下的方法:
3637

3738
func (c *Controller) Init(ct *Context, cn string) {
@@ -42,43 +43,43 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
4243
c.Ct = ct
4344
c.TplExt = "tpl"
4445
}
45-
46+
4647
func (c *Controller) Prepare() {
47-
48+
4849
}
49-
50+
5051
func (c *Controller) Finish() {
51-
52+
5253
}
53-
54+
5455
func (c *Controller) Get() {
5556
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
5657
}
57-
58+
5859
func (c *Controller) Post() {
5960
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
6061
}
61-
62+
6263
func (c *Controller) Delete() {
6364
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
6465
}
65-
66+
6667
func (c *Controller) Put() {
6768
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
6869
}
69-
70+
7071
func (c *Controller) Head() {
7172
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
7273
}
73-
74+
7475
func (c *Controller) Patch() {
7576
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
7677
}
77-
78+
7879
func (c *Controller) Options() {
7980
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
8081
}
81-
82+
8283
func (c *Controller) Render() error {
8384
if len(c.Layout) > 0 {
8485
var filenames []string
@@ -108,10 +109,10 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
108109
}
109110
return nil
110111
}
111-
112+
112113
func (c *Controller) Redirect(url string, code int) {
113114
c.Ct.Redirect(code, url)
114-
}
115+
}
115116

116117
上面的controller基类已经实现了接口定义的函数,通过路由根据url执行相应的controller的原则,会依次执行如下:
117118

@@ -125,21 +126,21 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
125126
上面beego框架中完成了controller基类的设计,那么我们在我们的应用中可以这样来设计我们的方法:
126127

127128
package controllers
128-
129+
129130
import (
130131
"github.com/astaxie/beego"
131132
)
132-
133+
133134
type MainController struct {
134135
beego.Controller
135136
}
136-
137+
137138
func (this *MainController) Get() {
138139
this.Data["Username"] = "astaxie"
139140
this.Data["Email"] = "[email protected]"
140141
this.TplNames = "index.tpl"
141142
}
142-
143+
143144
上面的方式我们实现了子类MainController,实现了Get方法,那么如果用户通过其他的方式(POST/HEAD等)来访问该资源都将返回403,而如果是Get来访问,因为我们设置了AutoRender=true,那么在执行完Get方法之后会自动执行Render函数,就会显示如下界面:
144145

145146
![](images/13.4.beego.png?raw=true)
@@ -161,3 +162,4 @@ index.tpl的代码如下所示,我们可以看到数据的设置和显示都
161162
* [目录](<preface.md>)
162163
* 上一章: [自定义路由器设计](<13.2.md>)
163164
* 下一节: [日志和配置设计](<13.4.md>)
165+
{% endraw %}

0 commit comments

Comments
 (0)