This project is a basic CRUD (Create, Read, Update, Delete) server for managing a collection of books. It is designed to demonstrate the use of Golang for building a RESTful API, utilizing various libraries and tools to handle routing, database interactions, and environment configurations. The server allows users to perform operations such as adding new books, retrieving details of existing books, updating book information, and deleting books from the database.
- Golang - A statically typed, compiled programming language designed for simplicity and efficiency.
- Gorilla Mux - A powerful URL router and dispatcher for Golang, used for handling incoming HTTP requests and routing them to the appropriate handlers.
- GORM - An Object-Relational Mapping (ORM) library for Golang, which simplifies database interactions by allowing developers to work with database records as if they were regular Go objects.
- MySQL - A widely-used relational database management system, chosen for its reliability and performance.
- Godotenv - A library for loading environment variables from a .env file, making it easier to manage configuration settings for different environments (development, testing, production).
- Clone the repository
git clone https://github.com/haroonalbar/book-store.github
cd book-store
-
Set up MySQL and create a database.
-
Create a .env file in root directory and specify the database URL and port of the application in a .env file.There is a .env.example file for reference.
-
Build the project to get the binary for execution:
go build -o ./bin/book ./cmd/main
- Run the program:
./bin/book
- Get all books:
GET {path}/book/
- Get book by ID:
GET {path}/book/{id}
- Create Book:
POST {path}/book/
{
"name": "Book Name",
"author": "Author Name",
"publication": "Publisher Name"
}
- Delete Book:
DELETE {path}/book/{id}
- Update Book:
PUT {path}/book/{id}
{
"name": "Updated Book Name",
"author": "Updated Author Name",
"publication": "Updated Publisher Name"
}
- cmd/main: Entry point of the application.
- pkg/config: Configuration files and database connection setup.
- pkg/controllers: Handlers for the API endpoints.
- pkg/models: Database models and methods for interacting with the database.
- pkg/routes: Route definitions for the API.
- pkg/utils: Utility functions.
This project is licensed under the MIT License.