Watchlist API π¬
Watchlist API is a REST API that allows you to save and manage a list of films you want to watch. It includes features for registration, managing film collections, adding comments, and leaving feedback.
π΄ Live Server
π Navigation
β Main Features
API Functionality
- Registration and Authorization: Secure user registration and login with JWT.
- Collections Management: Create and manage film collections.
- Comments: Add and manage comments on films.
- Viewing Status: Mark films as viewed.
- Ratings and Reviews: Rate films and write reviews.
Authorization
Authorization in Watchlist API is handled using JWT (JSON Web Token) in the Authorization
header. This ensures secure access to the API by verifying the token sent with each request.
Authorization: Bearer <JWT_TOKEN>
Additional Features
- Permissions: Flexible permission system to control access to different IP endpoints based on permissions.
- Validator: Automatic request validation to ensure incoming data is properly formatted and meets required conditions before processing.
- Filters: Filtering options for API requests to allow users to filter films, collections, and other resources based on specific criteria.
π Technology Stack
- Programming Language: Go
- Authorization: JWT
- Database: PostgreSQL
- API Documentation: Swagger
- Log Aggregation: Loki
- Metrics: Prometheus + Node Exporter
- Visualization: Grafana (for logs and metrics)
- Testing: Postman
- Deployment: Docker, Docker Compose
- CI/CD: GitHub Actions
π Project Requirements
- Go: 1.18+
- PostgreSQL: 13+
For Deployment
- Docker: 20.10+
- Docker Compose: 1.29+
- Loki Docker Driver
For Testing
π GitHub Actions (CI/CD)
The project uses GitHub Actions to automate testing, building, and deployment processes on remote server.
Instructions on setting up the workflows
βNote: If you are not using GitHub Actions, you can remove the .github/workflows/deploy.yml.
π οΈ Installation and Setup
- Clone the repository:
git clone https://github.com/k4sper1love/watchlist-api.git
- Go to the project directory:
cd watchlist-api
- Install dependencies:
go mod tidy
- Install Loki Docker Driver (required for Depoyment):
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
βοΈ Configuration
Create an .env
file in the root directory and configure the environment variables. Use .env.example as a reference.
(Optional) VERSION=unknown
(Optional) GRAFANA_PASSWORD=password
APP_PORT=8001
APP_ENV=local
APP_MIGRATIONS=file://migrations
APP_SECRET=TOKENPASSWORD
(Optional) APP_TELEGRAM=TOKENPASSWORD
POSTGRES_DB=watchlist
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_HOST=localhost
βNote: For Docker Compose, replace localhost with db in the POSTGRES_HOST
value.
β‘ Run the application
Using Terminal
Run the application directly with Go:
go run ./cmd/watchlist
Available Flags
-p
, --port
: Port number for the API server (default: 8001
).
-e
, --env
: Environment setting (local
, dev
, prod
) (default: local
).
-m
, --migrations
: Path to migration files (e.g., file://migrations
).
-s
, --secret
: Secret password for creating JWT tokens (default: secretPass
).
-t
, --telegram
: Secret password for checking verification token (default: secretPass
).
Using Docker Compose
Start the project with Docker Compose:
docker-compose up --build
βNote: Use the --env-file
flag if your .env
file is not in the root directory.
π API Documentation
You can find the Swagger documents and test the API functionality at:
http://localhost:8001/swagger/index.html
Swagger Documentation Update
βNote: Use the port on which your application is running.
π‘οΈ Account Authentication
There are two methods to register or log in to your account:
Using Credentials
- Endpoints:
/auth/register
, /auth/login
- Use this method to register or log in with your username and password.
Via Telegram Bot
- Endpoints:
/auth/register/telegram
, /auth/login/telegram
- The Telegram bot generates a token by signing it with the
APP_TELEGRAM
secret.
- The token contains the
Telegram ID
as an integer in the claims.
- This token is sent in the header with the key Verification.
- The API reads the token, extracts the
Telegram ID
, and generates a random username for the user.
π¨π»βπ» Testing with Postman
Watchlist API uses Postman for automated API testing.
Postman Tests
To run the tests, you need the Postman Collection and Environment files.
These files are located in the tests/postman directory:
- Postman Collection: postman_collection.json - Contains a set of pre-defined API requests for testing various endpoints.
- Postman Environment: postman_environment.json - Provides environment-specific variables such as base URLs and authentication tokens.
Running Tests
- Import Collection and Environment into Postman.
- Select the Environment.
- Run the Collection.
π Monitoring System (Grafana)
Grafana is used to collect and monitor logs using Loki and metrics using Prometheus.
Setting Up Monitoring π§
- Navigate to Grafana at:
http://localhost:3000
- Log in using the credentials (by default:
admin
, admin
).
- Create a new dashboard and select the preset data sources:
- For logs, use Loki.
- For metrics, use Prometheus.
- Set up a query, such as
{compose_service="app"}
, and save the dashboard.
π Watchlist REST API Endpoints
# Image Section
POST /upload
GET /images/:filename
# Auth section
POST /api/v1/auth/register
POST /api/v1/auth/register/telegram
POST /api/v1/auth/login
POST /api/v1/auth/login/telegram
POST /api/v1/auth/refresh
POST /api/v1/auth/logout
GET /api/v1/auth/check-token
# User section
GET /api/v1/user
PUT /api/v1/user
DELETE /api/v1/user
# Films section
GET /api/v1/films
POST /api/v1/films
GET /api/v1/films/:film_id
PUT /api/v1/films/:film_id
DELETE /api/v1/films/:film_id
# Collections section
GET /api/v1/collections
POST /api/v1/collections
GET /api/v1/collections/:collection_id
PUT /api/v1/collections/:collection_id
DELETE /api/v1/collections/:collection_id
# Collection_films section
GET /api/v1/collections/:collection_id/films
POST /api/v1/collections/:collection_id/films
POST /api/v1/collections/:collection_id/films/:film_id
GET /api/v1/collections/:collection_id/films/:film_id
PUT /api/v1/collections/:collection_id/films/:film_id
DELETE /api/v1/collections/:collection_id/films/:film_id
π Database Structure
The database schema is detailed in the schema.dbml file, which you can view in the docs/database folder.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
For any questions or feedback, please contact: