This project aims to create a robust REST API using the Go programming language (Golang) and the gin framework. It takes advantage of several Go libraries to handle different tasks:
Gin: This HTTP web framework is used for API routing. Gin, optimized for speed, offers an elegant and simple interface for creating routes to the REST API and includes useful built-in features such as JSON validation and error handling. Also, the project implements Swagger documentation using the github.com/swaggo/gin-swagger library, making it easier to understand and use the API.
Tests: Go's native testing library, in conjunction with the github.com/stretchr/testify/assert library, is used to write tests for the API. The assert library provides a set of useful assertion functions that can help make tests more readable and easier to write.
Database: Gorm, an Object-Relational Mapper (ORM) in Go, is used for database interactions. Gorm provides a simple interface for creating, retrieving, updating, and deleting records from a database. It also offers advanced features like automatic transactions and migrations.
Validation: The gopkg.in/validator.v2 library is used to validate the data before sending it to the database. This helps ensure that the data is in the correct format and meets any necessary requirements before it is stored.
With the combination of these libraries, this project provides a solid foundation for creating Go REST APIs that are easy to develop and test. It demonstrates best practices for structuring a Go application, including separation of responsibilities between routing, request handling, database interactions, and data validation. Furthermore, the integration of Swagger allows for effective documentation and easier usage of the API endpoints.
Create an .env file in the root of the project and define your database user and password in it and place the file in db.go and docker-compose.yml For example:
# .env
DATABASE_USER=use
DATABASE_PASSWORD=password
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=db
STRING_CONNECTDB="host=localhost..."
# use in docker-compose.yml
POSTGRES_USER=${POSTGRES_USER}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
POSTGRES_DB=${POSTGRES_DB}
PGADMIN_DEFAULT_EMAIL: ${DATABASE_USER}
PGADMIN_DEFAULT_PASSWORD: ${DATABASE_PASSWORD}
# install lib godotenv and use in db.go
stringConnect := os.Getenv("STRING_CONNECTDB")
To use it, you need to clone the repository, install the dependencies and run the project.
# Open terminal/cmd and then Clone this repository
$ git clone https://github.com/JuanCampbsi/api_go_rest.git
# Access project folder in terminal/cmd
$ cd api_go_rest
# Install the dependencies
$ go mod tidy
# Run the application in development mode
$ go run main.go
# Run test all
$ go test
# Run test one
$ go test -run TestVerifyStatusCodeSearchStudentsWithParams
# In order to create a container for this application, you'll need to run Docker command.
#Please ensure that Docker is already installed on your machine before proceeding.
#If you don't have Docker installed, you can download it from the
#[official website](https://www.docker.com/products/docker-desktop).
$ docker compose up
- Golang - version 1.20
- GIN - version 1.9.0
- Validator v2 - version 2.0.1
- Testify - version 1.8.3
- GORM - version 1.25.1
- Driver PostgreSQL - version 1.5.2
- Godotenv - version 1.5.1
👨💻 Author 💻
Developed by Juan Campos