This is a Golang project designed to handle a simple web service with user management, roles, permissions, and refresh tokens. It uses MySQL for the database, Docker for containerization, and includes support for migrations, seeding, and authentication.
The project follows a clean architecture and is organized into the following directories:
├── Dockerfile # Docker configuration for the application
├── README.md # Project documentation
├── cmd # Command-line interfaces (CLI)
│ ├── seeder # Seeder for initial data population
│ │ └── seeder.go
│ └── server # Main entry point for the web server
│ └── main.go
├── docker-compose.yml # Docker Compose configuration for the app and MySQL
├── docs # API documentation
│ ├── swagger.json # OpenAPI 3.0 specification
│ ├── swagger.html # Swagger UI documentation
│ └── LOGIN_FLOW.md # Login flow documentation
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── internal # Core application logic
│ ├── configs # Configuration files for database, environment variables, JWT, etc.
│ ├── constants # Constants and error handling
│ ├── database # Database migrations and seeding
│ ├── dto # Data transfer objects for request and response
│ ├── handlers # HTTP request handlers
│ ├── middlewares # Middlewares for authentication and logging
│ ├── models # Data models for the application
│ ├── repositories # Repositories for database access
│ ├── routes # Routes and routing logic
│ ├── services # Business logic for authentication, user, etc.
│ └── utils # Utility functions (e.g., for encryption, validation)
├── pkg # External packages
│ ├── apperror # Custom application errors
│ ├── logger # Logger utility
│ ├── mailer # Mailer for sending emails
│ └── migrator # Database migration utility
├── tests # Unit and integration tests
│ ├── e2e # End-to-end tests
│ └── mocks # Mocks for internal package tests
Before getting started, ensure that you have the following installed:
- Go (Go 1.23 or later)
- Docker
- Docker Compose
- MySQL
- Makefile (Usually pre-installed on macOS and Linux)
Before doing anything else, install all required development tools using the following command:
# Install all required tools
make install-toolsThis will install:
- Migrate CLI (for database migrations)
- Air (for live reloading)
- gotestsum (for running tests)
- golangci-lint (for linting)
git clone https://github.com/yourusername/yourproject.git
cd yourproject
cp .env.example .envYou can use Docker Compose to set up both the app and the MySQL database:
docker-compose up --buildThis will:
- Build the Docker images.
- Start a MySQL container.
- Start the application container.
To create a new migration file, use the following command:
migrate create -ext sql -dir internal/database/migrations -seq your_migration_nameFor example, to create a feedback table migration:
migrate create -ext sql -dir internal/database/migrations -seq feedback_tableThis will create two files:
- XXXXXX_feedback_table.up.sql (for applying the migration)
- XXXXXX_feedback_table.down.sql (for reverting the migration)
The project includes migrations for creating the necessary tables in the MySQL database. To apply the migrations:
make migrateOr manually:
migrate -path ./internal/database/migrations -database "mysql://root:root@tcp(127.0.0.1:3306)/golang_db_2" upTo revert migrations, you can use the down command:
migrate -path ./internal/database/migrations -database "mysql://root:root@tcp(127.0.0.1:3306)/golang_db_2" downYou can also revert a specific number of migrations by adding the number after the down command:
migrate -path ./internal/database/migrations -database "mysql://root:root@tcp(127.0.0.1:3306)/golang_db_2" down 1To seed the database with initial data (e.g., default users, roles, permissions), run:
make start-seederThe easiest way to run the server is using the provided make command:
make start-serverThis command will:
- Install required tools (if not already installed)
- Start Docker containers in detached mode
- Start the server with Air for live reloading
Alternatively, you can run the server in other ways:
Air provides live-reloading capability which is great for development:
airIf you prefer to run the server directly without live-reloading:
go run cmd/server/main.goThe server will start and be available at http://localhost:3000.
PHPMyAdmin is available for database management through a web interface at:
- URL:
http://localhost:8080 - Username:
root - Password:
root
The following environment variables are required for the application:
Database Configuration:
DB_HOST- MySQL database hostDB_PORT- MySQL port numberDB_USERNAME- MySQL database usernameDB_PASSWORD- MySQL database passwordDB_DATABASE- MySQL database name
Server Configuration:
PORT- Port number for the application server (default: 3000)GIN_MODE- Gin mode ("debug" or "release")RUN_MIGRATE- Whether to run migrations on startupSTAGE- Environment stage ("local", "dev", "prod")
JWT Configuration:
JWT_KEY- Secret key for JWT token generation
URL Configuration:
FRONTEND_URL- URL of the frontend application
Mail Configuration:
MAIL_HOST- SMTP server hostMAIL_PORT- SMTP server portMAIL_USERNAME- SMTP usernameMAIL_PASSWORD- SMTP passwordMAIL_FROM- Email address used as sender
These can be set in the .env file or passed directly as environment variables. A sample .env.example file is provided in the repository.
The API is documented using OpenAPI 3.0 specification. You can access the documentation through:
- Swagger UI:
http://localhost:8080/swaggerorhttp://localhost:8080/api-docs - OpenAPI JSON:
http://localhost:8080/docs/swagger.json - Login Flow: See
docs/LOGIN_FLOW.mdfor detailed authentication flow
POST /api/v1/login- User loginPOST /api/v1/refresh-token- Refresh access tokenPOST /api/v1/forgot-password- Request password resetPOST /api/v1/reset-password- Reset password with token
GET /api/v1/profile- Get user profilePATCH /api/v1/profile- Update user profilePOST /api/v1/change-password- Change user password
GET /api/v1/users- List users (admin only)POST /api/v1/users- Create user (admin only)GET /api/v1/users/{id}- Get user by IDPATCH /api/v1/users/{id}- Update user (admin only)DELETE /api/v1/users/{id}- Delete user (admin only)
GET /healthz- Health status
To install required testing tools and run tests with coverage report generation:
make test-coverageThis command will:
- Install required tools (gotestsum, gocov, gocov-html) if not already installed
- Run all tests and generate coverage.out
- Generate a coverage summary at coverage-summary.txt
- Generate an HTML coverage report at coverage.html
For specific tests, you can still use:
go test -v path/to/testmake test: Run all unit tests using gotestsummake test-e2e: Run end-to-end testsmake watch-test: Watch for changes and run tests automatically
The test files are located under the tests directory. The tests follow the Go testing conventions.
make lint: Run linter (golangci-lint)make fmt: Format codemake vet: Run go vetmake pre-push: Run all checks (fmt, vet, lint, test) before pushingmake docker-up: Start Docker containersmake dev: Start server with Air (requires DB to be running)
- Fork the repository.
- Create a feature branch (
git checkout -b feature/feature-name). - Commit your changes (
git commit -am 'Add feature'). - Push to the branch (
git push origin feature/feature-name). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.