Skip to content

Commit

Permalink
Make sure all code builds with travis
Browse files Browse the repository at this point in the history
Change-Id: I343c93cbb9165edd689e3f8bf72dca80d0adb0f9
  • Loading branch information
Francesc Campoy committed Feb 25, 2017
1 parent b587fd5 commit a32ca10
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 47 deletions.
2 changes: 1 addition & 1 deletion section01/README.md
Expand Up @@ -96,7 +96,7 @@ import (
"net/http"
)

func main() {
func doGet() {
req, err := http.NewRequest("GET", "https://golang.org", nil)
if err != nil {
log.Fatalf("could not create request: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion section01/examples/do-get.go
Expand Up @@ -19,7 +19,7 @@ import (
"net/http"
)

func main() {
func doGet() {
req, err := http.NewRequest("GET", "https://golang.org", nil)
if err != nil {
log.Fatalf("could not create request: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions section02/README.md
Expand Up @@ -20,7 +20,7 @@ method which makes it satisfy the `io.Writer` interface.

Let's see a very simple HTTP handler that simply writes `"Hello, web"` to the output:

[embedmd]:# (examples/step1.go /package main/ $)
[embedmd]:# (examples/step1/main.go /package main/ /^}/)
```go
package main

Expand Down Expand Up @@ -58,7 +58,7 @@ for any other paths in the `"/images/"` subtree.

Let's see how to register our `helloHandler` defined above:

[embedmd]:# (examples/step2.go /package main/ $)
[embedmd]:# (examples/step2/main.go /package main/ $)
```go
package main

Expand All @@ -81,7 +81,7 @@ Note that we're registering our handler as part of the `main` function.
Try to run the code above:

```bash
$ go run examples/step2.go
$ go run examples/step2/main.go
```

What happens? Well, we're missing the last piece of the puzzle: starting the
Expand Down Expand Up @@ -150,7 +150,7 @@ when no error has occurred the returned value equals to `nil`.
So if we want to check that our server started successfully and log an error
otherwise we would modify our code to add a call to `ListenAndServe`.

[embedmd]:# (examples/step3.go /package main/ $)
[embedmd]:# (examples/step3/main.go /package main/ $)
```go
package main

Expand Down Expand Up @@ -225,6 +225,7 @@ func addProduct(w http.ResponseWriter, r *http.Request) {

func getProduct(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["productID"]
log.Printf("fetching product with ID %q", id)
// get a specific product
}

Expand Down
1 change: 1 addition & 0 deletions section02/examples/gorilla.go
Expand Up @@ -30,6 +30,7 @@ func addProduct(w http.ResponseWriter, r *http.Request) {

func getProduct(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["productID"]
log.Printf("fetching product with ID %q", id)
// get a specific product
}

Expand Down
Expand Up @@ -21,3 +21,6 @@ import (
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, web")
}

// This doesn't appear on the markdown docs.
func main() {}
File renamed without changes.
File renamed without changes.
28 changes: 22 additions & 6 deletions section03/README.md
Expand Up @@ -22,9 +22,9 @@ The `http.Request` type has a method `FormValue` with the following docs:
That's easy! So if we want to obtain the value of a parameter `q` in the URL `/hello?msg=world`
we can write the next program.

[embedmd]:# (examples/hello_parameter.go /func handler/ /^}/)
[embedmd]:# (examples/handlers/main.go /func paramHandler/ /^}/)
```go
func handler(w http.ResponseWriter, r *http.Request) {
func paramHandler(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if name == "" {
name = "friend"
Expand Down Expand Up @@ -64,6 +64,22 @@ closed at the end of the execution of the http handler, so don't worry about it.
There's many ways we can read from an `io.Reader`, but for now you can use `io.ReadAll`,
which returns a `[]byte` and an `error` if something goes wrong.

[embedmd]:# (examples/handlers/main.go /func bodyHandler/ /^}/)
```go
func bodyHandler(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "could not read body: %v", err)
return
}
name := string(b)
if name == "" {
name = "friend"
}
fmt.Fprintf(w, "Hello, %s!", name)
}
```

### Exercise Hello, body

Modify the previous exercise so instead of reading the `name` argument from a query or form value it will
Expand Down Expand Up @@ -143,7 +159,7 @@ You can set headers in the response with the `Header` function in the `ResponseW
`Header` returns a [`http.Header`](https://golang.org/pkg/net/http/#Header) which has, among other methods,
the method `Set`. We can then set the content type in our `ResponseWriter` named `w` like this.

[embedmd]:# (examples/text_handler.go /w.Header.*/)
[embedmd]:# (examples/texthandler/main.go /w.Header.*/)
```go
w.Header().Set("Content-Type", "text/plain")
```
Expand All @@ -158,7 +174,7 @@ Some people call them decorators, most of them also write Python 😛.

To start we're going to define a new type named `textHandler` that contains a `http.HandlerFunc`.

[embedmd]:# (examples/text_handler.go /type textHandler/ /^}/)
[embedmd]:# (examples/texthandler/main.go /type textHandler/ /^}/)
```go
type textHandler struct {
h http.HandlerFunc
Expand All @@ -167,7 +183,7 @@ type textHandler struct {

Now we're going to define the `ServeHTTP` method on `textHandler` so it satisfies the `http.Handler` interface.

[embedmd]:# (examples/text_handler.go /.*ServeHTTP/ /^}/)
[embedmd]:# (examples/texthandler/main.go /.*ServeHTTP/ /^}/)
```go
func (t textHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Set the content type
Expand All @@ -179,7 +195,7 @@ func (t textHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

Finally we replace our `http.HandleFunc` calls with `http.Handle`.

[embedmd]:# (examples/text_handler.go /func main/ /^}/)
[embedmd]:# (examples/texthandler/main.go /func main/ /^}/)
```go
func main() {
http.Handle("/hello", textHandler{helloHandler})
Expand Down
Expand Up @@ -21,11 +21,12 @@ import (
)

func main() {
http.HandleFunc("/hello", handler)
http.HandleFunc("/body", bodyHandler)
http.HandleFunc("/param", paramHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
func bodyHandler(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "could not read body: %v", err)
Expand All @@ -37,3 +38,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
fmt.Fprintf(w, "Hello, %s!", name)
}

func paramHandler(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if name == "" {
name = "friend"
}
fmt.Fprintf(w, "Hello, %s!", name)
}
33 changes: 0 additions & 33 deletions section03/examples/hello_parameter.go

This file was deleted.

File renamed without changes.

0 comments on commit a32ca10

Please sign in to comment.