Go Auth
An Application For Handling Authentication Using Go Lang
For any fiber go application, just clone the repo & rename the application name.
Fiber is an Express.js inspired web framework build on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.
⚡️ Quick start
- Install
docker
, golang-migrate
& swag
- Rename
.env.example
to .env
- Run project by this command:
make docker.run
- Visit
http://localhost:9000
or http://localhost:9000/swagger/
- Stop
make docker.stop
📦 Used packages
🗄 Project structure
/app
Folder with business logic only. This directory doesn't care about what database driver you're using.
/app/controller
folder for functional controller (used in routes)
/app/dto
Data Transfer Objects(DTO) folder for transform data before sent to API clients
/app/model
folder for describe business models and methods of your project
/app/repository
folder for perform database operations for models of your project
/cmd
Main applications for this project.
The directory name for each application should match the name of the executable you want to have (e.g., /cmd/server
/cmd/cron
).
Don't put a lot of code in the application directory. If you think the code can be imported and used in other projects,
then it should live in the /pkg
directory.
/docs
Folder with API Documentation.
This directory contains config files for auto-generated API Docs by Swagger, screenshots
and any other documents related to this project.
/pkg
Folder with project-specific functionality. This directory contains all the project-specific code tailored only for your business use case.
/pkg/config
folder for configuration functions
/pkg/middleware
folder for add middleware (Fiber built-in and yours)
/pkg/route
folder for describe routes of your project
/pkg/validator
folder with validation functions
Folder with platform-level logic. This directory contains all the platform-level logic that will build up the actual project,
like setting up the database, logger instance and storing migrations, seeds(demo data).
/platform/database
folder with database setup functions (by default, PostgreSQL)
/platform/logger
folder with better logger setup functions (by default, Logrus)
/platform/migrations
folder with migration files (used with golang-migrate/migrate tool)
/platform/seeds
folder with demo data for application rapid setup. mostly sql scripts
⚙️ Configuration
# .env
# APP settings:
APP_HOST="0.0.0.0"
APP_PORT=9000
APP_READ_TIMEOUT=30
APP_DEBUG=false
# JWT settings:
JWT_SECRET_KEY="super_secret_here"
JWT_SECRET_KEY_EXPIRE_MINUTES_COUNT=120
# Database settings:
DB_HOST=go-auth-postgres
DB_PORT=5432
DB_USER=dev
DB_PASSWORD=dev
DB_NAME=go-auth
DB_SSL_MODE=disable
DB_DEBUG=true
DB_MAX_OPEN_CONNECTIONS=3
DB_MAX_IDLE_CONNECTIONS=1
DB_MAX_LIFETIME_CONNECTIONS=10
🔨 Docker development
- Install
docker
, docker-compose
& golang-migrate
- Rename
.env.example
to .env
- Start db container
docker-compose up db
- Run migrations
make migrate.up
- Now start api server with hot reloading
make docker.dev
- Visit
http://localhost:9000
or http://localhost:9000/swagger/
🔨 Local Development
-
Install PostgreSQL
golang >= 1.16
golang-migrate
gosec
& swag
-
Rename .env.example
to .env
and fill it with your environment values
-
Migrate db & seed some demo data
make migrate.up
make seed
-
Run project by this command:
make run
-
Visit http://localhost:9000
or http://localhost:9000/swagger/
-
Check Makefile
for more commands
# drop migration
make migrate.down
# force migration to specific version
migrate.force
# run test
make test
...
⚠️ License
MIT © A. Rostami