This is a simple REST API built with Go, Fiber, and GORM for managing a collection of books stored in a PostgreSQL database.
- Create a new book
- Get all books
- Get a book by its ID
- Delete a book by its ID
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/create_books |
Creates a new book. |
| GET | /api/get_all_books |
Retrieves all books. |
| GET | /api/get_book/:id |
Retrieves a single book by its ID. |
| DELETE | /api/delete_book/:id |
Deletes a book by its ID. |
- Go (version 1.15+ recommended)
- PostgreSQL
- Docker (optional, for running Postgres in a container)
-
Clone the repository:
git clone https://github.com/pathakanu/go_postgres_fiber.git cd go_postgres_fiber -
Install dependencies:
go mod tidy
-
Set up environment variables: Create a
.envfile in the root of the project and add the following variables:DB_HOST=localhost DB_PORT=5432 DB_USER=your_postgres_user DB_PASSWORD=your_postgres_password DB_NAME=your_database_name DB_SSLMODE=disable
-
Run the application:
go run main.go
The server will start on
http://localhost:3000. -
Example cURL requests:
-
Create a book:
curl -X POST http://localhost:3000/api/create_books -H "Content-Type: application/json" -d '{"title":"The Go Programming Language","author":"Alan A. A. Donovan","year":2015}'
-
Get all books:
curl http://localhost:3000/api/get_all_books
-
Get a book by ID:
curl http://localhost:3000/api/get_book/1
-
Delete a book by ID:
curl -X DELETE http://localhost:3000/api/delete_book/1
-