Learn how to put some of the Go frameworks and libraries into action and implement an advanced microservice.
- [Leander Reimer] Hi and welcome back to Advanced Cloud Native Go. My name is Leander Reimer and I'm happy to be your host. Advance Go Microservice Implementation. So in this video, we're going to take a look at how to implement a basic HTTP microservice server with a configurable port using the Gin framework, we learned about in the last video. We're going to implement a basic routine logic for different paths and verbs. And we're going to implement JSON request and response processing for a simple REST API.
So this is quite some ground to cover. We got to get started. So let's open out IDE. And I've already prepared a small skeleton here. So I've got good, an empty main function. I have implemented a helper function to extract the port from an environment variable called port. So remember, this is good practice in cloud native application development to make things like the port configurable via environment variables.
Also here, in the import list, you see the import for the Gin framework. So let's start with the very first basic implementation. So here, I have the Gin engine default in one line. Next, I'm going to define a very basic get request with the URL ping and then pass it a handler function.
So this handler function takes only one parameter and it's the Gin context. So via this Gin context, you see this here, you can access the request. You can access the response, you can access, well, the request parameters for examples, and so on and so forth. And within this handler function, I say c.String like all the string method. I respond with StatusOK and with the message pong.
And in this last line here, I'm going to run the engine on the configured port. So let's see if that works already. So we're going to build this. You see here, we have the Gin-Web.exe. And we're going to call this. And you see our service is already running. And you see here nicely I have our ping request. And it's listening and serving on port 8080.
So let's see. And you see, it's received the ping request. So let's continue and do something more sophisticated. So we go back to our editor and next up, we're going to add a few more endpoints. So let's say I want to add the following endpoint.
Again, it will be a GET request for the URL hello. And again, I pass in the handler function with the only parameter gin.Context. Now only this time, I respond with the very basic JSON message. So I say, c.JSON, http.StatusOK and I construct here a Gin map.
This is a helper function from Gin to construct a map with a message and I say, Hello Gin Framework. So let's see about that. So we're going to kill this. We build a microservice again. And I have the next endpoint here. And you see here, it already responsd with the JSON message.
Excellent. Now of course, this is all very basic, right? So let's add a book API like we did in the previous logging. So let's see, so first of all, we define an endpoint to return a list of books. So again, engine.GET api books and I respond with http.StatusOK. And that return AllBooks.
I've already prepare this so we have a helper function called AllBooks. We have a helper function to get books, create books, update books, and delete books. So what we're going to do is to build the rest API for those functions. So here is the mapping to create a new book. So I say engine.POST, api/books. Again, I define a handler function.
And in here, what you see is, I now process the JSON body and process it with the context by JSON function. I call out a CreateBook function and if it's created, I read set the location header and then a respond with http.StatusCreated. In any other case, I say StatusConflict. So next up, I define the handler function for we have to obtain a book, an individual book by isbn.
So engine.GET for api/book, and you see here, the call on isbn, this defines a name parameter. So in here, you see how you can get hold of the name parameter via the Gin context. So I say c.Params.ByName isbn. And this will automatically extract this name parameter here from this url pattern. Again, I try to get the book by isbn.
If I found it, I return the book as JSON and if I didn't find it, I abort with StatusNotFound. So I continue with put and with delete. Always using the same pattern, Engine.PUT and engine.DELETE. So let's see. We're going to build this. And we're going to call it and you see, you've already have different POST for books, GET for books and three by isbn.
So if we open our REST client, you see here. I say apibooks and I do get two books returned. I can create a book and if I call get books again, you see here that I now have three books. I can issue get request, I can issue get request for unknown books. You see that this status 404 Not Found.
I can update books and also delete books again. So we're back to where we're started. And you see here in the console window, all the different interactions with our REST API. So that's awesome, right? So last but not least, one final thing. You can also use the Gin framework to serve HTML files. And this is kind of useful when you want to implement so called self-contained systems.
So if you want to serve the url of your microservice from within the same instance and this is how I do it. We can define html templates and again here, what you can do is you can respond with html status code OK for index.html. So let's see if that works. So we're going to kill that. We're going to build our microservice and we started again.
So I see some more output now. And if we open our browser now, so we see, Hello Gin Framework. So I hope you enjoy this. In the next video, we will talk about the containerization and composition of this microservice using Docker. I hope to see you then, bye bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Cloud native application platforms
- Go frameworks and libraries for microservices
- Using Docker for containerization
- Using Kubernetes for orchestration
- Using Consul for microservice discovery and configuration
- Registration and lookup
- Implementing service discovery using Kubernetes
- Microservice communication patterns: Sync and async
- Using circuit breakers for resilient communication
- Implementing message queuing with Rabbitmq
- Using Apache Kafka for publish/subscribe