This is a sample application for REST service development with golang. Libraries used:
- gorm as ORM library
- viper for configuration management
- gin as web framework
- gin-swagger to generate OpenAPI spec from go comments
- resty for REST client implementation (for example to talk to OPA)
- Open Policy Agent (OPA) for authorization decisions
To build
docker build -t todo .
Docker-compose starts the built container with a database
docker-compose up
Go to Swagger UI http://localhost:8080/swagger/index.html
docker-compose down --volumes
docker-compose up db
opa build authz -o authz/bundle.tar.gz --ignore 'taskservice_authz_test.rego'
docker-compose up bundle_server
docker-compose up opa
To access the database:
$ docker exec -it todo_db_1 /bin/bash
root@187961c81d2e:/# psql -U postgres
psql (14.2 (Debian 14.2-1.pgdg110+1))
Type "help" for help.
go run main.go
Go to Swagger UI http://localhost:8080/swagger/index.html
go test ./...
Run unit tests
opa test authz -v --ignore '*.tar.gz'
Test rules on the server
echo "{\"input\": {\"method\":\"POST\",\"owner\":\"johndoe\",\"path\":[\"tasks\"],\"user\":\"johndoe\"}}" \
| http -v POST http://127.0.0.1:8181/v1/data/authz
swag init
For more see: https://github.com/swaggo/gin-swagger