This is simple blog api with golang and gin web framework. The main resources of this API is posts and categories. For authenticating i'm using jwt-go and gorm for database ORM. Installation guide, api endpoints and sample request/response are given below.
1. At first create a database by psql or pgAdmin
2. Then clone repository: git clone https://github.com/abdurraufraihan/golang-blog-api.git
3. Change directory to golang-blog-api: cd golang-blog-api
4. Add database configuration to config/database-config.go
5. Now install dependency: go mod download
6. Run the app: go run server.go
7. The project will now open on 8080 port of your localhost
8. Test with your favorite api client (e.g. postman)
- POST api/v1/auth/signup
- POST api/v1/auth/login
- POST api/v1/auth/token/verify
- POST api/v1/auth/token/refresh
- GET api/v1/posts
- GET api/v1/posts/:id
- POST api/v1/posts
- PUT api/v1/posts/:id
- DELETE api/v1/posts/:id
- GET api/v1/categories
- POST api/v1/categories
- PUT api/v1/categories/:id
- DELETE api/v1/categories/:id
request body:
{
"name": "Raihan",
"email": "abdurraufraihan@gmail.com",
"password": "123465"
}
response body:
{
"id": 1,
"name": "Raihan",
"email": "abdurraufraihan@gmail.com"
}
request body:
{
"email": "abdurraufraihan@gmail.com",
"password": "123465"
}
response body:
{
"access_token": "<access_token>",
"refresh_token": "<refresh_token>"
}
request body:
{
"token": "<access_token>"
}
response body:
{
"is_valid": true
}
request body:
{
"token": "<refresh_token>"
}
response body:
{
"access_token": "<access_token>",
"refresh_token": "<refresh_token>"
}
response body:
{
"totalPost": 74,
"posts": [
{
"id": 74,
"title": "this is title",
"description": "this is description",
"image": "media/images/13-300x300.jpg",
"created_at": "2022-03-05T23:50:17.526683+06:00",
"updated_at": "2022-03-05T23:50:17.526683+06:00",
"category": {
"id": 1,
"name": "category one"
}
} ......
]
}
response body:
{
"id": 74,
"title": "this is title",
"description": "this is description",
"image": "media/images/13-300x300.jpg",
"created_at": "2022-03-05T23:50:17.526683+06:00",
"updated_at": "2022-03-05T23:50:17.526683+06:00",
"category": {
"id": 1,
"name": "category one"
}
}
request body:
{
"title": "this is title",
"description": "this is description",
"image": "<binary file>",
"category": 1
}
response body:
{
"id": 74,
"title": "this is title",
"description": "this is description",
"image": "media/images/13-300x300.jpg",
"created_at": "2022-03-05T23:50:17.526683+06:00",
"updated_at": "2022-03-05T23:50:17.526683+06:00",
"category": {
"id": 1,
"name": "category one"
}
}
request body:
{
"title": "this is title (modified)",
"description": "this is description (modified)",
"image": "<binary file>",
"category": 2
}
response body:
{
"id": 74,
"title": "this is title (modified)",
"description": "this is description (modified)",
"image": "media/images/13-300x300.jpg",
"created_at": "2022-03-05T23:50:17.526683+06:00",
"updated_at": "2022-03-05T23:50:17.526683+06:00",
"category": {
"id": 2,
"name": "category two"
}
}
this will delete post with id 74 and response back a status 204 no content
response body:
[
{
"id": 1,
"name": "category one",
},
{
"id": 2,
"name": "category two",
}, ......
]
request body:
{
"name": "category one"
}
response body:
{
"id": 1,
"name": "category one"
}
request body:
{
"name": "category one (modified)"
}
response body:
{
"id": 1,
"name": "category one (modified)"
}
this will delete category with id 1 and response back a status 204 no content