add modules

This commit is contained in:
Jay
2018-08-22 16:57:02 +08:00
parent 0ab7aa73c7
commit a16ed990c1
18 changed files with 3093 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
package apimsg
// ResObject -
type ResObject struct {
Status int
Obj interface{}
}
var objs = map[string]*ResObject{
"NotFound": &ResObject{
Status: 404,
Obj: map[string]string{
"message": "not found",
},
},
"InternalError": &ResObject{
Status: 500,
Obj: map[string]string{
"message": "server internal error",
},
},
"Success": &ResObject{
Status: 200,
Obj: map[string]string{
"message": "success",
},
},
"Forbidden": &ResObject{
Status: 403,
Obj: map[string]string{
"message": "forbidden",
},
},
"DataFormat": &ResObject{
Status: 400,
Obj: map[string]string{
"message": "input data format error",
},
},
}
// GetRes -
func GetRes(name string, msg interface{}) *ResObject {
obj, ok := objs[name]
if !ok {
obj = objs["InternalError"]
}
resobj := &ResObject{}
resobj.Status = obj.Status
switch msg.(type) {
case string:
tmp := make(map[string]string)
tmp["message"] = msg.(string)
resobj.Obj = tmp
break
case map[string]interface{}:
resobj.Obj = msg
break
default:
resobj.Obj = obj.Obj
}
return resobj
}
+57
View File
@@ -0,0 +1,57 @@
package config
import (
"errors"
"io/ioutil"
"os"
"path"
"git.trj.tw/golang/mtfosbot/module/utils"
yaml "gopkg.in/yaml.v2"
)
// Config -
type Config struct {
Port string `yaml:"port"`
URL string `yaml:"url"`
ImageRoot string `yaml:"image_root"`
Line struct {
secret string `yaml:"secret"`
access string `yaml:"access"`
} `yaml:"line"`
}
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")
}
fp = utils.ParsePath(fp)
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
}
+60
View File
@@ -0,0 +1,60 @@
package context
import (
"git.trj.tw/golang/mtfosbot/module/apimsg"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// Context custom context struct
type Context struct {
*gin.Context
}
// CustomMiddle func
type CustomMiddle func(*Context)
// PatchCtx - patch ctx to custom middle
func PatchCtx(handler func(*Context)) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := &Context{
Context: c,
}
handler(ctx)
}
}
// BindData client body 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)
}
// NotFound -
func (c *Context) NotFound(msg interface{}) {
obj := apimsg.GetRes("NotFound", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// DataFormat -
func (c *Context) DataFormat(msg interface{}) {
obj := apimsg.GetRes("DataFormat", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// Success -
func (c *Context) Success(msg interface{}) {
obj := apimsg.GetRes("Success", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// ServerError response
func (c *Context) ServerError(msg interface{}) {
obj := apimsg.GetRes("InternalError", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
+84
View File
@@ -0,0 +1,84 @@
package utils
import (
"os"
"path"
"reflect"
"regexp"
"runtime"
"strings"
)
// ToMap struct to map[string]interface{}
func ToMap(ss interface{}) map[string]interface{} {
t := reflect.ValueOf(ss)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
smap := make(map[string]interface{})
mtag := regexp.MustCompile(`cc:\"(.+)\"`)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
tag := string(t.Type().Field(i).Tag)
str := mtag.FindStringSubmatch(tag)
name := t.Type().Field(i).Name
if len(str) > 1 {
name = str[1]
}
if name != "-" {
smap[name] = f.Interface()
}
}
return smap
}
// ParsePath - parse file path to absPath
func ParsePath(dst string) string {
wd, err := os.Getwd()
if err != nil {
wd = ""
}
if []rune(dst)[0] == '~' {
home := UserHomeDir()
if len(home) > 0 {
dst = strings.Replace(dst, "~", home, -1)
}
}
if path.IsAbs(dst) {
dst = path.Clean(dst)
return dst
}
str := path.Join(wd, dst)
str = path.Clean(str)
return str
}
// UserHomeDir - get user home directory
func UserHomeDir() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
return os.Getenv(env)
}
// CheckExists - check file exists
func CheckExists(filePath string, allowDir bool) bool {
filePath = ParsePath(filePath)
stat, err := os.Stat(filePath)
if err != nil && !os.IsExist(err) {
return false
}
if !allowDir && stat.IsDir() {
return false
}
return true
}