Greenlight API is a JSON-based RESTful API for managing movie information, inspired by the Open Movie Database API. This project is built with Go and uses PostgreSQL as the database for storing movie details, user authentication, and more.
- Movie Management: Create, update, delete, and fetch movie details.
- User Registration and Authentication: User registration, activation, and JWT-based authentication.
- Permission-based Authorization: Secure user access to resources based on their permissions.
- Rate Limiting: Implement rate limiting to prevent abuse.
- Filtering, Sorting, and Pagination: Powerful query support for movie listings.
- Graceful Shutdown: Ensure that the API shuts down gracefully when required.
- Structured Logging: Clear and structured logs for easier debugging and monitoring.
- Go: The backend language used for building the API.
- PostgreSQL: The database for persisting movie and user information.
- JWT: Used for securing user authentication.
- Docker: For containerization and easy setup.
git clone https://github.com/curator69/greenlight-api.git
cd greenlight-api
Make sure you have Go and PostgreSQL installed. Then run:
go mod download
Create a .env file in the root directory and add the following:
GREENLIGHT_DB_DSN="postgres://username:password@localhost/greenlight?sslmode=disable"
Replace username and password with your PostgreSQL credentials.
make db/migrations/up
make build/api
./bin/api
Once the API server is running, you can interact with it using tools like Postman or curl. Below are some sample API requests.
# Register a new user
curl -X POST {{APP_URL}}/users -d '{"name":"Alice Smith", "email":"[email protected]", "password":"pa55word"}'
# Log in to get the JWT token
curl -X POST {{APP_URL}}/tokens/authentication -d '{"email":"[email protected]", "password":"pa55word"}'
# Fetch all movies
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" {{APP_URL}}/movies
# Add a new movie
curl -X POST -H "Authorization: Bearer YOUR_JWT_TOKEN" -d '{"title": "Inception", "year": 2010, "runtime": "148 mins", "genres": ["action", "sci-fi"]}' {{APP_URL}}/movies
- GET
/v1/healthcheck
: Check the API status. - POST
/v1/users
: Register a new user. - POST
/v1/tokens/authentication
: Log in and receive a JWT token.
- GET
/v1/movies
: Retrieve a list of all movies. - POST
/v1/movies
: Create a new movie. - GET
/v1/movies/:id
: Get details of a specific movie. - PATCH
/v1/movies/:id
: Update an existing movie. - DELETE
/v1/movies/:id
: Delete a specific movie.
To manage database schema migrations, this project uses the migrate tool. You can apply or rollback migrations using the following commands:
# Run migrations
make db/migrations/up
# Rollback migrations
make db/migrations/down
Alex Edwards for the Let's Go Futher book, which inspired the structure of this project.