https://github.com/k5sha/gobook
An example of a REST API for a library written in Go using the Gin framework.
https://github.com/k5sha/gobook
books example-project gin go golang gorm rest-api
Last synced: about 2 months ago
JSON representation
An example of a REST API for a library written in Go using the Gin framework.
- Host: GitHub
- URL: https://github.com/k5sha/gobook
- Owner: k5sha
- Created: 2024-11-19T19:05:18.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-26T09:06:19.000Z (over 1 year ago)
- Last Synced: 2025-03-14T11:41:24.232Z (over 1 year ago)
- Topics: books, example-project, gin, go, golang, gorm, rest-api
- Language: Go
- Homepage:
- Size: 267 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README





# GoBook
This project demonstrates how to create a simple but scalable API using Go (Golang), Gin, and GORM.
It includes endpoints for managing workbooks with functions such as adding, searching, updating, and deleting workbook records.
The project also integrates PostgreSQL for data storage and uses the Aconfig package for configuration management.
This guide provides a step-by-step approach to installing and configuring the API via Docker.
### How it works:
- **Gin** is used for handling HTTP requests, routing, and middleware, making the API fast and efficient. It simplifies tasks like validation and error handling.
- **GORM** is used to interact with the PostgreSQL database, allowing seamless CRUD operations with Go structs instead of raw SQL queries, making database management easier and more efficient.
### Startup
- via Docker
```bash
docker compose -f docker-compose.dev.yml up -d
```
- via bash
```bash
go mod download
go run cmd/main.go
```
### Configuration
The application uses a configuration file, **config.yaml**, to define environment-specific settings, such as the server port and database connection details.
Here is an example of the config.local.yaml file:
> [!NOTE]
> If you are using docker to run the database on localhost, you should use `db` instead of `localhost` in the `database_dsn` clause of the config.yaml file
```yaml
port: ':3000'
database_dsn: 'postgres://postgres:password@localhost:5432/books_db?sslmode=disable'
```
#### Explanation:
- `port: ':3000'`
This sets the port on which the API server will run. By default, the server will listen on port 3000.
- `database_dsn: 'postgres://postgres:password@localhost:5432/books_db?sslmode=disable'`
This is the Data Source Name (DSN) for connecting to the PostgreSQL database. It contains the necessary credentials and database information:
- `postgres`: The username for the database.
- `password`: The password for the database.
- `localhost`: The hostname of the database server (in this case, it’s running locally).
- `5432`: The port on which the PostgreSQL database is listening (default is 5432).
- `books_db`: The name of the database to connect to.
- `sslmode=disable`: This disables SSL encryption for the connection, which is commonly used in local development environments.
By editing this file, you can easily change the server's port or adjust the database connection settings without modifying the source code.
### Nice to have features (backlog)
- [ ] Add swagger
- [ ] Add a separate author model to the database
- [ ] Add tests
- [ ] Add comments
- [ ] Add to config the 'release' mode
### Author:
**Yurii (k5sha) Yevtushenko**