This is a rest api application built in go (golang) with postgresql database and the net/http package from the standard library. The goal was to build the application without using any external library other than the ones in the standard library. The only external package that was used is the pq (postgres "github.com/lib/pq") driver since there is no way to interact with a database without using an external driver as of this time.
-
Clone the application with
https://github.com/emanuelquerty/bookrestapi.git
-
Use the postgres dump
books_db.sql
to create the database, create the tables and insert some dump data. -
Once the application is cloned and database is created, change the Connection String as per your postgres username, password and database name on line No. 12 in /pkg/db/database.go
-
Before running the application, you need to import the pq (postgres) driver that the database/sql package requires. To do that, just run the following command in your terminal:
go get "github.com/lib/pq"
-
To run the application, please use the following command:
go run main.go
Note: By default the server is listening on port 3000.
URL - *http://localhost:3000/api/v1/books*
Method - GET
URL - *http://localhost:3000/api/v1/books/2*
Method - GET
URL - *http://localhost:3000/api/v1/books*
Method - POST
Body - (content-type = application/json)
{
"title": "Rennaissance",
"published_year": 2022,
"author_id": 2
}
URL - *http://localhost:3000/api/v1/books/2*
Method - PUT
Body - (content-type = application/json)
{
"title": "This is a new updated title",
"published_year": 1840,
"author_id": 2
}
URL - *http://localhost:3000/api/v1/books/2*
Method - DELETE