🔥 Golang Rest Api with basic JWT Authentication and Basic Crud Operations.
First of all create a new .env file in the root of the project directory. see example.env file for all required environment variables.
After setting up environment variables, run the following command to start development and database servers.
docker-compose up
before running this command you must have docker and docker-compose installed in your system.
In order to deploy this application to production environment, create a docker image from Dockerfile and run it on your production server.
- Language (golang)
- Database (mysql)
- Router (gorilla/mux)
- Server (net/http)
- JWT (dgrijalva/go-jwt)
- Database ORM (gorm)
- Live Reload (cosmtrek/air)
- Monlith
- Authentication
- endpoint protection with middleware
- Authorization - Role based Access Control (RBAC)
POST
/auth/login
Login with username/email and password.
{
"id": "abc123",
"password": "abc123",
}
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
"user": {
"id": "1",
"name": "ABC 123",
"username": "abc123",
"email": "admin@abc123.io",
}
}
POST
/auth/signup
Create a new user in the database.
{
"name": "ABC 123",
"username": "abc123",
"email": "admin@abc123.io",
"password": "abc123",
}
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
"user": {
"id": "1",
"name": "ABC 123",
"username": "abc123",
"email": "admin@abc123.io",
}
}
All endpoints are protected, must send valid jwt as Authorization
header with each request.
GET Â
/food/all
Get All Food Items
[
{
"id": "1",
"name": "Apples",
"quantity": 100,
"selling_price": "100 USD",
},
{
"id": "2",
"name": "Mangos",
"quantity": 97,
"selling_price": "120 USD",
}
]
GET Â
/food/<name>
Get single Food Item by its name. name should be lowercase (e.g /food/apples)
{
"id": "1",
"name": "Apples",
"quantity": 100,
"selling_price": "100 USD",
}
POST Â
/food
Add a new food item to the database.
{
"name": "Oranges",
"quantity": 44,
"selling_price": "80 USD",
}
{
"id": "1",
"name": "Oranges",
"quantity": 44,
"selling_price": "80 USD",
}
DELETE Â
/food/<id>
Delete one Food Item from the database.
{
"id": "1",
"name": "Oranges",
"quantity": 44,
"selling_price": "80 USD",
}