A modern and robust CRUD (Create, Read, Update, Delete) API for managing movies, built with Go and GORM, featuring JWT authentication, rate limiting, and more.
- Features
- Getting Started
- Prerequisites
- Installation
- Running the Application
- API Endpoints
- Environment Variables
- Contributing
- License
- CRUD Operations: Manage movies with Create, Read, Update, and Delete operations.
- JWT Authentication: Secure API access with JSON Web Tokens.
- Rate Limiting: Control API usage and prevent abuse.
- Pagination, Filtering, and Sorting: Efficient movie listings.
- Swagger/OpenAPI Documentation: Explore the API effortlessly.
- PostgreSQL: Robust database support.
- Environment Variable Configuration: Easy database connection setup.
- JSON-based API: Seamless integration with other services.
Follow these instructions to set up and run the project on your local machine.
- Go (version 1.16 or higher)
- PostgreSQL
- Swagger for API documentation (optional)
-
Clone the repository:
git clone https://github.com/your-username/go-crud-movies-api.git cd go-crud-movies-api
-
Install dependencies:
go mod tidy
-
Set up the database: Create a PostgreSQL database and configure the environment variables as described below.
-
Set up environment variables: Create a
.env
file in the project root with the following content:DB_HOST=localhost DB_PORT=5432 DB_USER=your_db_user DB_NAME=your_db_name DB_PASSWORD=your_db_password JWT_SECRET_KEY=your_secret_key
-
Run the project:
go run main.go
-
The API will be running at:
http://localhost:8080
- Login (Generate JWT Token):
Request body:
POST /auth/login
Response:{ "username": "user", "password": "password" }
{ "token": "your_jwt_token" }
-
Create a movie:
POST /movies
Request body:
{ "name": "Movie Name", "description": "Movie Description" }
Response:
{ "id": 1, "name": "Movie Name", "description": "Movie Description" }
-
Get all movies with pagination and sorting:
GET /movies?page=1&limit=10&sort=desc
Response:
[ { "id": 1, "name": "Movie Name", "description": "Movie Description" } ]
-
Get a single movie by ID:
GET /movies/{id}
Response:
{ "id": 1, "name": "Movie Name", "description": "Movie Description" }
-
Update a movie by ID:
PUT /movies/{id}
Request body:
{ "name": "Updated Movie Name", "description": "Updated Movie Description" }
Response:
{ "id": 1, "name": "Updated Movie Name", "description": "Updated Movie Description" }
-
Delete a movie by ID:
DELETE /movies/{id}
Response:
{ "message": "Movie deleted successfully" }
The application uses the following environment variables for configuration:
- DB_HOST: The database host (default:
localhost
) - DB_PORT: The database port (default:
5432
) - DB_USER: The database user
- DB_NAME: The database name
- DB_PASSWORD: The database password
- JWT_SECRET_KEY: The secret key for JWT token generation
Contributions are welcome! Feel free to fork the repository, make changes, and submit pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.