Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/araujo88/golang-rest-api-template
Template for REST API made with Golang using Gin framework, PostgreSQL database, JWT authentication, Redis cache and MongoDB for logging
https://github.com/araujo88/golang-rest-api-template
gin-framework gin-gonic go-example go-rest-api go-rest-api-docker go-restful golang
Last synced: about 1 month ago
JSON representation
Template for REST API made with Golang using Gin framework, PostgreSQL database, JWT authentication, Redis cache and MongoDB for logging
- Host: GitHub
- URL: https://github.com/araujo88/golang-rest-api-template
- Owner: araujo88
- License: mit
- Created: 2022-10-10T22:44:48.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T00:52:35.000Z (about 2 months ago)
- Last Synced: 2024-10-01T01:01:29.347Z (about 2 months ago)
- Topics: gin-framework, gin-gonic, go-example, go-rest-api, go-rest-api-docker, go-restful, golang
- Language: Go
- Homepage:
- Size: 55.7 MB
- Stars: 140
- Watchers: 3
- Forks: 27
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# golang-rest-api-template
[![license](https://img.shields.io/badge/license-MIT-green)](https://raw.githubusercontent.com/araujo88/golang-rest-api-template/main/LICENSE)
[![build](https://github.com/araujo88/golang-rest-api-template//actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/araujo88/golang-rest-api-template/actions/workflows/go.yml)## Overview
This repository provides a template for building a RESTful API using Go with features like JWT Authentication, rate limiting, Swagger documentation, and database operations using GORM. The application uses the Gin Gonic web framework and is containerized using Docker.
## Features
- RESTful API endpoints for CRUD operations.
- JWT Authentication.
- Rate Limiting.
- Swagger Documentation.
- PostgreSQL database integration using GORM.
- Redis cache.
- MongoDB for logging storage.
- Dockerized application for easy setup and deployment.## Folder structure
```
golang-rest-api-template/
├── bin
│ └── server
├── cmd
│ └── server
│ └── main.go
├── docker-compose.yml
├── Dockerfile
├── docs
│ ├── docs.go
│ ├── swagger.json
│ └── swagger.yaml
├── go.mod
├── go.sum
├── LICENSE
├── Makefile
├── pkg
│ ├── api
│ │ ├── books.go
│ │ ├── books_test.go
│ │ ├── router.go
│ │ └── user.go
│ ├── auth
│ │ ├── auth.go
│ │ └── auth_test.go
│ ├── cache
│ │ ├── cache.go
│ │ ├── cache_mock.go
│ │ └── cache_test.go
│ ├── database
│ │ ├── db.go
│ │ ├── db_mock.go
│ │ └── db_test.go
│ ├── middleware
│ │ ├── api_key.go
│ │ ├── authenticateJWT.go
│ │ ├── cors.go
│ │ ├── rate_limit.go
│ │ ├── security.go
│ │ └── xss.go
│ └── models
│ ├── book.go
│ └── user.go
├── README.md
├── scripts
│ ├── generate_key
│ └── generate_key.go
└── vendor
```## Getting Started
### Prerequisites
- Go 1.21+
- Docker
- Docker Compose### Installation
1. Clone the repository
```bash
git clone https://github.com/araujo88/golang-rest-api-template
```2. Navigate to the directory
```bash
cd golang-rest-api-template
```3. Build and run the Docker containers
```bash
make setup && make build && make up
```### Environment Variables
You can set the environment variables in the `.env` file. Here are some important variables:
- `POSTGRES_HOST`
- `POSTGRES_DB`
- `POSTGRES_USER`
- `POSTGRES_PASSWORD`
- `POSTGRES_PORT`
- `JWT_SECRET`
- `API_SECRET_KEY`### API Documentation
The API is documented using Swagger and can be accessed at:
```
http://localhost:8001/swagger/index.html
```## Usage
### Endpoints
- `GET /api/v1/books`: Get all books.
- `GET /api/v1/books/:id`: Get a single book by ID.
- `POST /api/v1/books`: Create a new book.
- `PUT /api/v1/books/:id`: Update a book.
- `DELETE /api/v1/books/:id`: Delete a book.
- `POST /api/v1/login`: Login.
- `POST /api/v1/register`: Register a new user.### Authentication
To use authenticated routes, you must include the `Authorization` header with the JWT token.
```bash
curl -H "Authorization: Bearer " http://localhost:8001/api/v1/books
```## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
## TODOs
- Unit tests