This article documents in detail the construction of a complete music player backend service, covering the full implementation of core technologies such as Gin framework setup, JWT authentication, Alibaba Cloud OSS object storage, audio format conversion, scheduled task scheduling, and database design.
Table of Contents
- Project Overview and Architecture Design
- Technology Stack Selection and Project Structure
- Configuration Management and Environment Isolation
- Database Design and Model Relationships
- JWT Authentication and Access Control
- Alibaba Cloud OSS Integration and File Storage
- Audio Format Conversion Service
- Scheduled Tasks and Data Deduplication
- Deployment and Operations in Practice
- Summary and Outlook

1. Project Overview and Architecture Design
1.1 Project Background
Melody is the backend service of a local music player, and it needs to support the following core features:
- User System: registration, login, JWT authentication, personal profile management
- Song Management: CRUD operations, metadata extraction, audio format conversion
- Albums and Artists: complete music metadata management
- Playlist System: user-defined playlists, favorites
- Play Statistics: recently played, play count statistics
- File Storage: Alibaba Cloud OSS object storage, signed URL downloads
1.2 Overall Architecture
|
1.3 Core Features
| Feature | Technical Implementation | Notes |
|---|---|---|
| Auth & Authorization | JWT + bcrypt | Stateless auth, encrypted password storage |
| File Storage | Alibaba Cloud OSS | Supports both server-side proxy and client-side direct upload |
| Audio Processing | ffmpeg | Supports mutual conversion between MP3/FLAC/WAV/AAC and more |
| Scheduled Tasks | robfig/cron | Automatically deduplicates music files daily |
| Configuration Management | Viper | Supports YAML config files and environment variables |
2. Technology Stack Selection and Project Structure
2.1 Core Technology Stack
|
2.2 Project Structure
|
3. Configuration Management and Environment Isolation
3.1 Configuration Structure Design
Using Viper to implement flexible configuration management, supporting both YAML files and environment variables:
|
3.2 Configuration Loading Logic
|
3.3 Production Environment Configuration Recommendations
|
4. Database Design and Model Relationships
4.1 ER Diagram Relationships
|
4.2 Core Model Definitions
|
4.3 Database Connection Pool Optimization
|
5. JWT Authentication and Access Control
5.1 JWT Token Generation and Verification
|
5.2 Route Permission Grouping
|
6. Alibaba Cloud OSS Integration and File Storage
6.1 OSS Client Wrapper
|
6.2 Two Upload Modes
Mode 1: Server-side proxy upload (suitable for small files)
|
Mode 2: Client-side direct upload (recommended, reduces server load)
|
7. Audio Format Conversion Service
7.1 Audio Conversion Based on ffmpeg
|
7.2 Supported Audio Formats
| Format | Encoder | Use Case |
|---|---|---|
| MP3 | libmp3lame | Universal compatibility, lossy compression |
| FLAC | flac | Lossless quality, larger files |
| WAV | pcm_s16le | Raw audio, uncompressed |
| AAC | aac | Preferred for Apple devices |
| OGG | libvorbis | Open source, royalty-free |
| Opus | libopus | Low-latency voice |
8. Scheduled Tasks and Data Deduplication
8.1 Scheduled Task Scheduler
|
8.2 Deduplication Service Implementation
|
9. Deployment and Operations in Practice
9.1 systemd Service Configuration
|
9.2 Nginx Reverse Proxy Configuration
|
9.3 Deployment Script
|
10. Summary and Outlook
10.1 Project Highlights
- Clear architecture layering: handler → service → model three-tier architecture with clear responsibilities
- Flexible configuration management: Viper supports config files + environment variables, making deployment across environments easy
- Secure file storage: Alibaba Cloud OSS + signed URLs to keep resources safe
- Complete audio processing: ffmpeg-based support for mutual conversion between multiple formats
- Automated operations: scheduled deduplication tasks reduce manual intervention
10.2 Directions for Further Optimization
- Cache optimization: integrate Redis to cache hot data (song lists, user info)
- Search functionality: connect Elasticsearch for full-text search
- Message queue: use RabbitMQ to handle asynchronous tasks (audio conversion, data import)
- Rate limiting protection: implement API rate limiting based on the token bucket algorithm
- Monitoring and alerting: integrate Prometheus + Grafana to monitor service status
References
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿文的博客!
评论
0 条评论