A simple REST API for managing users built with Go and SQLite.
# Install dependencies
go mod tidy
# Run the server
go run cmd/server/main.go
The server will start on http://localhost:8080
.
GET /api/v1/users/{id}
Response:
{
"user_id": 123,
"user_name": "john_doe",
"email_address": "john@example.com",
"full_name": "John Doe",
"registration_date": "2023-01-15T10:30:00Z",
"user_age": 25,
"is_active": true
}
Error Response:
{
"error": {
"message": "User not found",
"code": "USER_NOT_FOUND"
}
}
GET /api/v1/users
Response:
[
{
"user_id": 123,
"user_name": "john_doe",
"email_address": "john@example.com",
"full_name": "John Doe",
"registration_date": "2023-01-15T10:30:00Z",
"user_age": 25,
"is_active": true
}
]
POST /api/v1/users
Request Body:
{
"user_name": "newuser",
"email_address": "user@example.com",
"full_name": "New User",
"user_age": 30,
"is_active": true
}
Response: 201 Created
{
"user_id": 124,
"user_name": "newuser",
"email_address": "user@example.com",
"full_name": "New User",
"registration_date": "2023-04-01T12:00:00Z",
"user_age": 30,
"is_active": true
}
PUT /api/v1/users/{id}
Request Body: (all fields optional)
{
"user_name": "updateduser",
"email_address": "updated@example.com",
"full_name": "Updated User",
"user_age": 35,
"is_active": false
}
Response: 200 OK
{
"user_id": 123,
"user_name": "updateduser",
"email_address": "updated@example.com",
"full_name": "Updated User",
"registration_date": "2023-01-15T10:30:00Z",
"user_age": 35,
"is_active": false
}
DELETE /api/v1/users/{id}
Response: 204 No Content
The API returns appropriate HTTP status codes:
200 OK
- Success201 Created
- Resource created successfully204 No Content
- Resource deleted successfully400 Bad Request
- Invalid request data404 Not Found
- Resource not found422 Unprocessable Entity
- Validation errors500 Internal Server Error
- Server errors
All error responses follow this format:
{
"error": {
"message": "Descriptive error message",
"code": "ERROR_CODE"
}
}
The API uses SQLite with the following schema:
CREATE TABLE users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_name TEXT NOT NULL UNIQUE,
email_address TEXT NOT NULL,
full_name TEXT NOT NULL,
registration_date DATETIME DEFAULT CURRENT_TIMESTAMP,
user_age INTEGER NOT NULL,
is_active BOOLEAN DEFAULT TRUE
);
The database comes pre-populated with sample users:
user_id | user_name | email_address | full_name | user_age | is_active |
---|---|---|---|---|---|
1 | john_doe | john@example.com | John Doe | 25 | true |
2 | jane_smith | jane@example.com | Jane Smith | 30 | false |
3 | bob_wilson | bob@example.com | Bob Wilson | 28 | true |
You can test the API using curl:
# Get all users
curl http://localhost:8080/api/v1/users
# Get user by ID
curl http://localhost:8080/api/v1/users/1
# Create a new user
curl -X POST http://localhost:8080/api/v1/users \
-H "Content-Type: application/json" \
-d '{
"user_name": "testuser",
"email_address": "test@example.com",
"full_name": "Test User",
"user_age": 25,
"is_active": true
}'
# Update a user
curl -X PUT http://localhost:8080/api/v1/users/1 \
-H "Content-Type: application/json" \
-d '{
"full_name": "John Updated Doe"
}'
# Delete a user
curl -X DELETE http://localhost:8080/api/v1/users/1
The application follows a clean architecture pattern:
cmd/server/main.go
- Application entry pointpkg/api/handlers.go
- HTTP request handlerspkg/api/types.go
- Data structures and typespkg/api/database.go
- Database operationsdata/users.db
- SQLite database file (created automatically)
The API is built using Go's standard net/http
library with manual routing and JSON handling.