Create a Go project using the Fiber web framework. The project will begin as a monolith but should be structured in a modular way to allow for future microservices separation.The project will have two main service modules:1. Auth Service - User registration, login, logout - JWT token generation and validation - Role-based access (viewer, creator, admin) - User profile and settings2. Content Management Service - Upload video via pre-signed URL (S3-compatible) - Edit/delete video metadata - Attach product tags from external eCommerce - Trigger video transcoding jobs - Provide creator dashboard APIsProject Structure:- Use a cmd/main.go entry point- Each module (auth, content) should be under internal/<module-name>/ and include: - handler (HTTP handler layer using Fiber) - service (business logic) - repository (interface for data access) - model (request/response and entity structs)- Create a pkg/ folder for shared utilities: - config (load env or YAML config) - jwt (for generating and validating tokens) - logger (standard structured logging)Add example routes:- POST /auth/register- POST /auth/login- GET /auth/me- POST /videos/upload- GET /videos- PATCH /videos/:idUse dependency injection (e.g. pass service layer to handler). Use Go modules. Structure it cleanly to allow future extraction into standalone microservices. Include reasonable stubs or dummy implementations for services and repositories.
Of course. Here is a well-structured Go project using the Fiber framework, designed as a modular monolith that can be easily broken down into microservices in the future.
This project includes two main modules, Auth and Content Management, with dependency injection, shared utility packages, and dummy implementations for a complete, runnable example.
Install dependencies: Open your terminal in the project's root directory and run:
go mod tidyRun the application:
go run cmd/main.goThe server will start on http://localhost:8080.
You can use a tool like curl or Postman to interact with the API.
Register a new user:
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{"email": "creator@example.com", "password": "password123"}'(Note: The default role is viewer. You would need an admin endpoint to change roles.)
Login:
curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "creator@example.com", "password": "password123"}'This will return a JWT token.
Get User Profile:
# Replace YOUR_JWT_TOKEN with the token from the login step
curl http://localhost:8080/api/v1/auth/me \
-H "Authorization: Bearer YOUR_JWT_TOKEN"creator or admin role):
Since the default role is viewer, this will currently fail with a 403 Forbidden error. This demonstrates the role-based access control. To make it work, you would need to manually change the user's role in the in-memory repository or implement an admin feature to do so.This modular structure makes it straightforward to extract services:
Extracting the Auth Service:
internal/auth and pkg directories into it.main.go in the new project would only initialize and run the auth handlers.internal/auth code and replace calls to authService with HTTP client calls to the new Auth microservice.pkg directory can be published as a private Go module that both microservices can import to share common logic like JWT validation and configuration loading.Create a Go Fiber module for handling video uploads in a scalable backend system. The module should follow clean architecture and be structured for future migration to a microservice.### Project Context:- Use Go (Golang) with the Fiber web framework.- This is part of a larger modular monolith.- The module should be placed in internal/content/ with subfolders for handler, service, repository, and model.### Functionality to implement:1. Upload Video - Endpoint: POST /videos/upload-url - Accepts metadata (title, description, fileName, contentType) - Responds with a pre-signed S3-compatible URL for direct upload (use stub/mock implementation of pre-signed URL generation) - Validate: - File extensions: .mp4, .mov, .webm - Max size: 2GB - Max duration: 60 minutes (assume frontend will limit this)2. Trigger Transcoding Job - After successful upload, call TriggerTranscode(videoID string) function - Simulate transcoding by printing a log (stub/mock implementation) - Transcoded format: 720p MP4 - Save updated video status (e.g., processing, completed)3. Get Video List - Endpoint: GET /videos - Returns list of videos with metadata and transcoding status4. Delete Video - Endpoint: DELETE /videos/:id - Deletes the record and optionally removes the file (stub this part)### File Structure:- internal/content/handler/video_handler.go → Fiber route handlers- internal/content/service/video_service.go → Core logic (e.g., validation, transcode trigger)- internal/content/repository/video_repository.go → In-memory or dummy storage (map or slice)- internal/content/model/video.go → Structs for video request/response### Extras:- Use UUIDs for video IDs- Use Go’s standard logging or structured logger in pkg/logger- Use a central config file or pkg/config- Return proper HTTP responses with status codesMake sure the module is loosely coupled, easily testable, and structured for future separation into a microservice.
Of course. Here is a complete, self-contained Go module for handling video uploads using Fiber.
This solution follows clean architecture principles, with distinct layers for handling, business logic, and data access. It's structured to be easily tested and migrated to a standalone microservice in the future.
go mod tidygo run cmd/main.goRequest an upload URL:
curl -X POST http://localhost:8080/api/v1/videos/upload-url \
-H "Content-Type: application/json" \
-d '{
"title": "My First Awesome Video",
"description": "A short clip about Go and Fiber.",
"fileName": "intro.mp4",
"fileSize": 50000000,
"contentType": "video/mp4"
}'Response:
{
"videoId": "...",
"uploadUrl": "https://..."
}(Simulate) Upload completion: After the client uploads the file to the uploadUrl, it notifies the backend.
# Replace {videoId} with the ID from the previous step
curl -X POST http://localhost:8080/api/v1/videos/{videoId}/upload-completeThe server will log that transcoding has started.
Check video status:
curl http://localhost:8080/api/v1/videosInitially, the status will be processing. After 10 seconds, it will change to completed.
Delete the video:
# Replace {videoId} with a valid ID
curl -X DELETE http://localhost:8080/api/v1/videos/{videoId}