Build a simple RESTful API in Golang that manages a list of users. Use MongoDB for persistence, JWT for authentication, and follow clean code practices.
Each user should have:
ID(auto-generated)Name(string)Email(string, unique)Password(hashed)CreatedAt(timestamp)
- Register a new user.
- Authenticate user and return a JWT.
- Use JWT for protecting endpoints.
- Use middleware to validate tokens.
- Use HMAC (HS256) with a secret key.
- Create a new user.
- Fetch user by ID.
- List all users.
- Update a user's name or email.
- Delete a user.
- Use the official Go MongoDB driver.
- Store and retrieve users from MongoDB.
- Logging middleware that logs HTTP method, path, and execution time.
- Run a background goroutine every 10 seconds that logs the number of users in the DB.
Write unit tests
Use Go’s testing package. Mock MongoDB where possible.
- Add Docker +
docker-composefor API + MongoDB. - Use Go interfaces to abstract MongoDB operations for testability.
- Add input validation (e.g., required fields, valid email).
- Implement graceful shutdown using
context.Context. - gRPC Version
- Create a
.protofile forCreateUserandGetUser. - Implement a gRPC server.
- (Optional) Secure gRPC with token metadata.
- Create a
- Hexagonal Architecture
- Structure the project using hexagonal (ports & adapters) architecture:
- Separate domain, application, and infrastructure layers.
- Use interfaces for data access and external dependencies.
- Keep business logic decoupled from frameworks and DB drivers.
- Structure the project using hexagonal (ports & adapters) architecture:
- Submit a GitHub repo or zip file.
- Include a
README.mdwith:- Project setup and run instructions
- JWT token usage guide
- Sample API requests/responses
- Any assumptions or decisions made
- Code quality, structure, and readability
- REST API correctness and completeness
- JWT implementation and security
- MongoDB usage and abstraction
- Bonus: gRPC, Docker, validation, shutdown
- Testing coverage and mocking
- Use of idiomatic Go