This is a simple example of a REST API in Go (Golang) that uses version 1.22 of Go itself to create an HTTP server and the GORM ORM to interact with a PostgreSQL database. This API will return a JSON with data about the Brazilian states according to some ID informed in the GET request: localhost:8080/api/{ID} for a specific return or localhost:8080/api/total to get all the information.
List of IDs:
Acre
Alagoas
Amapá
Amazonas
Bahia
Ceará
Distrito Federal
Espírito Santo
Goiás
Maranhão
Mato Grosso
Mato Grosso do Sul
Minas Gerais
Pará
Paraíba
Paraná
Pernambuco
Piauí
Rio de Janeiro
Rio Grande do Norte
Rio Grande do Sul
Rondônia
Roraima
Santa Catarina
São Paulo
Sergipe
Tocantins
Make sure you have Go installed on your machine. You will also need to install the following dependency:
- GORM: Go ORM.
You can set up a PostgreSQL database using Docker Compose. The docker-compose.yml
file is already configured to start two services: PostgreSQL and PgAdmin.
- Make sure you have Docker and Docker Compose installed on your machine.
- Run the following command in the root of the project:
docker-compose up
This command will start the PostgreSQL and PgAdmin services.
Access PgAdmin in your browser using the following address: http://localhost:54321
Use the email [email protected] and password admin to log in. Then, you can add a PostgreSQL server and connect to the database.
During the initialization of the PostgreSQL service, Docker Compose will run an SQL script located at ./migration/docker-database-initial.sql.
This script is responsible for creating the necessary database and table for the application.
- Clone this repository to your local machine.
- Make sure you have the GORM dependency installed.
- Set up the Docker Compose environment as described above.
- Run the server with the following command:
go run main.go
GET /api
: Returns all states in JSON format.GET /api/{id}
: Returns a specific state by ID in JSON format.GET /api/total
: Returns all states in JSON format.
config/
: Contains the HTTP server configuration.controllers/
: Controllers to handle HTTP requests.database/
: Configuration and connection with the database.models/
: Definition of the application data models.routes/
: Definition of API routes.main.go
: Entry point of the application, where routes are loaded and the server is initialized.