Backend As A Service with Go, Gin, Mysql & Docker
Welcome to the very first chapter of the Series : Backend as a Service with Go, Gin, Docker & MySQL
.
Learning Objectives
This chapter focuses on setting up our project with necessary tools to get us started with our awesome learning journey!
Pre-requisites
Here is the few list of pre-requisites:
- Go Installed (version : go1.19.1)
- Docker & Docker Compose
- Preferably Linux/Mac machine
- General understanding of Restful Services/APIs
Getting Started
Let's start by creating a blog
folder in your preferred location. Then initialize a new Go module inside blog
folder with following command.
go mod init blog
The above command creates a go.mod
file. This file allows us to manage project dependencies. Let's add gin
as a dependency for our project.
go get github.com/gin-gonic/gin
The above command installs Gin Framework and Gorm. It also creates go.sum
file. This file stores info about the dependencies and their versions.
Getting Up Hello World Server
Create a main.go
file in the root level. Open up your favorite editor and write below code.
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default() //new gin router initialization
router.GET("/", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"data": "Hello World !"})
}) // first endpoint returns Hello World
router.Run(":8000") //running application, Default port is 8080
}
Let's go through the code.
- package main : Each go file is a package. And this main package is an entry point for the gin project.
- import : As the name implies this imports the necessary packages
- func main : main function is triggered when we run the application. Inside the function block, a new
Gin router
is initialized to therouter
variable. A new endpoint/
is set on the router.
Setting up a route/endpoint requires two things:- Endpoint : It is the path from where the data is fetched. For instance, if visitor visits
all posts
page, the posts will be fetched from/posts
endpoint. - Handler : It determines how you provide the data to the endpoint. Business logic like
getting/saving
datafrom/to
thedatabase
, inputvalidation
and so.on. The data can be sent to the server withJSON
method of Context Object. This method takes a HTTP status code and a JSON response as the parameters
- Endpoint : It is the path from where the data is fetched. For instance, if visitor visits
The following command starts the server
go run main.go
If everything is configured correctly you should see similar output as below.
[GIN-debug] GET / --> main.main.func1 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8000
Now, visit to localhost:8000 to see {"data":"Hello World !"}
as a response.
At this point the project structure should look like this :
├── go.mod
├── go.sum
└── main.go
Woohoo!
That's a wrap.
We go slow and steady!
All of the code for this chapter is available Here.
The next chapter will cover up the following topics:
- Docker setup with Docker Compose
- MySQL database within Docker Compose
- Environment variables
Make sure to sign up the newsletter so that you never miss my upcoming articles.
Every comment/feedback/suggestions adds a great value and motivates me to keep up the good work!
Thank You! Happy learning!