A RESTful API for managing a collection of books built with Go, the Fiber web framework, and PostgreSQL.
This project provides a RESTful API to manage books in a PostgreSQL database. It supports CRUD (Create, Read, Update, Delete) operations for book records. The API allows you to add new books, retrieve all books or a specific book by its ID, and delete books from the database.
- Create Book: Add a new book to the database with details such as author, title, and publisher.
- Get All Books: Retrieve a list of all books stored in the database.
- Get Book by ID: Fetch details of a specific book using its unique ID.
- Delete Book: Remove a book from the database using its unique ID.
- POST /api/create_books
- Description: Adds a new book to the database.
- Request Body:
{ "author": "Author Name", "title": "Book Title", "publisher": "Publisher Name" }
- Response:
{ "message": "Book created successfully", "data": { "id": 1, "author": "Author Name", "title": "Book Title", "publisher": "Publisher Name" } }
- GET /api/books
- Description: Retrieves a list of all books from the database.
- Response:
{ "message": "Books fetched successfully", "data": [ { "id": 1, "author": "Author Name", "title": "Book Title", "publisher": "Publisher Name" } ] }
- GET /api/get_books/:id
- Description: Retrieves details of a book by its ID.
- Response:
{ "message": "Book fetched successfully", "data": { "id": 1, "author": "Author Name", "title": "Book Title", "publisher": "Publisher Name" } }
- DELETE /api/delete_book/:id
- Description: Deletes a book from the database using its ID.
- Response:
{ "message": "Book deleted successfully" }
-
Clone the Repository:
git clone https://github.com/yourusername/go-fiber-postgres.git cd go-fiber-postgres
-
Install Dependencies:
go mod tidy
-
Setup Environment Variables
Create a
.env
file in the root directory with the following content:DB_HOST=localhost DB_PORT=5432 DB_USER=your_db_user DB_PASSWORD=your_db_password DB_NAME=your_db_name DB_SSLMODE=disable
-
Run Database Migrations:
Ensure PostgreSQL is running and accessible, then start the application to run the migrations:
go run main.go
-
Start the Server:
go run main.go
The server will start on
http://localhost:8080
. -
Access the API: Use tools like Postman or
curl
to interact with the API endpoints.
To benchmark your PostgreSQL database, you can use tools like pgBench
or SysBench
.
-
Initialize the Database:
pgbench -i -s 10 your_database
-
Run the Benchmark:
pgbench -c 10 -j 2 -T 60 your_database
-
Prepare the Database:
sysbench --db-driver=pgsql --pgsql-host=localhost --pgsql-user=postgres --pgsql-password=your_password --pgsql-db=your_database oltp_read_write prepare
-
Run the Benchmark:
sysbench --db-driver=pgsql --pgsql-host=localhost --pgsql-user=postgres --pgsql-password=your_password --pgsql-db=your_database oltp_read_write --threads=10 --time=60 run
Feel free to fork the repository and submit pull requests. Contributions are welcome!
For questions or feedback, please open an issue on GitHub or contact [email protected].