Build required docker images and start the container.
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d --build
This will automatically start the go-app on port 8080 if in dev mode.
If in prod mod it will start the server on port 80 TCP.
This docker-compose file sets up a basic web application with three services:
Container
Role
go-app
Handles the main login for running the app.
nginx
Acts as a reverse proxy to the go-app service, so incoming traffic to the application can be efficiently directed to the appropriate service. Can also act as a load-balancer to distribute requests across multiple instances of the go-app service.
mysql
Runs MySQL database and creates a persistent volume for data storage. This way your data persists even after tearing down the containers.
This setup provides a scalable and easily deployable web application architecture that can be customized further according to specific requirements.
Environment Variables
To run this application, you will need to add the following environment variables to your .env file.
For development mode the .env file is present in backend directory.
SECRET_KEY
NGINX_PORT
DB_USERNAME
DB_PASSWORD
DB_HOST
DB_PORT
DB_NAME
ADMIN_ORG_NAME
ADMIN_ORG_HEAD
ADMIN_USERNAME
ADMIN_PASSWORD
Important change to be made before running
DB_HOST in .env file should be localhost if running the app using go command. DB_HOST in .env file should be mysql or whatever is the name of your MySQL service so that docker's internal dns will replace with the appropriate IP Address.
Postman API Reference
Postman API access - all the endpoints are here for your direct testing.
Admin endpoints - protected with isAdminCheck middleware
Create Organisation
POST /api/org
Body
Type
Description
name
string
Required Name of the organisation
head
string
Required Name of the head of organisation
Delete Organisation
DELETE /api/org
Body
Type
Description
name
string
Required Name of the organisation
Update Organisation details
PATCH /api/org
Body
Type
Description
name
string
Required Name of the organisation
head
string
Required Name of the new head of organisation
Create User Account
POST /api/auth
Body
Type
Description
username
string
Required Name of the user
password
string
Required Password that will be hashed while storing in the DB
org_id
string
Required Organisation the user belongs to
Delete User Account
DELETE /api/auth
Body
Type
Description
username
string
Required Name of the user
User endpoints - public
Login to account - will create a new JWT token with expiry time of 1 hour
POST /api/auth/login
Body
Type
Description
username
string
Required Name of the user
password
string
Required Password of the user
Refresh access token - grants a new acces token with an expiry time of 24 hours
POST /api/auth/refresh
Cookie
Type
Description
jwt
access token
Required Users should be logged in and have a valid access token to refresh the current token
Get user account
GET /api/auth/user
Cookie
Type
Description
jwt
access token
Required Users should be logged in and have a valid access token to get their account details
Logout
GET /api/auth/logout
Cookie
Type
Description
jwt
access token
Required Users should be logged in and have a valid access token to logout
Get people in same organisation
GET /api/org
Cookie
Type
Description
jwt
access token
Required Users should be logged in and have a valid access token to get list of users in their organisation
Running Unit Tests
To run tests, run the following commands.
Flush the whole DB before running the tests so that mock data doesn't intersect with the previous data.
Make sure you are in the root directory of the project where the docker-compose files are present.
Start the MySQL server service only if you are using Docker.
docker-compose -f .\docker-compose.yml -f .\docker-compose.dev.yml up -d mysql
Now run unit tests.
Make sure you are in the backend directory where the go.mod file is located.
cd backend
go test ./test -v
Rationale
Golang Framework: GoFiber is a lightweight and fast web framework that is perfect for building APIs. It has a simple and intuitive API that makes it easy to build and maintain APIs. It also has excellent performance and scalability, making it ideal for high-traffic applications.
MySQL Database: MySQL is a popular open-source relational database management system. It is known for its robustness, scalability, and performance. It also has excellent support for ACID transactions, making it ideal for applications that require data consistency and reliability.
Gorm: GORM is a powerful and easy-to-use ORM for Golang. It provides a simple and intuitive API for interacting with databases, making it easy to build and maintain database-driven applications. It also has excellent support for MySQL, making it an ideal choice for this project.
DB Design
The database will have two tables called "users" and "organisations" that will store the user and organisation details respectively. The tables will have the following columns:
Users Table
Column Name
Data Type
Description
id
int
Unique ID for each user
username
string
Name of the user
password
string
Password of the user
is_admin
bool
Is the user an admin
org_id
int
ID of the organisation
Organisations Table
Column Name
Data Type
Description
id
int
Unique ID for each org
name
string
Name of the organisation
head
string
Name of the head of the organisation
The "id" column will be the primary key for both tables. The "org_id" column in the "users" table will be a foreign key that references the "id" column in the "organisations" table.
The password will be hashed using bcrypt before being stored in the database to ensure security.
Authorization + Authentication service in Golang using JWT
Where is the source code ?
Features
Architecture / Stack
Installation
This app requires Go v1.20+ to run.
Also make sure to install docker and docker-compose for your operating system.
Clone the repository
Run using go locally.
Install the dependencies:
Run the application
This will start the dev server on port 8080.
Or run using docker in dev environment
Build required docker images and start the container.
This will automatically start the go-app on port 8080 if in dev mode.
If in prod mod it will start the server on port 80 TCP.
This setup provides a scalable and easily deployable web application architecture that can be customized further according to specific requirements.
Environment Variables
To run this application, you will need to add the following environment variables to your .env file.
For development mode the
.env
file is present in backend directory.SECRET_KEY
NGINX_PORT
DB_USERNAME
DB_PASSWORD
DB_HOST
DB_PORT
DB_NAME
ADMIN_ORG_NAME
ADMIN_ORG_HEAD
ADMIN_USERNAME
ADMIN_PASSWORD
Important change to be made before running
Postman API Reference
Admin endpoints - protected with isAdminCheck middleware
Create Organisation
name
string
head
string
Delete Organisation
name
string
Update Organisation details
name
string
head
string
Create User Account
username
string
password
string
org_id
string
Delete User Account
username
string
User endpoints - public
Login to account - will create a new JWT token with expiry time of 1 hour
username
string
password
string
Refresh access token - grants a new acces token with an expiry time of 24 hours
jwt
access token
Get user account
jwt
access token
Logout
jwt
access token
Get people in same organisation
jwt
access token
Running Unit Tests
To run tests, run the following commands.
Flush the whole DB before running the tests so that mock data doesn't intersect with the previous data.
Make sure you are in the root directory of the project where the docker-compose files are present.
Start the MySQL server service only if you are using Docker.
Now run unit tests.
Make sure you are in the backend directory where the go.mod file is located.
Rationale
Golang Framework: GoFiber is a lightweight and fast web framework that is perfect for building APIs. It has a simple and intuitive API that makes it easy to build and maintain APIs. It also has excellent performance and scalability, making it ideal for high-traffic applications.
MySQL Database: MySQL is a popular open-source relational database management system. It is known for its robustness, scalability, and performance. It also has excellent support for ACID transactions, making it ideal for applications that require data consistency and reliability.
Gorm: GORM is a powerful and easy-to-use ORM for Golang. It provides a simple and intuitive API for interacting with databases, making it easy to build and maintain database-driven applications. It also has excellent support for MySQL, making it an ideal choice for this project.
DB Design
The database will have two tables called "users" and "organisations" that will store the user and organisation details respectively. The tables will have the following columns:
Users Table
id
int
username
string
password
string
is_admin
bool
org_id
int
Organisations Table
id
int
name
string
head
string
The "id" column will be the primary key for both tables. The "org_id" column in the "users" table will be a foreign key that references the "id" column in the "organisations" table.
The password will be hashed using bcrypt before being stored in the database to ensure security.
Acknowledgements
Authors
License
MIT