forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-servers.html
214 lines (164 loc) · 8.83 KB
/
http-servers.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: HTTP 服务端</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'http-clients';
}
if (e.key == "ArrowRight") {
window.location.href = 'context';
}
}
</script>
<body>
<div class="example" id="http-servers">
<h2><a href="./">Go by Example 中文版</a>: HTTP 服务端</h2>
<table>
<tr>
<td class="docs">
<p>使用 <code>net/http</code> 包,我们可以轻松实现一个简单的 HTTP 服务器。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/LUFfvmYCzXD"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"net/http"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><em>handlers</em> 是 <code>net/http</code> 服务器里面的一个基本概念。
handler 对象实现了 <code>http.Handler</code> 接口。
编写 handler 的常见方法是,在具有适当签名的函数上使用 <code>http.HandlerFunc</code> 适配器。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">hello</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>handler 函数有两个参数,<code>http.ResponseWriter</code> 和 <code>http.Request</code>。
response writer 被用于写入 HTTP 响应数据,这里我们简单的返回 “hello\n”。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Fprintf</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="s">"hello\n"</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">headers</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这个 handler 稍微复杂一点,
我们需要读取的 HTTP 请求 header 中的所有内容,并将他们输出至 response body。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">headers</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">req</span><span class="p">.</span><span class="nx">Header</span> <span class="p">{</span>
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">h</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">headers</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Fprintf</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="s">"%v: %v\n"</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">h</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>使用 <code>http.HandleFunc</code> 函数,可以方便的将我们的 handler 注册到服务器路由。
它是 <code>net/http</code> 包中的默认路由,接受一个函数作为参数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">http</span><span class="p">.</span><span class="nf">HandleFunc</span><span class="p">(</span><span class="s">"/hello"</span><span class="p">,</span> <span class="nx">hello</span><span class="p">)</span>
<span class="nx">http</span><span class="p">.</span><span class="nf">HandleFunc</span><span class="p">(</span><span class="s">"/headers"</span><span class="p">,</span> <span class="nx">headers</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>最后,我们调用 <code>ListenAndServe</code> 并带上端口和 handler。
nil 表示使用我们刚刚设置的默认路由器。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">http</span><span class="p">.</span><span class="nf">ListenAndServe</span><span class="p">(</span><span class="s">":8090"</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>后台运行服务器。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go run http-servers.go &</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>访问 <code>/hello</code> 路由。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> curl localhost:8090/hello
<span class="go">hello</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="context.html">Context</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' fmt.Fprintf(w, \"hello\\n\")\u000A}\u000A');codeLines.push('func headers(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' for name, headers :\u003D range req.Header {\u000A for _, h :\u003D range headers {\u000A fmt.Fprintf(w, \"%v: %v\\n\", name, h)\u000A }\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.HandleFunc(\"/headers\", headers)\u000A');codeLines.push(' http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>