This project is a basic RESTful API built in Go (Golang) for managing user data. It leverages an in-memory cache to efficiently store and retrieve user information.
- CRUD Operations:
- Create users (
POST /user
) - Read users (
GET /user/{id}
) - Delete users (
DELETE /user/{id}
) - (Note: Update is not implemented in this example)
- Create users (
- In-Memory Cache: Utilizes a map to store user data for fast access.
- Concurrency Safe: Employs a read-write mutex (
sync.RWMutex
) to handle concurrent access and prevent race conditions. - JSON Responses: Communicates with clients using JSON formatted data.
- Basic Error Handling: Includes preliminary checks for invalid requests (e.g., missing ID, malformed JSON).
Method | Endpoint | Description | Request Body (JSON) | Response (JSON) |
---|---|---|---|---|
POST | /user |
Create a new user | { "name": "John Doe" } |
204 No Content |
GET | /user/{id} |
Retrieve a user by ID | { "name": "John Doe" } |
|
DELETE | /user/{id} |
Delete a user by ID | 204 No Content |
- Go (Golang) must be installed on your system.
- Clone: Clone this repository to your local machine.
- Build: Navigate to the project directory and run
go build
. This will compile the Go code into an executable. - Run: Execute the compiled binary (e.g.,
./your-executable-name
). The server will start listening onhttp://localhost:8080
. - Interact: Use tools like
curl
,httpie
, or Postman to send requests to the API endpoints.
# Create a user
curl -X POST -H "Content-Type: application/json" -d '{"name": "Alice Johnson"}' http://localhost:8080/user
# Get a user (replace {id} with the actual user ID)
curl http://localhost:8080/user/{id}
# Delete a user
curl -X DELETE http://localhost:8080/user/{id}
- Data Volatility: User data is stored only in memory. When the server shuts down, all data is lost.
- Simplified Error Handling: Error responses could be more informative and granular.
- Missing Update: There's no functionality to modify existing user data.
- Persistent Storage: Integrate a database (e.g., SQLite, PostgreSQL) to save user data permanently.
- Update Endpoint: Implement a
PUT
orPATCH
endpoint to allow modifying user details. - Authentication/Authorization: Add security mechanisms to protect user data.
- Advanced Error Handling: Provide clearer error messages and consider using standard HTTP status codes more comprehensively.
Contributions are welcome! Feel free to fork this repository, suggest improvements, and submit pull requests.