This application stores new users and their phone numbers.
In usercontroller.go, I have defined four handlers :
1- GetAllUsers: retrieve the records of all the users
2- DeletUserByID: delete specific user by ID
3- CreateUser: create a new user
4- UpdateUserByID: update a users’s record information
I use the gorilla/mux package to implement a request router and dispatcher for matching incoming requests to their respective handler.
For connecting this app to the database, there are two files:
1- config.go:
I defined the config structure in this file, and whit the GetConnectionString function, the connectionString was created.
2- connector.go:
For connecting to database i use gorm which is an ORM library for Golang.
GORM provides CRUD operations and can also be used for the initial migration and creation of the database schema.
in this file i define two functions :
- Connect: its open connection to the mysql service.
- Migration: this function use gorm AutoMigrate function to automaticly create table.
I leran about ORM technique from blow:
Introduction to Object-Relational Mapping
In this folder is a user.go file which contains and User object for REST(CRUD).
In main file three functions are defined:
- initDb: it loads .env file to get database config and after that called all functions which are used to connect ro database and create table
- initaliseHandlers: in this function we have four routes which are maped to their related handler.
- main: called initDb and initaliseHandlers functions and create router.
This link helps to learn CI/CD basics.
I just write some bash scripts, define all the automated tasks in a .gitlab-ci.yml file in the root of the repo, and with a few more small configuration steps.Three stages are defined in the CI/CD :
- test: it is an simple automated testing of the Golang code.
- build: just build the docker image and push it to dockerhub.
- deploy: releasing an app to production.