Skip to content

CRUD REST API with JWT auth protecting sensitive routes through middleware.

Notifications You must be signed in to change notification settings

gbLw1/go-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go REST API

This project is a secure REST API with JWT authentication, storing access tokens in cookies, and protecting sensitive routes through middleware.


Dependencies


Database

Tried with SQL Server but can't connect to port 1433 using TelNet so I use PostgreSQL

issue: hashicorp/vault#4167


Setting up the project

Follow the steps below to set up the project.

Environment Variables

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

Run the following command to start the server:

go run ./cmd/main.go

Running with Air (optional)

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

Testing

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.


Endpoints

  • [POST] ~/signup
  • [POST] ~/login
  • [GET] ~/posts
  • [GET] ~/posts/{id}
  • [POST] ~/posts (Requires authentication)
  • [PUT] ~/posts/{id} (Requires authentication)
  • [DELETE] ~/posts/{id} (Requires authentication)

Releases

No releases published

Packages

No packages published

Languages