Giter Site home page Giter Site logo

syahidfrd / go-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 7.0 97 KB

REST API boilerplate built with Go - Echo Framework

Home Page: https://pkg.go.dev/github.com/syahidfrd/go-boilerplate

License: MIT License

Shell 0.26% Dockerfile 0.82% Makefile 0.71% Go 98.22%
boilerplate rest-api echo-framework redis golang postgresql testify mockery unit-test go

go-boilerplate's Introduction

GO Boilerplate

Go Reference License: MIT Go Report Card

Directory Structure

├── cmd
│   └── api             // Main applications for this project
├── config              // Configuration app
├── delivery            // Delivery usefull to encode raw body, query params, make a response to client
│   ├── http
│   └── middleware
├── docs                // Design and user documents (in addition to your godoc generated documentation)
├── domain              // Domains is a struct to store any objects from database, example you have a products in your tables.
├── infrastructure
│   └── datastore       // Database configuration
├── migration           // Database migrations.
├── mocks               // Mock code, generated by mockery
├── repository          // Represents the behavior of the data store. It can be implemented using PostgreSQL, MongoDB, etc
│   ├── pgsql
│   └── redis
├── transport           // Request and response model
│   └── request
├── usecase             // Usecase contains all of business logic
└── utils               // Additional services
    ├── crypto
    ├── jwt

Prerequisite and full list what has been used

App requires 2 database (PostgreSQL and Redis server), run from your local machine or run it using docker with the following command

# run postgreSQL
docker run -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=go_boilerplate postgres

# run redis
docker run -d -p 6379:6379 redis

Migration

Run below command to run migration

migrate -path migration -database "${DATABASE_URL}" up

To create a new migration file

migrate create -ext sql -dir migration -seq name

Test

Run below command to run test, and make sure that all tests are passing

go test -v ./...

Running

Run below command to run app

go run ./cmd/api/main.go

Swagger URL

${BASE_URL}/swagger/index.html

You can find usefull commands in Makefile.

go-boilerplate's People

Contributors

alfiankan avatar erikadarisman avatar syahidfrd 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

Watchers

 avatar  avatar

go-boilerplate's Issues

Add graceful Shutdown for High availability service

This boilerplate is very helpful for me to create go api projects without starting from scratch, but it would be nice if we implemented a graceful shutdown for the sake of the user, let's say the user is still in the middle of a request and we need to update our app or maybe our app crashes we can drain the connection first before shutting down.

i was tried to do long live request and send interruption signal then i got an error.

Spoiler :
Screenshot 2023-01-02 at 13 10 12

My proposal :

adding graceful shitdown using go routine and channel just like in the echo docs https://echo.labstack.com/cookbook/graceful-shutdown/

package main

/* folded code */

func main() {
      
       /* folded code */

       
	// Setup route engine & middleware
	e := echo.New()
	e.Use(middleware.CORS())
	e.Use(appMiddleware.RequestID())
	e.Use(appMiddleware.Logger())
	e.Use(middleware.Recover())

	// Setup handler
	e.GET("/swagger/*", echoSwagger.WrapHandler)
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "i am alive")
	})

	httpDelivery.NewTodoHandler(e, appMiddleware, todoUC)
	httpDelivery.NewAuthHandler(e, appMiddleware, authUC)

       // gracefull shutdown
       	go func() {
		if err := e.Start(fmt.Sprintf(":%s", configApp.ServerPORT)); err != nil && err != http.ErrServerClosed {
			e.Logger.Fatal("shutting down the server")
		}
	}()

        quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := e.Shutdown(ctx); err != nil {
		e.Logger.Fatal(err)
	}

}

it would be nice if you assign this issue to me because i'm gabut

gorm

Hi, thanks for this boilerplate. I use it in my project. Do you think add gorm package to go-boilerplate?

openapi3 and swaggo

I was able to get swagger docs served via echo server. One question, your jwt stuff uses uid and password for authentication. Is it possible to change to bearer auth tokens for authentication?

Future plans

Are you planning to add user signup and JWT auth to your boilerplate?

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.