This is a Go-based REST API for managing a library, with features including CRUD operations for books, authors, and subscribers, book borrowing/returning functionality, and user authentication.
- CRUD operations: Manage books, authors, and subscribers.
- Book borrowing and returning: Keep track of borrowed books.
- Search functionality: Search for books and authors.
- User authentication: Signup and login for users.
- Image uploads: Support for uploading author photos.
Ensure the following are installed:
- Go (version 1.15 or later)
- MySQL database
- Git (optional, for cloning the repository)
- Docker and Docker Compose (for containerized deployment)
Create a .env
file in the project root with these variables:
DB_HOSTNAME
: Database hostDB_PORT
: Database portDB_NAME
: Database nameDB_USER
: Database userDB_PASSWORD
: Database password
MYSQL_ROOT_PASSWORD
: MySQL root passwordMYSQL_DATABASE
: Name of the MySQL databaseMYSQL_USER
: MySQL userMYSQL_PASSWORD
: MySQL user password
API_URL
: Base URL for the API
These environment variables should replace any hardcoded values within the code, ensuring flexible configuration across development and production environments.
This project uses both standard library and external dependencies. Key external libraries include:
- MySQL driver:
github.com/go-sql-driver/mysql
- Router:
github.com/gorilla/mux
- Environment loader:
github.com/joho/godotenv
To install external dependencies, run the following command:
go get github.com/go-sql-driver/mysql
go get github.com/gorilla/mux
go get github.com/joho/godotenv
Iniziate Go modules:
go mod init <module-name>
go mod tidy
This will create the necessary go.mod
and go.sum
files to manage your dependencies.
The API exposes the following endpoints:
GET /books
: Retrieve all books.GET /books/{id}
: Retrieve a book by ID.POST /books/new
: Add a new book.PUT /books/{id}
: Update a book by ID.DELETE /books/{id}
: Delete a book by ID.
GET /authors
: Retrieve all authors.GET /authors/{id}
: Retrieve books by a specific author.POST /authors/new
: Add a new author.PUT /authors/{id}
: Update an author by ID.DELETE /authors/{id}
: Delete an author by ID.POST /author/photo/{id}
: Upload a photo for an author.
GET /subscribers
: Retrieve all subscribers.GET /subscribers/{id}
: Retrieve a subscriber by book ID.POST /subscribers/new
: Add a new subscriber.PUT /subscribers/{id}
: Update a subscriber by ID.DELETE /subscribers/{id}
: Delete a subscriber by ID.
POST /book/borrow
: Borrow a book.POST /book/return
: Return a borrowed book.
GET /search_books
: Search for books.GET /search_authors
: Search for authors.
POST /signup
: User signup.POST /login
: User login.
Command/Instruction | Description |
---|---|
git clone https://github.com/yourusername/library-management-system.git cd library-management-system |
Clone the repository and into the project directory. |
docker-compose up-all --build |
Build and start the Docker containers. |
docker-compose down |
Stop the running containers. |
docker-compose up-all --build |
Rebuild and start the containers after making changes. |
Open in browser: http://localhost:8081 |
Access the API in your browser. |
docker-compose logs |
View logs of the running containers. |
docker-compose down -v |
Stop and remove containers, volumes, and networks. |