Every microservice needs an HTTP server to serve its API. The aim of this video is to show how this can be achieved with plain Go and standard packages only.
- Hi, and welcome back to Could Native Go. My name is Leander Reimer, and I'm very happy to be your host in this section, on Simple Go Microservices. So this is what we're going to take a look at in this section. First, we'll implement a simple HTTP server in Go. Next on, we'll be having a look at how to Marshal Go Structs to and from Json. And finally, we will implement a very simple REST API in Go.
So let's get started then. Simple HTTP Server in Go. So in this video, we are going to take a look at how we can use the Go language and the net/http package to implement and start a very simple HTTP server. And then, we're going to define simple handler functions that will respond to specific GET requests on our HTTP server. So, let's switch to code then.
I have my IDE here and what we do now is, first of all, we will be using the "net/http" package and we need to import it first. So that's done, "net/http". And to start our HTTP server we only need one line of code. So we will call the "ListenAndServe" method on the HTTP, and we will pass the first parameter, which basically specifies the port and the address to listen on.
So this is port "8080". So, of course, this server doesn't do a lot at the moment so of course we want it to respond to GET request. And this is what we're going to add next. So what we've done now is we added the handler function to respond to the index path and we specified method referenciator, which is called "index". So next we need to implement this function, which is called "index".
And this handle function is going to take two parameters. The first parameter is of type "ResponseWriter", and this is basically the construct we use to respond content to our client. The second parameter is a pointer to the "http.Request" we received. So let's do something useful here: so we will respond with http status code "OK", which is basically http 200, and then we will write something back to the "ResponseWriter".
And this is basically the body of the http response we were writing. So let's save that, go to our console, and compile everything. So you see here, we compiled a neat, small executable, called "Cloud-Native-Go", and let's call this one. So this one is running now, and let's switch back to our REST client, and that's issued a GET request on localhost port "8080".
And hooray! Here we go, load Cloud-Native-Go, that's the expected response. Now, it's good practice for Cloud Native applications to have some environment variables that specify certain behaviors. So what we want to do now is to extract our port parameter and make it configurable. So what we do, we specify a port function, and we will use this port function here instead of the statically defined port "8080".
Now the question is, how are we going to implement this port function? So let's have a look at this code. First of all, we will get the environment variable called "PORT". If we haven't specified it, we will fall back to port "8080" as default and then we are going to return this as a string. So let's save, go back to our console, compile everything again. So next thing, we will define the environment variable "PORT" and maybe use "9090" as the new port value.
The next stop, we will call our small HTP server again. So let's see how this changed the behavior of our HTP server. So we go here, and now we will call the HTP server and localhost port "9090". Again, we'll issue the GET request and hooray, we have here "Hello Cloud-Native-Go" as the response. So let's go back to our code once again and implement something even more useful.
Finally, I want to show you how we can implement a very basic "echo" functionality. So once again, we define a "HandleFunc", which responds to API echo and we call a function, which is called "echo". So this is what we do now. Now the good question is, how we're going to implement this one. Well, first of all, what we need to do is extract the message Query parameter from our URL.
And this is what this line will do, okay? We will use the request, the URL, we have to Query, and then we extract the first message parameter. And now we need to do then, we add the header for the content type, type "text/plain" and we will echo the message we've received. So once again, we save this, go back to our console, we reset the port to "8080", and we call our HTTP server again.
So we go back to our REST client, and I've prepared this GET request here for our "echo" API, so we say, "localhost:8080/api/echo?message-Cloud-Native-Go" So let's send this one, and hooray! This is our "echo" response, and if we take a look at the headers, you see here that the content types are "text/plain" and the status code is "200". So on the next video, we will talk about how we can Marshal Go Structs to and from Json.
I hope you enjoyed this video and I see you next time, bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Implementing Go HTTP Server
- JSON marshalling and unmarshalling of Go structs
- Implementing a simple REST API
- Using Docker workflows and commands
- Building a naïve Docker image
- Running a containerized Go microservice
- Kubernetes architecture and concepts
- Deploying a Go microservice to Kubernetes
- Implementing service descriptors
- Performing rolling updates