The application represents an API server based on nxs-go-appctx with implemented CRUD operations for manage user accounts stored in MySQL.
This project applies the following elements:
- nxs-go-appctx library helps bring together all the components for create a complete REST API server
- Project structure you may use for it (described in nxs-go-appctx documentation)
- GORM library to work with databases (MySQL in this case)
- Gin Web Framework to create a REST API
- golang-migrate to write the database migrations
To run the test application make sure you have installed following tools:
- Go to application directory
- Start MySQL Database via Docker Compose:
docker compose -f .env/docker-compose.yml up -d
- Run database migration:
migrate -path migrations/ -database mysql://user:somepass@tcp\(127.0.0.1:3306\)/db?multiStatements=true up
- Run API server with following command executed within the application directory:
go run main.go
- To test application make a following requests to API:
- Create new user account:
Response:
curl -X POST -H "Authorization: Bearer some_auth_token" -d '{"username": "userA"}' "http://127.0.0.1:8080/v1/user"
{ "user": { "id": 1, "username": "userA", "password": "ettih20vId4JOMq" } }
- List existing accounts:
Response:
curl -X GET -H "Authorization: Bearer some_auth_token" "http://127.0.0.1:8080/v1/user"
{ "users": [ { "id": 1, "username": "userA", "password": "ettih20vId4JOMq" } ] }
- Update account with ID: 1:
Response:
curl -X PATCH -H "Authorization: Bearer some_auth_token" -d '{"password": "fixed password"}' "http://127.0.0.1:8080/v1/user/1"
{ "user": { "id": 1, "username": "userA", "password": "fixed password" } }
- Get account with ID: 1:
Response:
curl -X GET -H "Authorization: Bearer some_auth_token" "http://127.0.0.1:8080/v1/user/1"
{ "user": { "id": 1, "username": "userA", "password": "fixed password" } }
- Delete account with ID: 1:
curl -X DELETE -H "Authorization: Bearer some_auth_token" "http://127.0.0.1:8080/v1/user/1"
- Create new user account:
For support and feedback please contact me:
- GitHub issues
- telegram: @borisershov
- e-mail: b.ershov@nixys.ru
nxs-go-appctx-example-restapi is released under the MIT License.