Name: Andreas Nilsson
Email: [email protected]
This project is a RESTful API implemented with Go. It uses Postgres for data storage and is containerized using Docker.
It provides authentication using JWT.
- Go 1.23 or higher
- Docker
- Docker Compose
git clone https://github.com/yourusername/rest_api_go.git
cd rest_api_go
Set up your environment variables for database connection if you're not using docker-compose:
export POSTGRES_HOST=localhost
export POSTGRES_PORT=5432
export POSTGRES_USER=yourusername
export POSTGRES_PASSWORD=yourpassword
export POSTGRES_DB=yourdbname
export PORT=8080
-
Build Docker Images
Ensure Docker is running, then build the images:
docker-compose up --build
-
Access the Application
- REST API: Accessible via
http://localhost:8080
- REST API: Accessible via
-
Login
POST /login
Payload:
{ "username": "username", "password": "password" }
-
Get All Devices
GET /devices
-
Get Device by ID
GET /devices/{id}
-
Create Device
POST /devices
Payload:
{ "name": "Device Name", "version": "1.0" }
-
Repository Pattern: This pattern separates data access logic from business logic, making it easier to test and maintain. It abstracts the database operations, enabling mock implementations for testing.
-
Service Layer: Provides a higher level of abstraction over the repository, implementing business logic and interacting with multiple repositories if needed.
-
Middleware Pattern: Used for cross-cutting concerns such as authentication and logging. The JWT middleware implements token validation for protected routes.
-
REST (Representational State Transfer): The API follows REST principles with stateless operations, resource-based URLs, and standard HTTP methods (GET, POST, POST, UPDATE, PATCH).
-
HATEOAS (Hypermedia as the Engine of Application State): While not fully implemented, the API can be extended to include links in responses to enable clients to navigate the API dynamically.