This is a robust, production-ready backend for a blogging platform, built with Go, Gin, MongoDB, and Clean Architecture principles. It features modular domain-driven design, JWT authentication, role-based access control, advanced search/filtering, and full CRUD for blogs, users, comments, tags, and reactions.
- Features
- Architecture
- Tech Stack
- Setup & Installation
- Configuration
- Running the App
- API Endpoints
- Authentication & Roles
- Testing
- Development Workflow
- Troubleshooting
- Contributing
- Clean Architecture: strict separation of domain, usecase, repository, and delivery layers
- MongoDB with repository pattern and transaction support
- JWT authentication with role-based middleware (USER, ADMIN)
- Blog CRUD, search, filter, view count, like/dislike, and comment support
- Tag normalization and auto-creation
- User registration, login, OAuth2, password reset, email verification
- Integration and unit tests (testify)
- Configurable via
.envorconfig.env(Viper)
- domain/: Pure business logic, interfaces, and error definitions
- usecases/: Application logic, orchestrates domain and repositories
- repositories/: MongoDB implementations, DTO conversion, data access
- delivery/controllers/: Gin HTTP handlers, request/response DTOs
- delivery/routes/: Route registration, middleware wiring
- infrastructures/: Auth, config, and external service integrations
- Go 1.20+
- Gin (HTTP framework)
- MongoDB (with replica set for transactions)
- Viper (config)
- Testify (testing)
- JWT (github.com/golang-jwt/jwt)
- Go 1.20 or newer
- MongoDB (run as a replica set for transaction support)
- [Optional] MongoDB Compass for GUI
git clone https://github.com/InkForge/Blog_Website.git
cd Blog_Websitego mod tidy- Stop any running
mongodprocess. - Start MongoDB as a replica set:
mongod --replSet rs0 --dbpath "C:\data\db" - In a new terminal:
mongo > rs.initiate()
- [Optional] Use MongoDB Compass to view/manage data.
Create a .env or config.env file in the project root. Example:
MONGO_URI=mongodb://localhost:27017
MONGO_DB=blogdb
JWT_SECRET=your_jwt_secret
PORT=8080
EMAIL_FROM=your@email.com
EMAIL_PASS=your_email_password
go run delivery/main.goThe server will start on the port specified in your config (default: 8080).
POST /auth/register— Register a new userPOST /auth/login— Login and receive JWT cookieGET /auth/verify— Email verificationPOST /auth/forget— Request password resetPOST /auth/reset— Reset passwordPOST /auth/logout— Logout (requires auth)GET /auth/refresh— Refresh JWT (requires auth)
GET /blogs— List blogs (paginated)GET /blogs/:id— Get blog by ID (requires auth)POST /blogs— Create blog (auth: USER/ADMIN)PUT /blogs/:id— Update blog (auth: USER/ADMIN, must be author)DELETE /blogs/:id— Delete blog (auth: USER/ADMIN, must be author)GET /blogs/search— Search blogs by title/authorGET /blogs/filter— Filter blogs by tag, popularity, etc.
POST /blogs/:id/like— Like a blog (auth)POST /blogs/:id/dislike— Dislike a blog (auth)POST /blogs/:id/unlike— Remove like (auth)POST /blogs/:id/undislike— Remove dislike (auth)
GET /blogs/:id/comments— List comments for a blogPOST /blogs/:id/comments— Add comment (auth)PUT /comments/:id— Update comment (auth, must be author)DELETE /blogs/:id/comments/:commentID— Delete comment (auth, must be author)
POST /comments/:id/react/:status— React to comment (auth)GET /comments/:id/reaction— Get user reaction (auth)
- Tags are auto-created/normalized when creating/updating blogs.
- JWT tokens are issued on login and stored in an
auth_tokencookie. - Use the cookie in all requests to protected endpoints.
- Roles:
USER,ADMIN(case-sensitive, see domain/user.go) - Role-based middleware restricts access to certain endpoints.
- Unit and integration tests are in the
repositories/andinfrastructures/folders. - To run tests:
go test ./...- Integration tests require a running MongoDB instance.
- Use feature branches for new features/fixes.
- Pull latest changes:
git pull --rebase - Create a new branch:
git checkout -b feature/your-feature - Commit and push:
git add . && git commit -m "feat: ..." && git push origin feature/your-feature - Open a PR for review.
- Transactions error: Make sure MongoDB is running as a replica set.
- User ID not found in context: Ensure you are sending the
auth_tokencookie. - Config file not found: Create a
.envorconfig.envin the project root. - CORS issues: Configure Gin CORS middleware as needed.
- Email not sending: Check your SMTP credentials in config.
- Fork the repo and clone your fork.
- Create a new branch for your feature or bugfix.
- Write clear, well-documented code and tests.
- Open a pull request with a detailed description.
MIT