A reusable Go REST API template with Chi, modular structure & best practices!
- Chi Router: Lightweight and efficient HTTP routing.
- MongoDB Integration: Easily configurable MongoDB database support.
- Concurrency Support: Goroutines for optimal performance.
- Modular Structure: CMD architecture for scalable projects.
- Task Management Example: CRUD implementation to demonstrate usage.
- Middleware: Logging, error recovery, and more.
- Customizable: Designed to be a flexible starting point for your needs.
- Go 1.23.3 or later
- MongoDB instance (local or cloud)
-
Clone the repository:
git clone https://github.com/yigit433/go-rest-template.git cd go-rest-template
-
Install dependencies:
go mod tidy
-
Set up your MongoDB connection string in the environment variables or configuration file.
-
Run the server:
go run ./cmd/app/main.go
-
The server will be available at
http://localhost:8080
.
.
βββ cmd
β βββ app
β βββ main.go # Entry point of the application, starts the app
β
βββ internal
β βββ app # Core application logic and features
β β βββ routes # HTTP route definitions and routing setup
β β β βββ tasks # Tasks-specific router
β β β βββ handler.go # HTTP handlers for task-related requests
β β β βββ setup.go # Route setup and middleware binding for tasks
β β β
β β βββ repository # Data access layer (interfacing with databases)
β β β βββ tasks_repository.go # Data operations related to task
β β β
β β βββ model # Data models used across the application
β β βββ tasks_model.go # Definition of the Task data model
β β
β βββ configs # Configuration management (e.g., environment variables, settings)
β β
β βββ database # Database-related logic and connection management
β βββ mongodb.go # MongoDB setup and configuration details
β
βββ go.mod # Go module definition, dependency tracking
βββ README.md # Project documentation (setup, usage, etc.)
Method | Endpoint | Description |
---|---|---|
GET | /tasks |
Retrieve all tasks |
POST | /tasks |
Create a new task |
PUT | /tasks/{id} |
Update an existing task |
DELETE | /tasks/{id} |
Delete a task |
All HTTP requests and responses are logged to the console for debugging and monitoring.
Example log output:
2025-01-26T13:17:59+03:00 INF [App: app] π§ '.env' file loaded and settings applied seamlessly! eventId=config_load
2025-01-26T13:17:59+03:00 ERR [App: app] β οΈ 'MONGO_URI' is not set in the environment variables eventId=dbconfig_load
2025-01-26T13:17:59+03:00 INF [App: app] π Server is running eventId=startup port=8080
2025/01/26 13:18:24 "GET http://localhost:8080/tasks HTTP/1.1" from [::1]:58303 - 200 8B in 0s
Contributions are welcome! Feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch-name
). - Make your changes and commit them (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature-branch-name
). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Thanks to the open-source community for inspiration and support!
Happy coding! π»