Skip to content

nixys/nxs-go-appctx-example-restapi

Repository files navigation

nxs-go-appctx-example-restapi

Introduction

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

Quickstart

Requrements

To run the test application make sure you have installed following tools:

Prepare environment

  • 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 test app

  • 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:
      curl -X POST -H "Authorization: Bearer some_auth_token" -d '{"username": "userA"}'  "http://127.0.0.1:8080/v1/user"
      Response:
      {
        "user": {
          "id": 1,
          "username": "userA",
          "password": "ettih20vId4JOMq"
        }
      }
    • List existing accounts:
      curl -X GET -H "Authorization: Bearer some_auth_token" "http://127.0.0.1:8080/v1/user"
      Response:
      {
        "users": [
          {
            "id": 1,
            "username": "userA",
            "password": "ettih20vId4JOMq"
          }
        ]
      }
    • Update account with ID: 1:
      curl -X PATCH -H "Authorization: Bearer some_auth_token" -d '{"password": "fixed password"}'  "http://127.0.0.1:8080/v1/user/1"
      Response:
      {
        "user": {
          "id": 1,
          "username": "userA",
          "password": "fixed password"
        }
      }
    • Get account with ID: 1:
      curl -X GET -H "Authorization: Bearer some_auth_token"  "http://127.0.0.1:8080/v1/user/1"
      Response:
      {
        "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"

Feedback

For support and feedback please contact me:

License

nxs-go-appctx-example-restapi is released under the MIT License.

About

Example of usage `nxs-go-appctx`

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages