Features
This project is developed as a grps microservice for authentication, authorization and permissions.
The project used:
- Standard CRUD operations of a database table
- JWT-based authentication
- Environment dependent application configuration management
- Structured logging with contextual information
- Error handling with proper error response generation
- Database migration
- Data validation
- Containerizing an application in Docker
The kit uses the following Go packages:
This project is a microservice that works in conjunction with a SSO microservice. For full functionality, you need to have two microservices running.
Getting Started
If this is your first time encountering Go, please follow the instructions to install Go on your computer.
The project requires Go 1.21 or above.
Docker is also needed if you want to try the kit without setting up your own database server.
The project requires Docker 17.05 or higher for the multi-stage build support.
Also for simple run commands i use Taskfile.
After installing Go, Docker and TaskFile, run the following commands to start experiencing:
## RUN SSO
# download the project
git clone [https://github.com/neepooha/url_shortener.git](https://github.com/neepooha/sso)
cd sso
# create config.env with that text:
> ./config.env {
CONFIG_PATH=./config/local.yaml
POSTGRES_DB=url
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypass
}
# start a PostgreSQL database server in a Docker container
task db-start
# run the SSO server
go run ./cmd/sso
Also, you can start project in dev mode. For that you need rename in config.env
"CONFIG_PATH=./config/local.yaml" to "CONFIG_PATH=./config/dev.yaml" in both projects
and run following commads:
# run the SSO server
cd sso/
docker compose up --build
SSO-grpc Server running at http://localhost:44044. The server provides the following endpoints:
auth
Regiter: register new user in db
Login: log in to the application
GETUserID: get user ID by name
permissions
SetAdmin: set exists user to admin in your app. You need be creator of app
DelAdmin: delete exists user from admin in your app. You need be creator of app
IsAdmin: is the user an admin by userID
IsCreator: is the user a creator by userID
apps
SetApp: set new app in db. You will be creator of the app
DelApp: delete exists apps. You need be creator of app
UpdApp: update app name and secret
GetAppID: get app id by app name
Project Layout
Project has the following project layout:
sso/
├── cmd/ start of applications of the project
├── config/ configuration files for different environments
├── deployment/ configuration for create daemon in linux
├── internal/ private application and library code
│ ├── app/ application assembly
│ ├── config/ configuration library
│ ├── domain/ models of apps and users
│ ├── grpc/ grpc handlers
│ │ ├── apps/ handlers of apps
│ │ ├── auth/ handlers of auth
│ │ └── permissions/ handlers of permissions
│ ├── lib/ additional functions for logging, error handling, migration
│ ├── services/ logics of handlers
│ │ ├── apps/ handlers of apps
│ │ ├── auth/ handlers of auth
│ │ └── permissions/ handlers of permissions
│ └── storage/ storage library
├── migrations/ migrations
└── config.env config for sercret variables
The top level directories cmd, internal, lib are commonly found in other popular Go projects, as explained in
Standard Go Project Layout.
Within each feature package, code are organized in layers (grpc server, service, db), following the dependency guidelines
as described in the clean architecture.
Updating Database Schema
for simple migration you can use the following commands
# For up migrations
task up
# For drop migrations
task drop
# Revert the last database migration.
tasl rollback
Managing Configurations
The application configuration is represented in internal/config/config.go. When the application starts,
it loads the configuration from a configuration environment as well as environment variables. The path to the configuration environment
should be in the project root folder.
The config directory contains the configuration files named after different environments. For example,
config/local.yml corresponds to the local development environment and is used when running the application
via go run ./cmd/url-shortener
You can keep secrets in local/dev confing, but do not keep secrets in the prud and in the configuration environment.
For set secret variable user github secrets and deploy.yaml.