Skip to content

GanFay/SecureBlog-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ SecureBlog API

A production-style REST API for a blogging platform built with Go, Gin, and PostgreSQL.

The project implements a secure authentication system using JWT access tokens, refresh tokens, and ownership protection for blog posts.

It also includes:

  • ๐Ÿณ Docker setup
  • ๐Ÿ“š Swagger documentation
  • ๐Ÿ” Secure authentication
  • ๐Ÿ“„ Pagination and filtering
  • ๐Ÿงฑ Clean modular architecture

This project was built as a backend portfolio project demonstrating how a real-world API can be structured and implemented.


โœจ Features

๐Ÿ” Authentication

  • User registration
  • User login
  • JWT access tokens
  • Refresh token flow
  • Logout functionality
  • /users/me endpoint to retrieve the current authenticated user

๐Ÿ›ก Security

  • Password hashing using bcrypt
  • JWT authentication middleware
  • Protected routes
  • Ownership validation (only the author can edit/delete their posts)

๐Ÿ“ Blog Posts

  • Create post
  • Update post
  • Delete post
  • Get all posts
  • Get post by ID
  • Search posts by text
  • Pagination support (limit / offset)

โš™ API Infrastructure

  • PostgreSQL database
  • Docker Compose environment
  • Environment variables configuration
  • Swagger (OpenAPI) documentation
  • Clean modular project architecture

๐Ÿงฐ Tech Stack

Technology Purpose
Go Backend language
Gin HTTP framework
PostgreSQL Database
pgxpool PostgreSQL driver
JWT Authentication
bcrypt Password hashing
Docker Containerization
Swagger (OpenAPI) API documentation

๐Ÿ“‚ Project Structure

SecureBlog-API
โ”‚
โ”œโ”€โ”€ auth/                 # JWT logic and password hashing
โ”‚   โ”œโ”€โ”€ password.go
โ”‚   โ””โ”€โ”€ token.go
โ”‚
โ”œโ”€โ”€ docs/                 # Swagger documentation (generated)
โ”‚
โ”œโ”€โ”€ handlers/             # HTTP handlers
โ”‚   โ”œโ”€โ”€ auth.go
โ”‚   โ”œโ”€โ”€ posts.go
โ”‚   โ”œโ”€โ”€ middleware.go
โ”‚   โ”œโ”€โ”€ me.go
โ”‚   โ””โ”€โ”€ ping.go
โ”‚
โ”œโ”€โ”€ models/               # Data models
โ”‚   โ”œโ”€โ”€ post.go
โ”‚   โ””โ”€โ”€ user.go
โ”‚
โ”œโ”€โ”€ router/               # Router configuration
โ”‚   โ””โ”€โ”€ router.go
โ”‚
โ”œโ”€โ”€ postgres_data/        # PostgreSQL volume
โ”‚
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ main.go
โ””โ”€โ”€ README.md

๐Ÿš€ Running the Project

๐Ÿณ Using Docker (recommended)

Start the API and PostgreSQL database:

docker compose up --build

API will be available at:

http://localhost:8080

Swagger documentation:

http://localhost:8080/swagger/index.html

swagger.img


๐Ÿ’ป Running Locally (without Docker)

Install dependencies:

go mod tidy

Run the server:

go run main.go

โš™ Environment Variables

Create a .env file in the project root.

Example:

PG_USER=bloguser
PG_PASSWORD=admin
PG_DB=blogdb

JWT_SECRET=super_secret_jwt_key

APP_PORT=8080

DB_URL=postgres://bloguser:admin@localhost:5432/blogdb?sslmode=disable

๐Ÿ“ก API Endpoints

๐ŸŒ Public Endpoints

Method Endpoint Description
GET /ping Check server status
POST /auth/register Register a new user
POST /auth/login Login user
GET /auth/refresh Refresh access token

๐Ÿ”’ Authenticated Endpoints

Require header:

Authorization: Bearer <access_token>
Method Endpoint Description
GET /users/me Get current user
POST /auth/logout Logout user
POST /posts Create post
GET /posts Get all posts
GET /posts/:id Get post by ID
PUT /posts/:id Update post
DELETE /posts/:id Delete post

๐Ÿ“„ Pagination

The posts endpoint supports pagination.

Example:

GET /posts?limit=10&offset=0

Parameters:

Parameter Description
limit number of posts returned
offset number of skipped posts

Example:

GET /posts?term=golang&limit=5&offset=10

๐Ÿ”‘ Authentication Flow

Login

POST /auth/login

Response:

{
  "access_token": "JWT_TOKEN"
}

Access Protected Routes

Requests must include:

Authorization: Bearer <token>

Refresh Token

GET /auth/refresh

Generates a new access token.


Logout

POST /auth/logout

Removes refresh token.


๐Ÿ—„ Database Schema

Users Table

users
Column Description
id user id
username username
email user email
password_hash hashed password
created_at account creation date

Posts Table

posts
Column Description
id post id
author_id post author
title post title
content post content
category post category
tags tags
created_at created time
updated_at last update time

๐Ÿ›ก Security Features

  • bcrypt password hashing
  • JWT authentication
  • token expiration
  • refresh token flow
  • ownership validation
  • protected routes middleware

๐Ÿ“š Swagger Documentation

Interactive API documentation:

http://localhost:8080/swagger/index.html

Swagger allows you to:

  • view all endpoints
  • inspect request schemas
  • test API directly in browser
  • authenticate using JWT

๐Ÿ”„ Example Workflow

Register

POST /auth/register

Login

POST /auth/login

Create Post

POST /posts

Get Posts

GET /posts

๐ŸŽฏ Purpose of the Project

This project demonstrates how to build a secure REST API backend with:

  • authentication
  • database integration
  • middleware
  • pagination
  • Docker infrastructure
  • API documentation

It can serve as:

  • ๐Ÿ’ผ a backend portfolio project
  • ๐Ÿš€ a starting point for a blogging platform
  • ๐Ÿ“š a learning project for Go backend development

๐Ÿ”ฎ Future Improvements

Possible extensions:

  • ๐Ÿ’ฌ comments system
  • โค๏ธ likes system
  • ๐Ÿ‘ฅ role-based access control
  • ๐Ÿ” full-text search
  • ๐Ÿšฆ rate limiting
  • ๐Ÿ“ฆ database migrations
  • โš™ CI/CD pipeline
  • โšก Redis caching

๐Ÿ“œ License

MIT License

About

Production-style REST API for a blogging platform built with Go, Gin, PostgreSQL, JWT authentication, refresh tokens, Docker and Swagger documentation.

localhost:8080/swagger/index.html

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors