A production-ready backend template built with Go that provides Role-Based Access Control (RBAC) authentication and seamless Single Page Application (SPA) hosting. Perfect for building modern web applications with a robust API backend and embedded frontend.
- π Role-Based Access Control (RBAC) - Complete authentication and authorization system
- π Single Page Application Support - Embedded frontend serving with client-side routing
- π‘οΈ Production-Ready Security - JWT authentication, rate limiting, CORS, secure cookies
- ποΈ SQLite Database - Embedded database with connection pooling and auto-migrations
- π File Storage - Image upload with automatic WebP conversion and resizing
- π― Clean Architecture - Modular, scalable codebase with dependency injection
- β‘ High Performance - Built with Chi router and optimized for speed
- π§ Easy Configuration - Environment-based configuration management
backend/
βββ cli/ # Command line interface
βββ frontend/ # Embedded frontend (SPA support)
βββ internal/
β βββ app/ # Application singleton and HTTP server
β βββ config/ # Configuration management
β βββ db/ # Database connection and initialization
β βββ infrastructure/ # Core infrastructure (permissions, tokens, storage)
β βββ middleware/ # HTTP middleware (auth, CORS, rate limiting)
β βββ module/ # Feature modules (session, health, static, storage)
β βββ utils/ # Utilities (validation, cryptography, file handling)
βββ .env.example # Environment variables template
βββ go.mod # Go module dependencies
- Go 1.24.3 or higher
The application automatically creates an admin user on first run:
- Email:
root@admin.dev - Password:
admin123
Create a .env file in the project root:
# Server
PORT=8723
# JWT Configuration (minimum 32 characters)
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
# Token Durations (seconds)
ACCESS_TOKEN_DURATION=900 # 15 minutes
REFRESH_TOKEN_DURATION=604800 # 7 daysThe template supports embedded frontends:
- Build your SPA and place the files in
frontend/dist/ - The application will automatically:
- Serve static assets (
/assets/*) - Handle client-side routing (catch-all for SPA routes)
- Serve
index.htmlfor non-API routes
- Serve static assets (
- Login - POST
/internal/sessionwith credentials - Receive Tokens - Get JWT access token and refresh cookie
- API Access - Include JWT in
Authorization: Bearer <token>header - Token Refresh - Automatic refresh using HTTP-only cookie
- Logout - DELETE
/internal/sessionto invalidate tokens
Database Schema:
users- User accounts with role associationsroles- Role definitions (Admin, User, etc.)permissions- Granular permissions (user:create,post:delete, etc.)role_permissions- Many-to-many mapping between roles and permissions
Permission Format: resource:action (e.g., user:list, post:create, admin:dashboard)
GET /- SPA fallback (servesindex.html)GET /assets/*- Static assetsGET /uploads/avatar/{imageId}- Public image serving
POST /internal/session- User loginPOST /internal/session/refresh- Refresh access tokenDELETE /internal/session- User logout
GET /internal/health- Application health check
/uploads/- File upload endpoints
- JWT Authentication - Secure token-based authentication
- Role-Based Authorization - Granular permission system
- Rate Limiting - IP-based throttling (100 requests/minute)
- CORS Configuration - Configured for development
- Secure Cookies - HTTP-only, secure refresh tokens
- Password Hashing - bcrypt for secure password storage
- Input Validation - Comprehensive request validation
- Automatic WebP Conversion - Reduces file size without quality loss
- Image Resizing - Configurable dimensions
- File Validation - Type and size restrictions
- Secure Storage - Organized upload directory structure
- Input: JPEG, PNG, WebP, GIF
- Output: Optimized WebP format
- Max Size: Configurable (default 10MB)
# Development build
go run .
# Production build
go build -o backend .
./backendThe application uses SQLite with:
- Auto-migrations on startup
- Connection pooling (25 max, 5 idle)
- Seed data (default roles and admin user)
- Soft delete support for user records
- Singleton Pattern for core services (app, db, config, permissions)
- Dependency Injection for clean architecture
- Middleware Stack for cross-cutting concerns
- Modular Design for feature isolation
curl -X POST http://localhost:8723/internal/session \
-H "Content-Type: application/json" \
-d '{
"email": "root@admin.dev",
"password": "admin123"
}'curl -X GET http://localhost:8723/internal/health \
-H "Authorization: Bearer <your-jwt-token>"curl -X POST http://localhost:8723/uploads/avatar \
-H "Authorization: Bearer <your-jwt-token>" \
-F "file=@profile.jpg"# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific package tests
go test ./internal/module/sessionKey Go modules used:
- chi v1.5.5 - HTTP router
- sqlite v1.14.32 - Database driver
- golang-jwt/jwt v5.3.0 - JWT implementation
- go-playground/validator v10.27.0 - Input validation
- godotenv v1.5.1 - Environment configuration
- golang.org/x/crypto v0.42.0 - Cryptography utilities
Built with β€οΈ and Go!