О проекте Music Service
Relevant source files
The following files were used as context for generating this wiki page:
Music Subscription Service is a backend REST API developed in Go, designed to mimic the core functionalities of music streaming platforms like Spotify or Yandex Music. The system provides a comprehensive set of features for managing music catalogs, user subscriptions, playlists, and listening history.
The project follows a modular architecture where business logic is organized into "features" (Auth, Tracks, Playlists, etc.), supported by a core layer for infrastructure concerns like configuration, database connections, and middleware. It utilizes PostgreSQL for persistent storage, Redis for caching and rate-limiting, and JWT for secure authentication.
Sources: README.md:1-5, cmd/app/main.go:53-128, README.md:173-195
System Architecture
The application is built using a layered architecture with clear separation of concerns. The entry point is located in cmd/app/main.go, which handles dependency injection, configuration loading, and server initialization.
High-Level Components
- Transport Layer: Uses the
chirouter to handle HTTP requests. - Service Layer: Contains business logic and enforces subscription-based limits.
- Repository Layer: Interacts with PostgreSQL using
sqlxfor data persistence. - Core Layer: Provides shared utilities such as logging (zap), configuration, and middleware.
This diagram illustrates the request flow from the client through the middleware and services to the data storage layers.
Sources: cmd/app/main.go:53-150, README.md:173-195
Database Schema
The system uses a relational schema in PostgreSQL to manage the music catalog and user-related data. The schema includes tables for users, artists, albums, tracks, and their relationships.
Entity Relationship Diagram
The diagram depicts the relationships between core entities such as Users, Tracks, and Artists.
Table Definitions
| Table | Description |
|---|---|
users | Stores user credentials, roles (USER/ADMIN), and subscription_type. |
tracks | Contains track metadata and file_url for audio streaming. |
listening_history | Logs every track play with a timestamp for analytics. |
playlist_tracks | A join table for the many-to-many relationship between playlists and tracks. |
Sources: migrations/001_init.up.sql:1-96, README.md:149-158
Subscription & Limits Logic
A key feature of the service is the enforcement of limits based on the user's subscription type (FREE vs PREMIUM).
Limit Enforcement
The system uses Redis to track daily play counts for free users. If a user exceeds the FREE_DAILY_PLAY_LIMIT, the service rejects the play request.
The sequence illustrates the rate-limiting logic applied to free users during track playback.
Configuration Constants
| Variable | Default Value | Description |
|---|---|---|
FREE_DAILY_PLAY_LIMIT | 10 | Max tracks a FREE user can play per day. |
FREE_PLAYLIST_LIMIT | 3 | Max playlists a FREE user can create. |
JWT_ACCESS_TTL | 15m | Lifetime of the access token. |
Sources: internal/features/tracks/service/play.go:37-75, internal/core/config/config.go:42-43, README.md:49-50
Infrastructure and Deployment
The project is containerized using Docker and configured for automated deployment.
- Docker Compose: Manages three main services:
app(Go backend),postgres, andredis. - Healthchecks: The
appservice depends onpostgresandredisbeing in aservice_healthystate. - CI/CD: Configured for Fly.io via GitLab CI/CD, using
FLY_API_TOKENfor automated deployments upon merging to themainbranch.
Sources: docker-compose.yml:5-50, README.md:200-225
Authentication Flow
Authentication is handled via JWT (JSON Web Tokens) with a rotation mechanism for refresh tokens.
- Login: User provides credentials; the server returns an
access_tokenand arefresh_token. - Authorization: The client includes the
access_tokenin theAuthorization: Bearer <token>header. - Middleware: The
AuthMiddlewarevalidates the token and injectsClaims(UserID, Role, SubscriptionType) into the request context. - Token Refresh: When the access token expires, the client uses the
refresh_tokento obtain a new pair, which rotates the old refresh token in the database.
Sources: internal/core/middleware/middleware.go:115-148, internal/features/auth/service/refresh.go:15-68, cmd/app/main.go:121-140
The Music Service provides a robust foundation for a streaming platform, combining strict business rule enforcement with a scalable, containerized architecture.
Sources: README.md:1-5