new project
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package cmd
|
||||
@@ -0,0 +1,72 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Config -
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
Database struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
User string `yaml:"user"`
|
||||
Pass string `yaml:"pass"`
|
||||
DB string `yaml:"db"`
|
||||
} `yaml:"database"`
|
||||
Line struct {
|
||||
Access string `yaml:"access"`
|
||||
Secret string `yaml:"secret"`
|
||||
} `yaml:"line"`
|
||||
Redis struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Prefix string `yaml:"prefix"`
|
||||
}
|
||||
}
|
||||
|
||||
var conf *Config
|
||||
|
||||
// LoadConfig -
|
||||
func LoadConfig(p ...string) error {
|
||||
var fp string
|
||||
|
||||
if len(p) > 0 && len(p[0]) > 0 {
|
||||
fp = p[0]
|
||||
} else {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fp = path.Join(wd, "config.yml")
|
||||
}
|
||||
|
||||
exists := utils.CheckExists(fp, false)
|
||||
if !exists {
|
||||
return errors.New("config file not exists")
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(fp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
conf = &Config{}
|
||||
err = yaml.Unmarshal(data, conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConf -
|
||||
func GetConf() *Config {
|
||||
return conf
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package context
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import "github.com/gin-gonic/gin/binding"
|
||||
|
||||
// Context - custom http context
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
}
|
||||
|
||||
// CustomMiddle -
|
||||
type CustomMiddle func(*Context)
|
||||
|
||||
// PatchCtx - change custom middle to gin middle
|
||||
func PatchCtx(handler CustomMiddle) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := &Context{
|
||||
Context: c,
|
||||
}
|
||||
handler(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// BindData - binding data
|
||||
func (c *Context) BindData(i interface{}) error {
|
||||
b := binding.Default(c.Request.Method, c.ContentType())
|
||||
return c.ShouldBindWith(i, b)
|
||||
}
|
||||
|
||||
// CustomRes -
|
||||
func (c *Context) CustomRes(status int, msg interface{}) {
|
||||
c.AbortWithStatusJSON(status, msg)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package options
|
||||
|
||||
import "flag"
|
||||
|
||||
// Options -
|
||||
type Options struct {
|
||||
Config string
|
||||
DBTool bool
|
||||
}
|
||||
|
||||
var opts *Options
|
||||
|
||||
// RegFlag -
|
||||
func RegFlag() {
|
||||
opts = &Options{}
|
||||
flag.StringVar(&opts.Config, "config", "", "config file path (default {PWD}/config.yml)")
|
||||
flag.StringVar(&opts.Config, "f", "", "config file path (short) (default {PWD}/config.yml")
|
||||
flag.BoolVar(&opts.DBTool, "dbtool", false, "run deploy database script")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
// GetOpts -
|
||||
func GetOpts() *Options {
|
||||
return opts
|
||||
}
|
||||
Reference in New Issue
Block a user