CRUD RESTful API with Go, GORM, JWT, MySQL and Testing
Go, also known as Golang, is a programming language developed by Google. It was created by Robert Griesemer, Rob Pike, and Ken Thompson and was first announced in 2009. Go was designed with the goal of providing a simple and efficient programming language that is easy to learn, write, and read.
In this blog post:
We will build a blog application where a user can:
- Signup (Register)
- Edit his account
- Shutdown (Delete his account)
- Create a blog post
- Edit blog post created by him
- View all blog posts
- View a particular blog post
- View other blog posts published by other users
- Delete blog post created by him
This API will be built with:
- Go
- GORM (A Golang ORM)
- JWT
- Mysql
- Gorilla Mux (For HTTP routing and URL matcher)
All methods and endpoints will tested in in the next post. Table tests will also be used to test every possible case for a particular functionality.
In Future Articles:
This article is the first part. There will be future articles that will explain how to:
- Creating The API
- Testing the API
- Dockerize the API
- Deploy on Kubernetes
In case you just want to see the code, check this atgithub
Step 1. Setup Go Project and create Database
CRUD RESTful API with Go, GORM, JWT, MySQL and Testing
Go, also known as Golang, is a programming language developed by Google. It was created by Robert Griesemer, Rob Pike, and Ken Thompson and was first announced in 2009. Go was designed with the goal of providing a simple and efficient programming language that is easy to learn, write, and read.
In this blog post:
We will build a blog application where a user can:
- Signup (Register)
- Edit his account
- Shutdown (Delete his account)
- Create a blog post
- Edit blog post created by him
- View all blog posts
- View a particular blog post
- View other blog posts published by other users
- Delete blog post created by him
This API will be built with:
- Go
- GORM (A Golang ORM)
- JWT
- Mysql
- Gorilla Mux (For HTTP routing and URL matcher)
All methods and endpoints will tested in in the next post. Table tests will also be used to test every possible case for a particular functionality.
In Future Articles:
This article is the first part. There will be future articles that will explain how to:
- Creating The API
- Testing the API
- Dockerize the API
- Deploy on Kubernetes
In case you just want to see the code, check this atgithub
Step 1. Setup Go Project and create Database
Create the directory the project will live in. This can be created anywhere on your computer. Let’s call it skillpedia
mkdir skillpedia && cd skillpedia
Initiate go modules which makes dependency version information explicit and easier to manage
go mod init github.com/{username}/{projectdir}
Where {username} is your github username and {projectdir} is the directory created above. For my case:
go mod init github.com/Sangwan70/skillpedia
We will have to use third party packages in this application. If you are setting this for the first time, run the following commands:
go get github.com/badoux/checkmail
go get github.com/jinzhu/gorm
go get golang.org/x/crypto/bcrypt
go get github.com/dgrijalva/jwt-go
go get github.com/gorilla/mux
go get github.com/jinzhu/gorm/dialects/mysql
go get github.com/joho/godotenv
go get gopkg.in/go-playground/assert.v1
go get golang.org/x/text
This command add all modules to go.mod in the project directory. Your go.mod will look like:
Next, create two directories, api and tests, inside the skillpedia directory
mkdir api tests
Create the .env file to setup environmental variables required by our project:
# vi .env
# DB_HOST=skillpedia-mysql # will be required for docker container
DB_HOST=127.0.0.1
DB_DRIVER=mysql
API_SECRET=8tY6ew32U # Any Random string Required for JWT.
DB_USER=sangwan
DB_PASSWORD=skpDBPass#12
DB_NAME=skillpedia_api
DB_PORT=3306
# MySQL Test
# TEST_DB_HOST=mysql_test # will be required for docker container
TEST_DB_HOST=127.0.0.1
TEST_DB_DRIVER=mysql
TEST_API_SECRET=8tY6ew32U
TEST_DB_USER=sangwan
TEST_DB_PASSWORD=skpDBPass#12
TEST_DB_NAME=skillpedia_api_test
TEST_DB_PORT=3306
Note the MySQL database connection details. You will have to create a MySQL Database with same name, user id and password as given.
You can user this script to install mysql on RHEL 8/Rocky Linux 8. Simply download and run the script as root user.
After logging in as root user to mysql, use the following commands to create required users and databases: