This project is a secure REST API with JWT authentication, storing access tokens in cookies, and protecting sensitive routes through middleware.
- Air - Hot reload
- GoDotEnv - Environment variables
- Gin Web Framework - Web framework
- Gorm - Database ORM
- Bcrypt - Password hashing
- JWT - JSON Web Token
Tried with SQL Server but can't connect to port 1433 using TelNet so I use PostgreSQL
issue: hashicorp/vault#4167
Follow the steps below to set up the project.
create a .env
file in the root directory and add the following:
PORT=8080
DB_URL="host=localhost user=gorm password=gorm dbname=gorm port=5432 sslmode=disable"
SECRET="Your_Super_Secret_Key"
PORT
is the port where the API will run (default: 8080). You can change it if you want.
DB_URL
is the connection string for the database. Change it according to your database credentials.
SECRET
is the secret key for JWT authentication.
Run the following command to start the server:
go run ./cmd/main.go
Air is a tool for running Go applications in the background, refreshing whenever it is modified.
If you got air
installed, run the following command
air
You can use Postman to test the API.
Base address: http://localhost:{port}/
ps: Check the port in .env
file and feel free to change it.
- [POST]
~/signup
- [POST]
~/login
- [GET]
~/posts
- [GET]
~/posts/{id}
- [POST]
~/posts
(Requires authentication) - [PUT]
~/posts/{id}
(Requires authentication) - [DELETE]
~/posts/{id}
(Requires authentication)