repositorypackage
0.0.0-20240514045233-ebc20700b9d9
Repository: https://github.com/tiagoc0sta/api-go-gin.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# README
Go: Developing a REST API - Gin and GORM
Acknowledgements
- This is a project of creating a a REST API using Gin and GORM, connect it to a PostgreSQL database using a Docker image.
Authors
Requirements
- Golang
- PostgreSQL
- Gin
- GORM
- Docker
- Postman
Environment Variables
To run this project, you will need to do the following:
- docker-compose up
- go run main.go
Features
- Access from postman
- Create / update / edit / delete an item from a db using the frontend
Demo
Postgre database created via Docker
- http://localhost:54321 = login to pgAdmin - PostgreSQL
Postman - Perform CRUD tests via
View all API data on JSON format
- http://localhost:8080/vehicles - see vehicles (json)
Swagger - Framework for API documentation / testing
- Command to edit swagger: $ swag init --parseDependency --parseInternal --parseDepth 1
- Swagger official documentation: https://github.com/swaggo/gin-swagger
Frontend
Tests
-
Implement automated testing
-
run on terminal: go test
-
Testify official documentation: https://github.com/stretchr/testify
Golang: Website Monitoring
Lessons Learned:
- Understood the importance of structuring the API endpoints and handling HTTP requests and responses efficiently.
Integrating with a Database
-
Learned how to integrate a Go API with a database using Docker.
-
Gained knowledge of using Docker for containerization and database management. Using GORM
-
Learned how to use GORM, Go's popular ORM (Object-Relational Mapping) library, for database operations.
-
Explored features such as model definitions, querying, and relationships management provided by GORM.
Creating Middleware
- Learned how to create middleware in Go to handle cross-cutting concerns such as logging, authentication, and request validation.
- Understood the benefits of using middleware to avoid code duplication and maintain clean API logic.
Integrating with a React Frontend
- Learned how to integrate a Go API with a React frontend for a full-stack application.
- Explored methods for communication between the frontend and backend, such as RESTful APIs and WebSocket.
Installation
-
Open Docker Desktop
-
Run Docker
docker-compose up
- Run API
run main.go
- Open Postman and perform CRUD operations on API. See the reponse on the frontend
Notes:
Gin framework- to create API - Official site: https://github.com/gin-gonic/gin
Gorm ORM
- (Object–relational mapping - avoid the need for queries) used to connect to DB
- site: https://gorm.io/docs/index.html
PostgreSQL
- Store data
Docker - create two images:
- 1 postgres
- 2 pgadmin-compose