This is an example project demonstrating Clean Architecture principles in Go. It is a simple blog application that allows you to create and retrieve blog posts. The project structure is designed to separate concerns and make the application easy to maintain and test.
The project is organized into the following layers:
- Entities: Core business logic.
- Use Cases (Interactors): Application-specific business rules.
- Interface Adapters: Adapters for the user interface, database, web controllers, etc.
- Frameworks and Drivers: Framework-specific code, like routing, databases, etc.
.
│ .gitignore
│ coverage
│ go.mod
│ go.sum
│ gocleanarchitecture.exe
│ GoCleanArchitecture.Md
│ LICENSE
│ README.Md
│
├───.vscode
│ settings.json
│
├───cmd
│ blog.db
│ blogposts.json
│ gocleanarchitecture.exe
│ main.go
│ server.log
│
├───config
│ config.go
│
├───entities
│ blog_post.go
│
├───errors
│ errors.go
│
├───frameworks
│ ├───db
│ │ │ in_memory_blog_post_repository.go
│ │ │
│ │ └───sqlite
│ │ blog_post_repository.go
│ │ sqlite.go
│ │
│ ├───logger
│ │ logger.go
│ │
│ └───web
│ │ router.go
│ │
│ └───middleware
│ logging.go
│ recovery.go
│
├───interfaces
│ blog_post_controller.go
│ blog_post_repository.go
│
├───tests
│ ├───entities
│ │ blog_post_test.go
│ │
│ ├───errors
│ │ errors_test.go
│ │
│ ├───frameworks
│ │ ├───db
│ │ │ in_memory_blog_post_repository_test.go
│ │ │ sqlite_blog_post_repository_test.go
│ │ │
│ │ └───logger
│ │ logger_test.go
│ │
│ ├───interfaces
│ │ blog_post_controller_test.go
│ │
│ └───usecases
│ blog_post_usecase_test.go
│
└───usecases
blog_post_usecase.go
Clean Architecture promotes separation of concerns, making your codebase easier to maintain and test. Here are some benefits:
- Testability: Each layer can be tested independently.
- Maintainability: Changes in one layer have minimal impact on other layers.
- Scalability: Easily extend functionality by adding new layers or components.
- Flexibility: Swap out frameworks or tools with minimal changes to the overall architecture.
- CRUD operations for blog posts
- Clean Architecture implementation
- SQLite database integration
- In-memory database option
- Custom error handling
- Structured logging
- Middleware for logging and recovery
- Go 1.17 or higher
- SQLite3 (for this sample) You can use SQL, Firebase, Postgresql or whatever you choose.
-
Clone the repository:
git clone https://github.com/tomblanchard312/gocleanarchitecture.git cd gocleanarchitecture
-
Install dependencies:
go mod tidy
-
Build the application:
cd cmd go build -o gocleanarchitecture.exe ./cmd
-
Run the application:
./gocleanarchitecture.exe
-
The server will start on port 8080. You can test the endpoints using curl or any API testing tool.
POST /blogposts
: Create a new blog postGET /blogposts
: Retrieve all blog postsGET /blogposts/{id}
: Retrieve a specific blog postPUT /blogposts/{id}
: Update a blog postDELETE /blogposts/{id}
: Delete a blog post
curl -X POST http://localhost:8080/blogposts -H "Content-Type: application/json" -d '{"ID":"1", "Title":"Test Title", "Content":"Test Content"}'
curl http://localhost:8080/blogposts
To run all tests:
go test ./...
To run tests for a specific package:
go test ./tests/entities
go test ./tests/usecases
go test ./tests/interfaces
go test ./tests/frameworks/db
go test ./tests/frameworks/logger
To run a specific test function:
go test ./tests/entities -run TestBlogPostCreation
go test ./tests/usecases -run TestCreateBlogPost
To run tests with verbose output:
go test -v ./tests/...
To run tests and see coverage:
go test ./tests/... -coverprofile=coverage.out
go tool cover -html=coverage.out
Logs are written to server.log for debugging purposes. Ensure the log file is writable and check it for any server-related errors. License
This project uses custom error types defined in the errors
package. All errors are logged and appropriate HTTP status codes are returned to the client.
- Gorilla Mux for HTTP routing
- Viper for configuration management
The application can be configured using environment variables:
SERVER_PORT
: The port on which the server will run (default: ":8080")DB_PATH
: The path to the SQLite database file (default: "./blog.db")LOG_LEVEL
: The logging level (default: "info")LOG_FILE
: The path to the log file (default: "server.log")
Contributions are welcome! Please feel free to submit a Pull Request.
- Add authentication and authorization
- Implement a persistent database
- Add more CRUD operations for blog posts
This project is licensed under the MIT License. See the LICENSE file for details.
- The Clean Architecture by Robert C. Martin