How tkc/go-json-server works
Main Components
• Client / Consumer – Represents external users or automated tests interacting with the server (sending HTTP requests).
• API Configuration Loader – A module that reads and parses the JSON configuration file (located in the example directory and used by the server).
• Request Router / Handler – Core component that maps incoming HTTP requests to endpoints based on the configuration (using the src/handler package). This component distinguishes between API endpoints (using json files like health-check.json, users.json, etc.) and static file serving (through the folder attribute in the configuration).
• Middleware Chain – Illustrate the middleware layer (from src/middleware) that includes logging, CORS, panic recovery, request ID assignment, and any additional custom middleware (like authentication in advanced examples).
• Logging Service – The logging component (from src/logger) which provides structured log support (configurable via command-line flags and config settings).
• Configuration Management – Modules in src/config that manage application settings; also includes support for command-line flag overrides.
• File Server – The static file server element designed to serve files from directories (as specified in the configuration, e.g., serving the "static" folder).
Relationships and Data Flow
• A client makes an HTTP request toward the go-json-server.
• The request first enters the middleware chain – each middleware function processes the request (e.g., logging, CORS, recovery) before passing it forward.
• The Request Router (or Handler) then inspects the URL and method, compares it with the API configuration from the loaded JSON file, and either:
• Returns a JSON response from one of the configured JSON files (after matching the endpoint and replacing any path parameters).
• Serves static content from a designated folder if the endpoint maps to a file server route.
• The Logger records these interactions, errors, and delivery status.
• The Configuration Loader continuously monitors for changes (hot-reloading) and informs the Router to update endpoints if necessary.
Architectural Patterns & Design Principles
• Middleware architecture: A chain of reusable components that process requests.
• Configuration-driven behavior: The endpoints and file serving behaviors are fully dictated by external JSON configuration files.
• Modularity: Different responsibilities are separated in packages (config, handler, middleware, logger).
Relevant Technologies and Frameworks
• Go (the primary language) and its native HTTP libraries.
• JSON for configuration and responses.
• GitHub Actions (for CI/CD), as indicated by the workflows.
• Docker (as an optional deployment container technology, per the Dockerfile provided).