Book Store Golang Project
This is a Golang project for a book store, designed for learning purposes. The project implements basic CRUD (Create, Read, Update, Delete) operations for managing books.
Features
Prerequisites
Before you begin, ensure you have met the following requirements:
- Golang installed on your system
- PostgreSQL database server installed and running
- Goose CLI tool installed (for database migrations)
- SQLC CLI tool installed (for generating database queries)
- Git installed on your system
Installation
To install and run this project, follow these steps:
-
Clone the repository:
git clone https://www.github.com/Hayoun01/book_store_api.git
-
Navigate to the project directory:
cd book_store_api
-
Install project dependencies:
go mod tidy
-
Set up the PostgreSQL database:
- Create a new database named
book_store
.
- GO TO
-
Configure environment variables:
- Copy the .env.example file to .env.
- Update the .env file with your PostgreSQL database credentials and app Port.
-
Run the application:
go run ./cmd/main/
The application should now be running. Access the API at http://localhost:8080.
Usage
Once the application is running, you can interact with the API endpoints using tools like cURL, Postman, or a web browser:
- GET
/books
: Retrieve a list of all books.
- POST
/create_book
: Add a new book to the store (authentication required).
- GET
/books/{book_id}
: Get details of a specific book by its ID.
- DELETE
/books/{book_id}
: Delete a book from the store (authentication required).
- POST
/create_author
: Create a new author (authentication required).
- GET
/authors
: Retrieve a list of all authors.
- POST
/users
: Create a new user (no authentication required).
- GET
/ready
: Check if the server is ready (no authentication required).
Project Hierarchy
The project follows the following directory structure:
cmd/main
: Main entry point of the application.
pkg/auth
: Contains authentication-related code.
pkg/config
: Configuration settings for the application.
pkg/controllers
: Controllers for handling HTTP requests.
pkg/db
: Database-related code, including migrations, queries, and SQLC generated code.
pkg/models
: Data models used by the application.
pkg/routes
: Route definitions for the API endpoints.
pkg/utils
: Utility functions used throughout the application.
Database Migrations
Database migrations are managed using Goose. Use the following Makefile commands to manage migrations:
# Apply pending migrations.
make migrateup
# Rollback the last migration.
make migratedown
SQLC Queries
SQLC is used to generate type-safe database queries. Use the following Makefile command to generate SQLC queries:
# Generate SQLC queries.
make sqlc
Contributing
Contributions are welcome! Here's how you can contribute to this project:
- Fork the repository.
- Create a new branch for your feature or bug fix: git checkout -b feature-name.
- Make your changes and commit them: git commit -m 'Add new feature'.
- Push to your fork: git push origin feature-name.
- Create a pull request on the original repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to customize this template according to your project's specific requirements and structure.