go-gin-wrapper

module
v0.0.0-...-914eb61 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2022 License: MIT

README

go-gin-wrapper

Build Status Coverage Status Go Report Card codebeat badge Codacy Badge MIT License Deploy

Go-gin-wrapper is wrapper of gin-gonic/gin web framework plus reverse proxy.
React by ES6 is used on part of front-end. But it's quite outdated.

This project has started since 2016 to study Golang and code is quite messy. Now it's under refactoring.

Refactoring

  • refactoring code by The Second Edition of "Refactoring"
  • refactoring code by codebeat.co
  • change architecture like Clean Architecture
    • make UI adaptable like WEB to CUI
  • fix session functionality
  • remove any dependencies from hiromaily/golibs
    • use sqlboiler as ORM
    • remove MongoDB
    • replace log to zap logger
  • add zap logger
  • add graceful shutdown
  • refactoring jwt package
  • fix main_test
  • unittest by table driven test
  • clean up variable, func name
  • clean up comments
  • catch up with latest gin
  • update front-end

Requirements

  • Golang 1.15+
  • Docker compose
    • MySQL 5.7
    • Redis
  • direnv for MacOS user

Example

Example is here on Heroku.

Functionalities

Authentication for Login

OAuth2 authentication with Google/Facebook is available.

Authentication for API

JWT(Json Web Token) is used for authentication. See configuration [api.auth] in toml file.

APIs documentation by Swagger

Configuration

See ./configs/settings.default.toml
As needed, secret information can be encrypted.(using AES encryption)

Dependent middleware

  • MySQL
  • Redis whose used as session store

Installation on local (MacOS is expected)

# 1. clone repository
$ git clone https://github.com/hiromaily/go-gin-wrapper.git

# 2. copy settings.default.toml
$ cp configs/settings.default.toml configs/settings.toml

# 3. edit `configs/settings.toml`
# 3.1. you may need to modify in settings.toml
 [server.docs]
 # set `go-gin-wrapper` path
 # this path must be chnaged first for specific environment
 path = "${GOPATH}/src/github.com/hiromaily/go-gin-wrapper"

$ 4. start MySQL
$ docker-compose up mysql
 
# 5. make sure `go run` works
$ go run ./cmd/ginserver/ -f ./configs/settings.toml

# 6. unit test requires ${GOGIN_CONF} environment variable
- define proper path on `.envrc`

Installation on Docker

$ docker-compose build
$ docker-compose up
  • configs/docker.toml
  • docker-compose.yml
  • docker-compose.override.yml
  • Dockerfile
  • ./build/docker/*

Installation on Heroku

## Install 
$ heroku create ginserver --buildpack heroku/go

# MySQL
$ heroku addons:create cleardb
$ heroku config | grep CLEARDB_DATABASE_URL

# Redis
$ heroku addons:create heroku-redis:hobby-dev -a ginserver 
$ heroku config | grep REDIS

## Environment variable
$ heroku config:add ENC_KEY=xxxxx
$ heroku config:add ENC_IV=xxxxx

## Check
$ heroku config | grep CLEARDB_DATABASE_URL
$ heroku config | grep REDIS
$ heroku ps -a ginserver

## Deploy
$ git push -f heroku master
$ ginserver -f /app/configs/heroku.toml -crypto

Environment variables

NAME Value Explanation
GOGIN_CONF xxxxx config path, required in unit test
ENC_KEY xxxxx encryption
ENC_IV xxxxx encryption
Only Heroku environment
NAME Value
PORT 8080
  • Heroku server use PORT automatically as environment variable.

Usage

Usage: ginserver [options...]

Options:
  -f      Toml file path
  -p      Overwriten server port number
  -crypto if true, values in config file are encrypted

e.g.
 $ ginserver -f /app/configs/yourfile.toml -crypto

Profiling

See config file.

[profile]
enable = true

After running ginserver, acccess the below links.

[GIN-debug] GET    /debug/pprof/
[GIN-debug] GET    /debug/pprof/heap
[GIN-debug] GET    /debug/pprof/goroutine
[GIN-debug] GET    /debug/pprof/block
[GIN-debug] GET    /debug/pprof/threadcreate
[GIN-debug] GET    /debug/pprof/cmdline
[GIN-debug] GET    /debug/pprof/profile
[GIN-debug] GET    /debug/pprof/symbol
[GIN-debug] POST   /debug/pprof/symbol
[GIN-debug] GET    /debug/pprof/trace

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL