A modern RESTful blog API implemented in Go, complete with JWT authentication, CRUD operations, pagination, and basic tagging. Designed for backend developers to learn or extend a production-ready service.
- Overview
- Features
- Tech Stack & Requirements
- API Endpoints
- Installation & Run
- Usage Examples
- Code Structure
- Security & Best Practices
- Enhancements
- Contributing
- License
This Go-based API implements a backend for a blogging platform, providing authentication, article management, tagging, and search. Built with performance and simplicity in mind—perfect for learning REST patterns, database integration, and secure design.
- 🔐 User registration & login with JWT authentication
- 📝 CRUD operations for blog posts (Create / Read / Update / Delete)
- 🗂️ Tag-based filtering & post searches
- 📄 Pagination for article lists
- 🧹 Input validation and error handling
- ✅ Role-based access (author vs reader)
- Go 1.18+ (modules enabled)
- Gin or Chi router (fast HTTP handling)
- GORM (PostgreSQL/MySQL/SQLite support)
- GitHub.com/golang-jwt/jwt for token handling
- Validator.v10 for input validation
Endpoint | Method | Description | Auth Required |
---|---|---|---|
/api/register |
POST | Create a new user | ❌ |
/api/login |
POST | Authenticate and obtain JWT | ❌ |
/api/posts |
GET | List posts (with pagination/query filters) | ❌ |
/api/posts |
POST | Create a new post | ✅ |
/api/posts/{id} |
GET | Retrieve a specific post | ❌ |
/api/posts/{id} |
PUT | Update an existing post | ✅ |
/api/posts/{id} |
DELETE | Delete a post | ✅ |
/api/tags |
GET | Retrieve all tags | ❌ |
git clone https://github.com/MisaghMomeniB/Blog-Service-Go.git
cd Blog-Service-Go
go mod tidy
export DB_DSN="postgres://user:pass@localhost:5432/blogdb?sslmode=disable"
export JWT_SECRET="your_jwt_secret"
go run main.go
This initializes the API at http://localhost:8080
.
curl -X POST http://localhost:8080/api/register \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"secret"}'
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"secret"}'
curl -X POST http://localhost:8080/api/posts \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"title":"Hello Go","content":"My first post","tags":["go","api"]}'
Blog-Service-Go/
├── cmd/ # main app entrypoint (main.go)
├── config/ # configuration, env loading
├── controllers/ # HTTP handlers
├── models/ # DB models (User, Post, Tag)
├── repositories/ # DB access logic using GORM
├── middleware/ # JWT auth, logging, validation
├── routes/ # Router setup
├── utils/ # Helpers (JWT, hashing)
├── go.mod
└── README.md
- Uses bcrypt for secure password hashing
- Implements JWT tokens with expiration via
jwt-go
- Sanitizes inputs and enforces schema validation
- Role-based authorization ensures users only modify their own content
- 🧠 Add comments, likes, and user profiles
- 📦 Implement role-based permissions (admin/editor pipelines)
- ♻️ Add caching (Redis) for list endpoints
- 📤 Add media uploads, image handling, and thumbnails
- 🧪 Write unit tests and integration tests
Contributions welcome! Steps:
- Fork the repo
- Create a feature branch (
feature/…
) - Add changes/tests
- Submit a detailed Pull Request
Released under the MIT License — see LICENSE
for details.