Giter Site home page Giter Site logo

yhagio / go_api_boilerplate Goto Github PK

View Code? Open in Web Editor NEW
144.0 4.0 34.0 461 KB

🐶Go (Golang)🚀REST / GraphQL API + Postgres boilerplate

Home Page: https://go-gin-postgres-api.herokuapp.com/graphql

License: MIT License

Go 99.05% Dockerfile 0.46% Shell 0.49%
golang go gin-gonic gin rest-api gorm postgresql docker testify gqlgen graphql swagger jwt

go_api_boilerplate's Introduction

Build Status codecov MIT Licence Open Source Love

Go (Golang) REST / GraphQL API Boilerplate

Note: in demo, I disabled email

Used libraries:


  • Components Diagram

diagram image

  • See Swagger Doc http://localhost:3000/swagger/index.html

swagger image

  • See GraphQL Playground http://localhost:3000/graphql

graphql image


Features

  • User Auth functionality (Signup, Login, Forgot Password, Reset Password)
  • JWT Authentication
  • REST API
  • GraphQL API
  • Gorm (Golang SQL DB ORM) with Postgres implementation and auto migration
  • Configs via environmental variables
  • Email notification (Welcome email, Reset password email)
  • Swagger REST API documentation
  • GraphQL playground
  • Unit tests
  • Dependency injection

Run locally

Create .env at root, i.e.

MAILGUN_API_KEY=key-b9jksfh8s9843uhfsdhds
MAILGUN_DOMAIN=xxxxx.mailgun.org

EMAIL_FROM=support@go_api_boilerplate.com

DB_HOST=localhost
DB_PORT=5432
DB_USER=your-user
DB_PASSWORD=your-password
DB_NAME=local-dev-db

JWT_SIGN_KEY=secret
HAMC_KEY=secret
PEPPER=secret

ENV=development

APP_PORT=3000
APP_HOST=http://localhost

Run

# Terminal 1
docker-compose up        # docker-compose up (Run postgres)
docker-compose down      # docker-compose down (Shutdown postgres)

# Terminal 2
go run github.com/99designs/gqlgen -v # Generate Graphql stuff
swag init -g app/app.go               # Generates Swagger
go run *.go                           # Run application
go test -v -cover ./...               # Run go test

Todo

  • Input Validations
  • Custom Error messages
  • Logger
  • More unit tests

maybe?

  • gRPC
  • Redis Streams
  • Redis PubSub
  • WebSocket
  • MongoDB

Contribution

Welcome for suggestions

go_api_boilerplate's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go_api_boilerplate's Issues

Implement MongoDB in place of Postgres

Hi, first all I really like this boilerplate it's really well thought of. However, I'm trying to replicate your same approach with MongoDB instead of Postgres and inplace I'm using github.com/Kamva/mgm/v2 as my ODM . Here's my repository file book_repository.go

package bookrepo

import (
	"go-app/domain/book"

	"github.com/Kamva/mgm/v2"
)

type Repo interface {
	// GetByID(id uint) (*user.User, error)
	// GetByEmail(email string) (*user.User, error)
	CreateBook(book *book.Book) error
	// Update(user *user.User) error
}

// FIXME Bug Here
// In My case I'm not using `gorm`
type bookRepo struct {
	db *gorm.DB
}

// FIXME Fix this error
// NewBookRepo will instantiate Book Repository
func NewBookRepo(db *gorm.DB) Repo {
	return &bookRepo{
		db: db,
	}
}

// CreateBook returns a pointer to a Book Struct that contains specified data
// This function takes a name and pages as parameters,
// and returns a pointer to a Book struct that contains the specified data.
func CreateBook(name string, pages int) *book.Book {
	return &book.Book{
		Name:  name,
		Pages: pages,
	}
}

and here is where I set up repositories app.go


package app

import (
	"go-app/repositories/bookrepo"
	"go-app/services/bookservice"
	"net/http"

	"github.com/Kamva/mgm/v2"
	"go.mongodb.org/mongo-driver/mongo/options"

	"github.com/gin-gonic/gin"

	"go-app/controllers"
)

var (
	r = gin.Default()
)

// Init sets up mgm default config for mongodb
func init() {
	// Setup mgm default config
	err := mgm.SetDefaultConfig(nil, "books_db", options.Client().ApplyURI("mongodb://mongo:27017"))
	if err != nil {
		panic(err)
	}
}

// Run is the App Entry Point
func Run() {
	/*
		====== Setup repositories =======
	*/
	// FIXME - BUG
	bookRepo := bookrepo.NewBookRepo(db)
	/*
		====== Setup services ===========
	*/
	bookService := bookservice.NewBookService(bookRepo)
	/*
		====== Setup controllers ========
	*/
	bookCtl := controllers.NewUserController(bookService)

        // routes
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Welcome to your Gin App on Docker",
		})
	})

	r.POST("/books", bookCtl.PostBook)
	r.Run()
}

My main issue is I don't really understand how you are passing db while instantiating your Repo. Any help to integrate mongo following your approach will be highly appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.