HTTP servers sample
This commit is contained in:
parent
552611bc1c
commit
973da6773f
@ -62,6 +62,7 @@ Command-Line Flags
|
||||
Command-Line Subcommands
|
||||
Environment Variables
|
||||
HTTP Clients
|
||||
HTTP Servers
|
||||
Spawning Processes
|
||||
Exec'ing Processes
|
||||
Signals
|
||||
|
50
examples/http-servers/http-servers.go
Normal file
50
examples/http-servers/http-servers.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Writing a basic HTTP server is very easy using the
|
||||
// `net/http` package.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// A fundamental concept in `net/http` servers is
|
||||
// *handlers*. A handler is an object implementing the
|
||||
// `http.Handler` interface. A very common way to write
|
||||
// a handler is by using the `http.HandlerFunc` adapter
|
||||
// on functions with the appropriate signature.
|
||||
func hello(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// Functions serving as handlers take a
|
||||
// `http.ResponseWriter` and a `http.Request` as
|
||||
// arguments. The response writer is used to fill in the
|
||||
// HTTP response. Here out simple response is just
|
||||
// "hello\n".
|
||||
fmt.Fprintf(w, "hello\n")
|
||||
}
|
||||
|
||||
func headers(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// This handler does something a little more
|
||||
// sophisticated by reading all the HTTP request
|
||||
// headers and echoing them into the response body.
|
||||
for name, headers := range req.Header {
|
||||
for _, h := range headers {
|
||||
fmt.Fprintf(w, "%v: %v\n", name, h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// We register our handlers on server routes using the
|
||||
// `http.HandleFunc` convenience function. It sets up
|
||||
// the *default router* in the `net/http` package and
|
||||
// takes a function as an argument.
|
||||
http.HandleFunc("/hello", hello)
|
||||
http.HandleFunc("/headers", headers)
|
||||
|
||||
// Finally, we call the `ListenAndServe` with the port
|
||||
// and a handler. `nil` tells it to use the default
|
||||
// router we've just set up.
|
||||
http.ListenAndServe(":8090", nil)
|
||||
}
|
2
examples/http-servers/http-servers.hash
Normal file
2
examples/http-servers/http-servers.hash
Normal file
@ -0,0 +1,2 @@
|
||||
1bffba1944537a9f9d7166a67d7a2b9f4ce2d70e
|
||||
hg47WDVcY_W
|
6
examples/http-servers/http-servers.sh
Normal file
6
examples/http-servers/http-servers.sh
Normal file
@ -0,0 +1,6 @@
|
||||
# Run the server in the background.
|
||||
$ go run examples/http-servers/http-servers.go &
|
||||
|
||||
# Access the `/hello` route.
|
||||
$ curl localhost:8090/hello
|
||||
hello
|
@ -146,7 +146,7 @@ settings.</p>
|
||||
|
||||
|
||||
<p class="next">
|
||||
Next example: <a href="spawning-processes">Spawning Processes</a>.
|
||||
Next example: <a href="http-servers">HTTP Servers</a>.
|
||||
</p>
|
||||
|
||||
<p class="footer">
|
||||
|
198
public/http-servers
Normal file
198
public/http-servers
Normal file
@ -0,0 +1,198 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Go by Example: HTTP Servers</title>
|
||||
<link rel=stylesheet href="site.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="example" id="http-servers">
|
||||
<h2><a href="./">Go by Example</a>: HTTP Servers</h2>
|
||||
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Writing a basic HTTP server is very easy using the
|
||||
<code>net/http</code> package.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/hg47WDVcY_W"><img title="Run code" class="run" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJ1SURBVCjPY/j//z8DMu7o6GAAgpQgD9tLqcmJH4KDg14aaik/MtdXe2ZjY6OCrh6Fs2jRYmZ9Pd05M9uL/u9dPfU/CLS0dfxvKIz/X5Dg/z8pKdkGqwGpqakMUdExDHJSYqt37tjxf+qUSf9rc2P+79298/+RA3v+H1zV///o6r7/DrbWFQkJiQwxMTGoBjAxMTpKiQmuqMuP/f/xw/v/J0+f/W9tbvTfxVLn/8rJVf+v757z/96hRf8TQtxuCQmLMjk4OKAawMfDVWVvrvd85eTq/7tXTP6/e/XM/22lif9LCnL+b13Q/v/Kzln/L++c/X/7/Jb/VpYWuZFRUagGAAErEBtlxvi+vn944f9L26cDNcz6v21R9/8zm6aC2SBDbu+f/78kK+4/L79AO7oBYCAqxD/57JZp/y/tmPX/wrYZ/6+CbAayD6zs/78daBjIgPayFJAGG6wGAIFAcpjH/dv7F4ANABuya/b/Od3l/ye2V/+/tnv2/7ldxSANmrgMYGBhZg7fuagD7GyYIeeBrrqwdRrQgLn/l02sBGkwwWkAEAjV5EZ/vQV0LswAGAYZsLC3DKTBAJ8BzCkRni/uHFyIYcAtoNc6ypL/ANVIohigrKwMxqqqqgxMzKzM6VHeL+6iGQAKzDtAV5XlJv3n5uFLRTHgzZs3YPzz50+GwqJiPitD9Y8Pjy4BB+CNvfP+3wUmIpAhhckhr3X19LodHZ28UQxQU1MDYw0NDQYBAQEeoBOTK7JjP2xf3Pt/bkfB/4KkoDcKMmIL5OXlFerq6hhu3rzJgC8MwMDYxGSfm5vbVn9/f0cgVxAkpqioyFBfX49iAACbTAK+xT3CzgAAAABJRU5ErkJggg==" /></a>
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>A fundamental concept in <code>net/http</code> servers is
|
||||
<em>handlers</em>. A handler is an object implementing the
|
||||
<code>http.Handler</code> interface. A very common way to write
|
||||
a handler is by using the <code>http.HandlerFunc</code> adapter
|
||||
on functions with the appropriate signature.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Functions serving as handlers take a
|
||||
<code>http.ResponseWriter</code> and a <code>http.Request</code> as
|
||||
arguments. The response writer is used to fill in the
|
||||
HTTP response. Here out simple response is just
|
||||
“hello\n”.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>This handler does something a little more
|
||||
sophisticated by reading all the HTTP request
|
||||
headers and echoing them into the response body.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <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="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>We register our handlers on server routes using the
|
||||
<code>http.HandleFunc</code> convenience function. It sets up
|
||||
the <em>default router</em> in the <code>net/http</code> package and
|
||||
takes a function as an argument.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">http</span><span class="p">.</span><span class="nx">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="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Finally, we call the <code>ListenAndServe</code> with the port
|
||||
and a handler. <code>nil</code> tells it to use the default
|
||||
router we’ve just set up.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">http</span><span class="p">.</span><span class="nx">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></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<table>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Run the server in the background.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre><span class="gp">$</span> go run examples/http-servers/http-servers.go <span class="p">&</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Access the <code>/hello</code> route.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre><span class="gp">$</span> curl localhost:8090/hello
|
||||
<span class="go">hello</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
|
||||
<p class="next">
|
||||
Next example: <a href="spawning-processes">Spawning Processes</a>.
|
||||
</p>
|
||||
|
||||
<p class="footer">
|
||||
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/http-servers">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -151,6 +151,8 @@
|
||||
|
||||
<li><a href="http-clients">HTTP Clients</a></li>
|
||||
|
||||
<li><a href="http-servers">HTTP Servers</a></li>
|
||||
|
||||
<li><a href="spawning-processes">Spawning Processes</a></li>
|
||||
|
||||
<li><a href="execing-processes">Exec'ing Processes</a></li>
|
||||
|
Loading…
x
Reference in New Issue
Block a user