{"id":19430603,"url":"https://github.com/jmrashed/rest-api-golang-mongodb","last_synced_at":"2026-05-09T14:05:44.628Z","repository":{"id":189149680,"uuid":"680105209","full_name":"jmrashed/rest-api-golang-mongodb","owner":"jmrashed","description":"A concise, production-oriented starter for building RESTful APIs in Go using MongoDB. This repository demonstrates a clean project layout, layered architecture (handlers, services, repositories), simple middleware, and migration scripts for a MongoDB-backed service.","archived":false,"fork":false,"pushed_at":"2025-10-06T03:47:39.000Z","size":41,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-11T06:57:44.227Z","etag":null,"topics":["golang","graphql","mongodb","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmrashed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-18T10:58:25.000Z","updated_at":"2025-10-14T15:57:21.000Z","dependencies_parsed_at":"2024-06-20T23:09:17.779Z","dependency_job_id":"ed386636-3a3d-44dc-bc97-8b9eeebde5f4","html_url":"https://github.com/jmrashed/rest-api-golang-mongodb","commit_stats":null,"previous_names":["jmrashed/rest-api-golang-mongodb","mrzstack/rest-api-golang-mongodb"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jmrashed/rest-api-golang-mongodb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmrashed%2Frest-api-golang-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmrashed%2Frest-api-golang-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmrashed%2Frest-api-golang-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmrashed%2Frest-api-golang-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmrashed","download_url":"https://codeload.github.com/jmrashed/rest-api-golang-mongodb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmrashed%2Frest-api-golang-mongodb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32672686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","graphql","mongodb","rest-api"],"created_at":"2024-11-10T14:25:47.398Z","updated_at":"2026-05-06T00:05:49.387Z","avatar_url":"https://github.com/jmrashed.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RESTful API with Go and MongoDB\n\n[![LinkedIn](https://img.shields.io/badge/LinkedIn-0A66C2?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white)](https://www.linkedin.com/in/jmrashed/) \n[![LeetCode](https://img.shields.io/badge/LeetCode-FFA116?style=for-the-badge\u0026logo=leetcode\u0026logoColor=black)](https://leetcode.com/u/jmrashed/)\n[![HackerRank](https://img.shields.io/badge/HackerRank-00EA64?style=for-the-badge\u0026logo=hackerrank\u0026logoColor=white)](https://www.hackerrank.com/profile/jmrashed)\n[![Email](https://img.shields.io/badge/Email-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white)](mailto:jmrashed@gmail.com)\n[![WhatsApp](https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white)](https://wa.me/8801734446514)\n\nA concise, production-oriented starter for building RESTful APIs in Go using MongoDB. This repository demonstrates a clean project layout, layered architecture (handlers, services, repositories), simple middleware, and migration scripts for a MongoDB-backed service.\n\n## Table of contents\n\n- Project structure\n- Features\n- Requirements\n- Quick start (PowerShell)\n- Configuration\n- API endpoints\n- Project conventions\n- Tests\n- Contributing\n- License \u0026 Contact\n\n## Project structure\n\nTop-level layout (important folders):\n\n- `cmd/` - application entrypoint (`main.go`)\n- `internal/config/` - configuration loader\n- `internal/handler/` - HTTP handlers\n- `internal/service/` - business logic\n- `internal/repository/` - MongoDB data access\n- `internal/router/` - route registration\n- `internal/middleware/` - HTTP middleware\n- `internal/model/` - domain models\n- `internal/util/` - helpers/utilities\n- `migrations/` - SQL migration files (if needed for other DBs) and DB seed scripts\n- `scripts/` - helper scripts (build, migrate, deploy)\n\n## Features\n\n### Core Features\n- RESTful API with proper HTTP status codes\n- MongoDB integration with connection pooling\n- Layered architecture (handler → service → repository)\n- API versioning (/api/v1)\n\n### Authentication \u0026 Authorization\n- JWT-based authentication\n- User registration and login\n- Role-based access control (user/admin)\n- Password hashing with bcrypt\n- Protected routes with middleware\n\n### Data Management\n- Input validation with custom validator\n- Pagination, sorting, and filtering\n- Unique constraints for username and email\n- Proper error handling with custom error types\n\n### Security \u0026 Performance\n- CORS middleware\n- Rate limiting per IP\n- Request logging\n- Standardized API error responses\n\n### Development\n- Comprehensive configuration management\n- Unit and integration tests\n- Docker support\n- Makefile for common tasks\n\n## Requirements\n\n- Go 1.20+ (check `go.mod` for exact version)\n- A running MongoDB instance (local or cloud)\n\n## Quick start (PowerShell)\n\n1. Clone the repo and change directory:\n\n```powershell\ngit clone https://github.com/jmrashed/rest-api-golang-mongodb.git .\ncd d:\\laragon\\www\\open-sources\\html\\go\\rest-api-golang-mongodb\n```\n\n2. Set required environment variable(s) and run the app (example uses local MongoDB):\n\n```powershell\n# Set the MongoDB connection string (update with your credentials/host)\n$env:MONGODB_URL = 'mongodb://localhost:27017'\n\n# Run the server\ngo run cmd/main.go\n```\n\nNote: The app expects the `MONGODB_URL` environment variable to be defined. See `internal/config/config.go` for details.\n\nIf you prefer to build an executable first:\n\n```powershell\n# Build\ngo build -o bin/app cmd/main.go\n\n# Run\n.\\bin\\app\n```\n\nIf there are migration scripts to apply (check `migrations/`), run the provided script (PowerShell example):\n\n```powershell\n# Example migration script (provided by repo)\nbash scripts/migrate-db.sh\n```\n\nOn Windows you may need WSL or Git Bash to run bash scripts. The `scripts/` folder also contains a `deploy.sh` and `build.sh` for Linux/macOS environments.\n\n## Configuration\n\nThe application reads configuration from environment variables. Copy `.env.example` to `.env` and adjust values:\n\n```bash\ncp .env.example .env\n```\n\n### Environment Variables\n- `MONGODB_URL` - MongoDB connection string (default: mongodb://localhost:27017)\n- `DB_NAME` - Database name (default: restapi)\n- `SERVER_PORT` - Server port (default: 8080)\n- `JWT_SECRET` - JWT signing secret (required for production)\n- `JWT_EXPIRY` - JWT token expiry duration (default: 24h)\n- `RATE_LIMIT` - Rate limit per IP (default: 100)\n- `RATE_DURATION` - Rate limit duration (default: 1h)\n\n## API Endpoints\n\nThe API is versioned and uses `/api/v1` as the base path. Default server runs on `http://localhost:8080`.\n\n### Authentication Endpoints\n- `POST /api/v1/auth/register` - Register a new user\n- `POST /api/v1/auth/login` - Login user\n\n### User Endpoints (Protected)\n- `GET /api/v1/users` - List users with pagination, sorting, and filtering\n- `GET /api/v1/users/{id}` - Get user by ID\n- `PUT /api/v1/users/{id}` - Update user (own profile or admin)\n- `DELETE /api/v1/users/{id}` - Delete user (own profile or admin)\n\n### Admin Only Endpoints\n- `POST /api/v1/users` - Create user (admin only)\n\n### Query Parameters for User Listing\n- `page` - Page number (default: 1)\n- `limit` - Items per page (default: 10)\n- `sort` - Sort field (default: created_at)\n- `order` - Sort order: asc/desc (default: desc)\n- `username` - Filter by username (partial match)\n- `email` - Filter by email (partial match)\n- `role` - Filter by role (exact match)\n\n## Project conventions\n\n- Keep business logic in `internal/service/`.\n- Data access and MongoDB queries belong in `internal/repository/`.\n- Keep models simple POJOs in `internal/model/`.\n\n## Tests\n\nThe project includes unit and integration tests:\n\n```powershell\n# Run all tests\ngo test ./...\n\n# Run tests with coverage\ngo test -coverprofile=coverage.out ./...\ngo tool cover -html=coverage.out\n\n# Run specific test packages\ngo test ./tests/unit/...\ngo test ./tests/integration/...\n```\n\n### Test Structure\n- `tests/unit/` - Unit tests for individual components\n- `tests/integration/` - Integration tests for API endpoints\n\n## Contributing\n\nContributions are welcome. Please follow the contributor guidelines in `CONTRIBUTING.md` and include tests for new behavior.\n\n## License \u0026 Contact\n\nThis project is available under the MIT License - see the `LICENSE` file for details.\n\nContact / social links:\n\n- LinkedIn: https://www.linkedin.com/in/jmrashed/\n- LeetCode: https://leetcode.com/u/jmrashed/\n- HackerRank: https://www.hackerrank.com/profile/jmrashed\n- Email: jmrashed@gmail.com\n- WhatsApp: https://wa.me/8801734446514\n \n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmrashed%2Frest-api-golang-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmrashed%2Frest-api-golang-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmrashed%2Frest-api-golang-mongodb/lists"}