Simple REST API for Note-taking App with auth
This is Golang REST API server example including the following features:
- based on Go web framework - Gin (firstly write on go http)
- made with Clean Architecture in mind
- have services that work with PostgreSQL database
- includes controller go tests based on mocks auto-generated with go:generate and mockery
- Postman commands included aswell
- config based on env file with viper
To run app
Setup your local app.env @ ./configs/app.env file and then run:
make run
or
docker compose --env-file .\configs\app.env up --build post-app
To run with gRPC audit logger please run with grpc server @ https://github.com/Arkosh744/grpc-audit-log
TESTs
To run tests:
make test
go test -v -count=1 ./...
REST API:
Examples of usage
First we need to sign-up:
POST /auth/sign-up
{
"name": "NewGopher",
"email": "go@golang.com",
"password": "gog8g!pher"
}
response:
"OK"
Then we need to sign-in:
POST /auth/sign-in
{
"email": "go2g2o2@golang.com",
"password": "aaaaaa"
}
response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjYyNzc4MjcsImlhdCI6MTY2NjE5MTQyNywic3ViIjoiMSJ9.WZeSW-tLPQZBXfhsTza_JLXKR9O2CGAgRh32uNzG1Tg"
}
Then we can create a post:
First, we need add authorization token in headers:
Authorization: Bearer <token>
Then we can create a post:
POST /post
{
"title": "web-develompent",
"body": "THis is my first REST API in GO lang"
}
To get all posts:
GET /post
[
{
"id": 1,
"title": "web-develompent",
"body": "THis is my first REST API in GO lang",
"AuthorId": 1,
"created_at": "2022-10-12T15:21:56.075473Z",
"updated_at": "2022-10-12T15:21:56.075473Z"
},
{
"id": 3,
"title": "web-develompent 3",
"body": "Slice in Go lang",
"AuthorId": 1,
"createdAt": "2022-10-12T15:25:31.069819Z",
"updatedAt": "2022-10-12T15:25:31.069819Z"
}
]
To get post by id:
GET /post/<id>
{
"id": 1,
"title": "web-develompent",
"body": "THis is my first REST API in GO lang",
"AuthorId": 1,
"created_at": "2022-10-12T15:21:56.075473Z",
"updated_at": "2022-10-12T15:21:56.075473Z"
}
To update post by id:
PUT /post
[
{
"id": 1,
"title": "web-develompent UPDATED",
"body": "THIS IS PYTHON"
}
]
response:
{
"message": "updated"
}
Swagger docs
to update need to run command: swag init -g cmd/main.go
Example of usage: http://localhost:8080/swagger/index.html