An open API service indexing awesome lists of open source software.

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.

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.