How raahii/golang-grpc-realworld-example works
Project Type and Purpose
• The project is a full-stack backend application written in Go that uses GRPC for internal communication and a grpc-gateway as a reverse proxy to translate RESTful requests to GRPC.
• It implements the RealWorld spec with CRUD operations, user authentication (via JWT), and typical content management features (articles, profiles, comments, favorites).
Break Down the File Structure into Main Components
• API Gateway:
• Directory: “gateway”
• File: gateway/gateway.go
• Function: Acts as a reverse proxy, translating RESTful JSON API calls into gRPC requests.
• GRPC Server:
• File: server.go
• Core business logic and gRPC service implementation.
• Request Handlers:
• Directory: “handler”
• Files: handler/*.go (article, comment, profile, user, etc.)
• Role: Layer to interpret incoming requests (from GRPC or REST gateway) and route them to service logic.
• Authentication:
• Directory: “auth”
• File: auth/auth.go
• Provides JWT-based authentication.
• Database and Storage:
• Directories: “db” (with subdirectories for data and seed) and “store”
• Files: db/db.go, db/seed/seed.go, and store files (article.go, user.go)
• Role: Manages database connectivity, ORM (using GORM) for persistence into MySQL.
• Business Models:
• Directory: “model”
• Files: model/*.go (article, comment, user)
• Represents data objects that the system manipulates.
• Protobuf Definitions:
• Directory: “proto”
• Files: *.proto and their generated Go files.
• Role: Defines the gRPC contracts and message formats used for inter-service communication.
• Auxiliary Components:
• Dockerfile, docker-compose.yml, Makefile, and environment files under “env”
• Provide containerization, build, testing, and deployment scripts.
Architectural Patterns and Design Principles
• Clearly denote the separation of concerns:
• API Layer (REST gateway) vs. Business Logic (gRPC server and handlers)
• Data access (store and database modules)
• Domain Models (model objects)
• From external client → API Gateway (REST request) → Internal GRPC request → gRPC handlers → Database operations.
• Document which components interact directly and note any asynchronous interactions if applicable.