This commit is contained in:
Jay
2019-02-25 22:56:48 +08:00
commit 0dc1db02b0
9 changed files with 305 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
package config
import (
"errors"
"io/ioutil"
"os"
"path"
"strconv"
"gopkg.in/yaml.v2"
"git.trj.tw/golang/utils"
)
// Config struct
type Config struct {
Port int `yaml:"port"`
FilePath string `yaml:"file_path"`
}
var conf *Config
// LoadConfig -
func LoadConfig(p ...interface{}) (err error) {
var confPath string
if len(p) > 0 {
if str, ok := p[0].(string); ok && len(str) > 0 {
confPath = str
}
}
if len(confPath) == 0 {
root, err := os.Getwd()
if err != nil {
return err
}
confPath = path.Join(root, "config.yml")
}
if !utils.CheckExists(confPath, false) {
return errors.New("config file not found")
}
fileByte, err := ioutil.ReadFile(confPath)
if err != nil {
return err
}
conf = &Config{}
err = yaml.Unmarshal(fileByte, conf)
overrideConfgWithEnv()
if !utils.IsDir(conf.FilePath) {
return errors.New("file path not folder")
}
return
}
func overrideConfgWithEnv() {
port := os.Getenv("PORT")
if len(port) > 0 {
if port, err := strconv.Atoi(port); err == nil {
conf.Port = port
}
}
filePath := os.Getenv("FILE_PATH")
if len(filePath) > 0 {
if utils.CheckExists(filePath, true) {
conf.FilePath = filePath
}
}
}
// GetConf -
func GetConf() *Config {
return conf
}
+88
View File
@@ -0,0 +1,88 @@
package context
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// Context -
type Context struct {
*gin.Context
}
// CustomMiddleware -
type CustomMiddleware func(*Context)
// PatchContext -
func PatchContext(handle CustomMiddleware) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := &Context{
Context: c,
}
handle(ctx)
}
}
func parseResponse(body interface{}) interface{} {
switch body.(type) {
case string:
return map[string]interface{}{
"message": body,
}
case map[string]interface{}:
case map[string]string:
return body
default:
return map[string]string{
"message": "empty",
}
}
return nil
}
// BindData client body data
func (p *Context) BindData(i interface{}) error {
b := binding.Default(p.Request.Method, p.ContentType())
return p.ShouldBindWith(i, b)
}
// Success -
func (p *Context) Success(res ...interface{}) {
var resBody interface{}
if len(res) > 0 {
resBody = parseResponse(res)
} else {
resBody = parseResponse("success")
}
p.AbortWithStatusJSON(http.StatusOK, resBody)
}
// ClientError -
func (p *Context) ClientError(res ...interface{}) {
var resBody interface{}
if len(res) > 0 {
resBody = parseResponse(res)
} else {
resBody = parseResponse("bad request")
}
p.AbortWithStatusJSON(http.StatusBadRequest, resBody)
}
// ServerError -
func (p *Context) ServerError(res ...interface{}) {
var resBody interface{}
if len(res) > 0 {
resBody = parseResponse(res)
} else {
resBody = parseResponse("internal error")
}
p.AbortWithStatusJSON(http.StatusInternalServerError, resBody)
}
// CustomRes -
func (p *Context) CustomRes(status int, res interface{}) {
resBody := parseResponse(res)
p.AbortWithStatusJSON(status, resBody)
}
+22
View File
@@ -0,0 +1,22 @@
package flags
import "flag"
// Flags -
type Flags struct {
ConfigPath string
}
var flags *Flags
// RegFlags -
func RegFlags() {
flags = &Flags{}
flag.StringVar(&flags.ConfigPath, "config", "", "config path")
flag.StringVar(&flags.ConfigPath, "f", "", "config path")
}
// GetFlags -
func GetFlags() *Flags {
return flags
}