https://github.com/polunlin/go-gin-api
go-gin-api
https://github.com/polunlin/go-gin-api
gin-gonic go golang
Last synced: about 1 month ago
JSON representation
go-gin-api
- Host: GitHub
- URL: https://github.com/polunlin/go-gin-api
- Owner: PolunLin
- Created: 2022-07-21T00:53:01.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-21T02:52:58.000Z (almost 4 years ago)
- Last Synced: 2025-03-21T19:19:05.840Z (about 1 year ago)
- Topics: gin-gonic, go, golang
- Language: Go
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go-Gin-Api
| This repo is for go gin api .
## Table of contents
- [Go-Gin-Api](#go-gin-api)
- [Table of contents](#table-of-contents)
- [How to Create](#how-to-create)
- [Repository Struct](#repository-struct)
- [Run the application](#run-the-application)
- [Result](#result)
- [Reference](#reference)
## How to Create
1. Create Database
```bash
psql -U postgres # open psql CLI with user postgres
CREATE DATABASE go-gin-api;
\l # list all databases
\q #CLI exit
```
2. Clone the project
```bash
git clone https://github.com/PolunLin/go-gin-api.git
cd go-gin-api
code .
go mod init github.com/YOUR_USERNAME/go-gin-api
```
3. Install Modules
```
go get github.com/spf13/viper
go get github.com/gin-gonic/gin
go get gorm.io/gorm
go get gorm.io/driver/postgres
```
4. Environment Variable
in ``` common/envs/.env```
```
PORT=:3000
DB_URL=postgres://DB_USER:DB_PASSWORD@DB_HOST:DB_PORT/go-gin-api
```
## Repository Struct
```bash
tree /f
go-gin-api
│ go.mod
│ go.sum
│ Makefile
│ README.MD
│
├─cmd
│ main.go
│
└─pkg
├─books
│ add_book.go
│ controller.go
│ delete_book.go
│ get_book.go
│ get_books.go
│ update_book.go
│
└─common
├─config
│ config.go
│
├─db
│ db.go
│
├─envs
│ .env
│
└─models
book.go
```
## Run the application
```bash
make server # or
go run cmd/main.go
```
## Result
1. POST:add a new Book
```bash
curl --request POST \
--url http://localhost:3000/books/ \
--header 'Content-Type: application/json' \
--data '{
"title": "Book A",
"author": "Author",
"description": "Some cool description"
}'
```
2. GET: Get All Books
```bash
curl --request GET --url http://localhost:3000/books/
```
3. GET: Get Book by ID
```bash
curl --request GET --url http://localhost:3000/books/1/
```
4. PUT: Update Book by ID
```bash
curl --request PUT \
--url http://localhost:3000/books/1/ \
--header 'Content-Type: application/json' \
--data '{
"title": "Updated Book Name",
"author": "Another author",
"description": "Updated description"
}'
```
5. DELETE: Delete Book by ID
```bash
curl --request DELETE --url http://localhost:3000/books/1/
```
## Reference
1. https://betterprogramming.pub/build-a-scalable-api-in-go-with-gin-131af7f780c0
2. https://gin-gonic.com/