Giter Club home page Giter Club logo

go-api-boilerplate's Introduction

Go API Boilerplate

Features

  • Framework for API: Gin
  • Package manager: go mod
  • DI: Based on service container
  • Layers: Controller->Service->Repository->Entity
  • Routes: Gin
  • Process controller results and convert them into JSON/XML according to request headers
  • Logger: logrus
  • Environment variables, config: Viper
  • ORM: GORM
  • Migrations: gorm-goose
  • Base CRUD service
  • Base CRUD repository
  • Base CRUD controller
  • Request validation (Gin)
  • Console commands: Cobra
  • Unit tests with overriding of services in DI (go test)
  • Code coverage by tests (go tool cover)
  • Logger integration with Sentry: logrus_sentry
  • Setup alerting for unhandled errors
  • Swagger
  • Docker compose

Folders structure

  • command/: Console commands.
  • controller/: Controllers for web requests processing.
  • db/: Migrations.
  • dic/: Dependency Injection Container.
  • doc/: Swagger documentation.
  • docker/: Docker containers description.
  • install/: Scripts for environment preparing.
  • logger/: Logger and client for Sentry.
  • model/: Business logic.
  • model/db/: DB connection.
  • model/entity/: GORM entities.
  • model/repository/: Repositories for access to storage.
  • model/service/: Business logic.
  • route/: Web requests routes.
  • test/: Unit tests.
  • vendor/: Packages used in application.
  • .env: Environment variables for current environment.
  • base.env: Base environment variables.

How to use (Docker)

docker-compose up --build

Check http://localhost:8080

How to use (without Docker)

Prepare environment for Go projects if you do not done it early

sudo apt update
sudo apt upgrade
# See last version here: https://golang.org/dl/
wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
sudo tar -xvf go1.12.5.linux-amd64.tar.gz
sudo mv go /usr/local
sudo mcedit /etc/profile

And add last line:

export PATH=$PATH:/usr/local/go/bin

Update environment variables:

source /etc/profile

Check Go version:

go version

Now create folder for Go projects:

mkdir ~/go
cd ~/go
touch init.sh
mcedit init.sh

Paste next code into this file:

#!/bin/bash

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Execute file:

chmod +x init.sh
source init.sh

Clone repo

git clone [email protected]:zubroide/go-api-boilerplate.git
cd go-api-boilerplate

Install necessary packages

./install/install.sh

Create and edit config

cp .env.example .env
mcedit .env

Download vendor packages

go mod download

Run migrations

Create database go-api-boilerplate.

And run migrations:

make migrate

Run application

make server

Or:

go run main.go server --port=8080

Check http://localhost:8080

Run tests

Run all tests:

go test ./... -v -coverpkg=./... -coverprofile=coverage.out
go tool cover -html=coverage.out

Run test for one package:

go test go-api-boilerplate/test/unit -v -coverpkg=./... -coverprofile=coverage.out

Run one test:

go test test/unit/user_service_test.go -v -coverpkg=./... -coverprofile=coverage.out

Using make:

make test

Generate Swagger documentation

Generate swagger.json:

make swagger

Documentation must be available at url http://localhost:8080/swagger/index.html

Requirements

  • Go 1.12+

go-api-boilerplate's People

Contributors

mnvx 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

go-api-boilerplate's Issues

Other database providers

Id love to use this with mongodb it would be great if there was an example or some db abstraction to make it easy to plug in another type

A quick question

Hey there,
I am still fairly new to go and wanted to try this out. Attempting to run govendor sync as per the instructions says it's no longer in use and to use go mod vendor, which I am still trying to get working, but I noticed this is fairly recently uploaded, but is this an older project that was just uploaded, or is it up to date to try and use/learn from?

Thanks!
-MH

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.