https://github.com/misaghmomenib/blog-service-go
This is a blog service written in Go language You can use the codes Different packages are used.
https://github.com/misaghmomenib/blog-service-go
blog blog-service git go golang open-source
Last synced: 20 days ago
JSON representation
This is a blog service written in Go language You can use the codes Different packages are used.
- Host: GitHub
- URL: https://github.com/misaghmomenib/blog-service-go
- Owner: MisaghMomeniB
- License: gpl-3.0
- Created: 2025-02-08T18:34:46.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-09-06T08:41:20.000Z (2 months ago)
- Last Synced: 2025-09-06T10:21:33.782Z (2 months ago)
- Topics: blog, blog-service, git, go, golang, open-source
- Language: Go
- Homepage:
- Size: 1.28 MB
- Stars: 15
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# βοΈ Blog Service (Go)
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.
---
## π Table of Contents
1. [Overview](#overview)
2. [Features](#features)
3. [Tech Stack & Requirements](#tech-stack--requirements)
4. [API Endpoints](#api-endpoints)
5. [Installation & Run](#installation--run)
6. [Usage Examples](#usage-examples)
7. [Code Structure](#code-structure)
8. [Security & Best Practices](#security--best-practices)
9. [Enhancements](#enhancements)
10. [Contributing](#contributing)
11. [License](#license)
---
## π‘ Overview
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.
---
## β
Features
- π 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)
---
## π οΈ Tech Stack & Requirements
- 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
---
## π§ API Endpoints
| 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 | β |
---
## βοΈ Installation & Run
```bash
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`.
---
## π Usage Examples
### Register & Login (CLI)
```bash
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"}'
```
### Create a Post (with JWT)
```bash
curl -X POST http://localhost:8080/api/posts \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{"title":"Hello Go","content":"My first post","tags":["go","api"]}'
```
---
## π Code Structure
```
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
```
---
## π Security & Best Practices
* 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
---
## π§ Enhancements
* π§ 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**
---
## π€ Contributing
Contributions welcome! Steps:
1. Fork the repo
2. Create a feature branch (`feature/β¦`)
3. Add changes/tests
4. Submit a detailed Pull Request
---
## π License
Released under the **MIT License** β see `LICENSE` for details.