Go Microservice Part VI - Application
By ski11up.com
In the previous blog post, Go MVC Part V - Controller we have developed a Contact Controller
which has the call to the service and stores the response in the response object for the presentation layer.
Let’s start with the application which stitches everything in previous posts.
Application
We started with the design of the application and then wrote the configuration and then the Model
, Service
and finally the Controller
and the View
now we need to put everything together in a form of an application. Let’s start with writing code for an Application
which when executed, starts the HTTP server and a microservice is hosted on to it which takes the contact id as input and presents the contact details to the presentation layer (usually HTML).
Below is the Application
code, 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
31
32
33
34
35
36
37
38
39
40
41
42
package app
import (
"fmt"
"go-lang/go-serv/config"
"go-lang/go-serv/controllers"
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
func StartApp() {
viper.SetConfigName("config") (1)
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetConfigType("yml")
r := mux.NewRouter() (2)
var configuration config.Configurations (3)
if err := viper.ReadInConfig(); err != nil { (4)
fmt.Printf("Error while reading config.yml %s", err)
}
err := viper.Unmarshal(&configuration) (5)
if err != nil {
fmt.Printf("Error on unmarshal %v", err)
}
r.HandleFunc("/contact/{id:[0-9]+}", controllers.GetContact).Methods("GET") (6)
http.Handle("/", r) (7)
fmt.Println("app started...")
err = http.ListenAndServe(configuration.Server.Port, nil) (8)
if err != nil {
panic(err)
}
}
1 | viper is a configuration lib of Go , we have used this lib for configuring application port |
2 | mux is another Go lib, we have used this to write routing logic of the application |
3 | initializing configuration variable |
4 | reading the configuration from config.yml, for any error spit it out to std output |
5 | unmarshaling is the same as deserialization (let’s assume this for a while) |
6 | this is where we are calling the GET /contact/:id |
7 | telling HTTP to use mux for routing |
8 | starting the server on the given port, on failure panic |
Source Code
The complete source of is located at go-mvc feel free to either git fork
, git clone
or download this repository.
Next
We have learned to write a simple microservice using minimal GoLang
code. We started with the design of the application using MVC
, then configuration and then other peace and finally we stitch together all the peace and wrote the application code, from here we can move forward and write authentication on this application and the actual view in HTML.
Experiment
We have written a simple microservice using Go
what we can do is write the same application using Java
/ Scala
or any other preferred language of yours and find the performance by benchmarking. Go
is built on an assumption that the world is not object-oriented but it is concurrent, it has the support of routine
and channel
which can be used for better CPU time utilization and hence better performance.