Skip to content

Commit

Permalink
set a homework assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
maleck13 committed Sep 10, 2016
1 parent d259ab1 commit 8a909fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## This is the example web server from lesson two in Golang intro.

[Golang intro](https://github.com/fheng/golang-intro/blob/master/chapter-two.md)

## Homework

- Add a new route to the api
```
/api/time
```
That returns a json structure that looks like :

```json

{"time":1473438486}

```
- Add a new test that calls the new api


## Hints

- Start with the router and look at the echo handler
- you will need a new type that matches has a time property
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

//import our dependencies

import (
"encoding/json"
"log"
Expand All @@ -8,12 +10,13 @@ import (
)

//Message wraps a message and stamps it
//Define a custom type to hold our data.
type Message struct {
Message string `json:"message"` //tells the decoder what to decode into
Message string `json:"message"` //tells the decoder what to decode into this is needed if the property is lowercase and the property is uppercase
Stamp int64 `json:"stamp,omitempty"`
}

//BuisnessLogic does awesome BuisnessLogic
//BuisnessLogic does awesome BuisnessLogic taking a string and returning a pointer to a Message
func BuisnessLogic(text string) *Message {
mess := &Message{}
mess.Message = text
Expand All @@ -22,18 +25,24 @@ func BuisnessLogic(text string) *Message {
}

//Echo echoes what you send
//http.ResponseWriter is responsible for writing things back to the response stream
//http.Request represents the incoming request
func Echo(res http.ResponseWriter, req *http.Request) {
var (
jsonDecoder = json.NewDecoder(req.Body) //decoder reading from the post body
jsonEncoder = json.NewEncoder(res) //encoder writing to the response stream
message = &Message{} // something to hold our data
message = &Message{} // something to hold our data
)
//Add a content type header
res.Header().Add("Content-type", "application/json")
if err := jsonDecoder.Decode(message); err != nil { //decode our data into our struct
//decode our data into our struct. Notice the assignment and the err check can be done is a single line as long as you use the ;
if err := jsonDecoder.Decode(message); err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
//call our BusinessLogic function and assign the return value
pointless := BuisnessLogic(message.Message)
//Encode the Message contained in pointless and write it back to the response.
if err := jsonEncoder.Encode(pointless); err != nil { //encode our data and write it back to the response stream
res.WriteHeader(http.StatusInternalServerError)
return
Expand All @@ -42,6 +51,7 @@ func Echo(res http.ResponseWriter, req *http.Request) {

//Setup our simple router
func router() http.Handler {
//http.HandleFunc expects a func that takes a http.ResponseWriter and http.Request
http.HandleFunc("/api/echo", Echo)
return http.DefaultServeMux //this is a stdlib http.Handler
}
Expand Down
8 changes: 5 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@ import (
)

func TestEcho(t *testing.T) {
//create a test server based from our router
server := httptest.NewServer(router())
defer server.Close() //notice we use defer here to ensure our server is closed
//Make a new request using the test server url
res, err := http.NewRequest("POST", server.URL+"/api/echo", strings.NewReader(`{"message":"test"}`))
if err != nil {
log.Fatal(err)
}
//readAll of the body back as as bytes
echo, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
message := &Message{}
message := &Message{} //allocate a message to store our data in
//Unmarshal takes a slice / array of bytes and converts it into the passed type
if err := json.Unmarshal(echo, message); err != nil {
log.Fatal(err)
}
log.Println(message)
if "test2" != message.Message {
t.Fail()
log.Println("expected the message to equal test")
}
}

0 comments on commit 8a909fb

Please sign in to comment.