Go Microservice Part V - Controller
By ski11up.com
In the previous blog post, Go MVC Part V - Service we have developed a Contact Service which interacts with the Domain object and return the Model to the calling function.
Let’s start with developing Controller which will consume the response from the Contact Service and present it to the presentation layer(View in MVC).
Controller
Model contains domain objects as well the information retrieval mechanical from the database. Service has the Business Logic. Controller gets the request from the user and sends it to the Service Layer which in turn gets information from the Domain Layer. Controller is the main peace that takes the request from the user and sends back the response to the View.
Below is the Controller code which takes the user input, calls the service, and present it to the View, let’s look at it more closely.
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
package controllers
import (
"encoding/json"
"fmt"
"go-lang/go-serv/services"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func GetContact(resp http.ResponseWriter, req *http.Request) { (1)
id := mux.Vars(req)["id"] (2)
contactId, err := strconv.ParseInt(id, 10, 64) (3)
if err != nil { (4)
resp.WriteHeader(http.StatusBadRequest) (5)
resp.Write([]byte("id must be number...\n")) (6)
return
}
contact, err := services.GetContact(contactId) (7)
if err != nil { (8)
resp.WriteHeader(http.StatusNotFound) (9)
resp.Write([]byte(err.Error())) (10)
return
}
jsonVal, _ := json.Marshal(contact) (11)
resp.Write(jsonVal) (12)
}
| 1 | GetContact controller function has two arguments, takes the request in the pointer to http.Request and stores the response on http.ResponseWriter |
| 2 | mux is a Go lib mainly used for request routing, here we are reading the request variable(s) and storing into a local variable for processing, remember we want the contact details based on the given input id |
| 3 | checking if the user input is a number or a character, base of 64 |
| 4 | if we get any error which says it’s not nil |
| 5 | writing the response headers as BadRequest as the user has provided invalid input |
| 6 | writing to the response body with the message |
| 7 | if we get the right type of input then call the service |
| 8 | got an error here |
| 9 | in case of any error or the contact not found simply write that to the response header as StatusNotFound |
| 10 | adding to the response object with the error message |
| 11 | finally all is fine and we need to unmarshal the contact details into a JSON |
| 12 | writing the response which we got in the above line as a JSON body, this is View |
for processing requests, the Controller takes the arguments as http.ResponseWriter and *http.Request, where http.ResponseWriter takes care of the response body and request is a pointer to the http.Request
| it is always good to have the validation of user input, for example, the app is accepting the integer and let’s user sends the character then what? |
Notes
We have read the user response using Go gorilla/mux and checked if there are any errors and if the error is not nil then we have returned with writing the error to the response body. Finally, We have marshaled the response and converted it into the JSON body before being presenting it to the presentation layer. We have not written the View but it is just the response body presentation.
Next
Go to Go MVC Part VI - Application to get started with the App code.