This is a simple and modern example of a CRUD (Create, Read, Update, Delete) API for managing movies, built using Go and GORM (Go Object Relational Mapper), along with added features such as JWT authentication, rate limiting, pagination, and Swagger documentation.
- Features
- Getting Started
- Prerequisites
- Installation
- Running the Application
- API Endpoints
- Environment Variables
- Contributing
- License
- Create, Read, Update, and Delete (CRUD) operations for managing movies.
- JWT-based Authentication for secured API access.
- Rate Limiting to control API usage and prevent abuse.
- Pagination, Filtering, and Sorting for movie listings.
- Swagger/OpenAPI Documentation for easy API exploration.
- Uses PostgreSQL as the database.
- Environment variable configuration for database connection.
- Simple JSON-based API for seamless integration.
Follow these instructions to set up and run the project on your local machine.
- Go (version 1.16 or higher)
- PostgreSQL
- Swagger for 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.