How jurazodda/mini-blog works
Project type and purpose
• This is a backend microservice: a RESTful Mini-Blog platform written in Go.
• Purpose: expose HTTP endpoints for user authentication, posts, comments, likes, and reposts; persist data in PostgreSQL; log events.
File structure
• cmd/
• main.go – application entrypoint (loads config, initializes router & services)
• migrate.go – migration entrypoint
• config/
• config.go – centralizes env/config parsing
• internal/
• handler/ – HTTP layer using Gin: auth, post, comment, like, repost, middleware, router setup, server startup
• service/ – business logic per domain (user, post, comment, like, repost)
• repository/ – data access with Gorm: repositories for each model, DB connection setup
• models/ – shared domain models
• errs/ – error definitions
• pkg/glog/ – wrapper around Zerolog for structured logging
• logs/ – persisted log files
• mini-blog.sql – database schema
• go.mod, Makefile, .gitignore – build/config scripts
README for additional insights
• Uses Gin (HTTP server), Gorm (ORM), PostgreSQL, Zerolog
• Features: registration/login, CRUD posts, comments, likes, reposts
a. Main components:
• Clients (Web/Mobile/UI)
• API Gateway / HTTP Server (Gin)
• Handlers (Gin routes & controllers)
• Service Layer (business logic)
• Repository Layer (Gorm + PostgreSQL)
• Configuration Loader
• Logging subsystem (pkg/glog + Zerolog)
b. Relationships and data flow:
1. Client → HTTP Request → Gin Router
2. Router → Handler
3. Handler → Service (enforces business rules, authorization)
4. Service → Repository (CRUD via Gorm)
5. Repository → PostgreSQL
6. Handler → HTTP Response → Client
7. Throughout: Logging at each layer via glog → writes to logs/*.log
c. Architectural patterns:
• Layered (Controller/Service/Repository)
• Hexagonal-ish: external systems (DB, clients) connected through well-defined ports (handlers, repos)
• Dependency injection via “config” and pass-around of interfaces
d. Technologies and frameworks:
• Gin: HTTP router & middleware
• Gorm: ORM connecting repository layer to PostgreSQL
• Zerolog (wrapped by pkg/glog) for structured, leveled logging
• JWT in handler/jwt.go for stateless authentication
Detailed breakdown
• Startup sequence: cmd/main.go → config loader → initialize DB & logger → register routes → start Gin server
• Authentication flow:
• Handler/auth.go ↔ Service/user.go ↔ Repository/user_repo.go
• JWT middleware in handler/jwt.go validating tokens on protected routes
• CRUD flows for posts, comments, likes, reposts, each following the Handler → Service → Repository → DB path
• Error handling via internal/errs propagated up to Handlers → HTTP error responses
• Logging calls at each layer, captured by pkg/glog → logs/*.log