Commit dc7cf41f by zhengqiuyun86

初始化

parent b0fa14c1
package log
package rym_util
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"gorm.io/gorm/logger"
"runtime"
"strconv"
......@@ -25,7 +24,7 @@ func DebugDo(f func()) {
func IsDebug() bool {
ok := false
switch strings.ToLower(conf.LogLevel) {
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
ok = true
break
......@@ -35,7 +34,7 @@ func IsDebug() bool {
func Debug(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
ok = true
break
......@@ -48,7 +47,7 @@ func Debug(msg string) {
func Info(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
ok = true
break
......@@ -64,7 +63,7 @@ func Info(msg string) {
func Error(msg string) {
ok := false
switch strings.ToLower(conf.LogLevel) {
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
ok = true
break
......@@ -83,7 +82,7 @@ func Error(msg string) {
func mark(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if conf.LogColorful {
if LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + logger.Red + "%s" + logger.Reset
} else if tag == "Info" {
......
......@@ -2,14 +2,14 @@ package exception
import (
"errors"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util"
"github.com/go-playground/validator/v10"
"reflect"
)
func ThrowsErr(err error) {
if err != nil {
log.Error(err.Error())
rym_util.Error(err.Error())
panic(err)
}
}
......@@ -57,7 +57,7 @@ func (e *DError) Error() string {
}
func ThrowsErrS(err string) {
if &err != nil && err != "" {
log.Error(err)
rym_util.Error(err)
panic(DError{Err: err})
}
}
......@@ -2,7 +2,7 @@ package generate
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util"
"github.com/deckarep/golang-set"
"github.com/iancoleman/strcase"
"io"
......@@ -49,7 +49,7 @@ func BuildPgModels() {
doTable, ok := DoTables[table.Name]
if ok {
var tableFields []TableField
if conf.DBType == "PostgreSql" {
if rym_util.DBType == "PostgreSql" {
tableFields = GetTableFiledByTableName(table.Name)
} else {
tableFields = MysqlGetTableFiledByTableName(table.Name)
......@@ -83,7 +83,7 @@ func getModelImport(field TableField) string {
}
func getModelFiledType(field TableField) string {
if conf.DBType == "PostgreSql" {
if rym_util.DBType == "PostgreSql" {
return DbToGoPG[field.FieldType]
} else {
if strings.Index(field.ColumnType, "unsigned") > 0 {
......@@ -294,10 +294,10 @@ func generateDao(table *Table, fields []TableField, doTable TableInfo) {
pageQuery += "}\n"
importStr := "import (\n"
importStr += "\t\"" + projectName + "/a/PagePlus\"\n"
if conf.DBType == "" {
if rym_util.DBType == "" {
importStr += "\t\"" + projectName + "/a/db\"\n"
} else {
importStr += "\t\"" + projectName + "/a/db/" + conf.DBType + "\"\n"
importStr += "\t\"" + projectName + "/a/db/" + rym_util.DBType + "\"\n"
}
importStr += "\t\"" + projectName + "/a/exception\"\n"
importStr += "\t\"" + projectName + "/a/page\"\n"
......
......@@ -3,9 +3,9 @@ package getWay
import (
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/gin-gonic/gin"
"io/ioutil"
"net"
......@@ -45,18 +45,18 @@ func GetAdminInfo(token string) Admin {
var adminResponse = AdminResponse{}
resp, err := http.Get(conf.SystemHost + "/admin/current?token=" + token)
if err != nil {
log.Error(err.Error())
rym_util.Error(err.Error())
return Admin{}
}
defer resp.Body.Close()
adminResponseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err.Error())
rym_util.Error(err.Error())
return Admin{}
}
err = json.Unmarshal(adminResponseBytes, &adminResponse)
if err != nil {
log.Error(err.Error())
rym_util.Error(err.Error())
}
return adminResponse.Admin
}
......@@ -145,13 +145,13 @@ func ValidRequest(c *gin.Context) {
token := GetToken(c)
if token == "" {
if checkIp(ip) { //如果没有token,看是否是ip白名单
log.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip))
rym_util.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip))
} else {
log.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip))
rym_util.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip))
if checkUri(url) { //如果不是ip白名单,看是否是url白名单,例如一些第三方的回调
log.Debug(fmt.Sprintf("URL白名单校验通过:%s", url))
rym_util.Debug(fmt.Sprintf("URL白名单校验通过:%s", url))
} else {
log.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url))
rym_util.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url))
ThrowsGetWayErr(Illegal9)
}
}
......@@ -159,9 +159,9 @@ func ValidRequest(c *gin.Context) {
if redis.Exists(token) { //校验token是否有效
currentUser := GetAdminInfo(token)
c.Set(CurrentUserKey, currentUser)
log.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id))
rym_util.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id))
} else {
log.Debug(fmt.Sprintf("登陆校验不通过:%s", token))
rym_util.Debug(fmt.Sprintf("登陆校验不通过:%s", token))
ThrowsGetWayErr(Expire)
}
}
......@@ -180,7 +180,7 @@ func exceptionGetWayCatch(c *gin.Context) {
}
func checkUri(uri string) bool {
for _, v := range conf.WhiteListUri {
for _, v := range rym_util.WhiteListUri {
if strings.Index(uri, v) == 0 {
return true
}
......@@ -189,7 +189,7 @@ func checkUri(uri string) bool {
}
func checkIp(ip string) bool {
for _, v := range conf.WhiteListIp {
for _, v := range rym_util.WhiteListIp {
if strings.Index(ip, v) == 0 {
return true
}
......@@ -198,5 +198,5 @@ func checkIp(ip string) bool {
}
func logStart(c *gin.Context) {
log.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c)))
rym_util.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c)))
}
......@@ -3,10 +3,9 @@ package getWay
import (
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util"
db "git.168cad.top/zhengqiuyun/rym-util/a/db/mysql"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/log/es"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"github.com/gin-gonic/gin"
......@@ -77,7 +76,7 @@ func AliSuccess(c *gin.Context) {
}
func logEnd(c *gin.Context, data interface{}) {
esSwitch := redis.SwitchOpen(redis.ES日志开关 + conf.ServerName)
esSwitch := redis.SwitchOpen(redis.ES日志开关 + rym_util.ServerName)
startTime := GetStartTime(c)
endTime := time.Now().UnixNano()
var responseStr string
......@@ -86,14 +85,14 @@ func logEnd(c *gin.Context, data interface{}) {
rs := string(dataJson)
excludeUrl := []string{""}
if esSwitch {
if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > conf.LogResponseLength {
responseStr = rs[0:conf.LogResponseLength] + "......"
if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > rym_util.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......"
} else {
responseStr = rs
}
} else {
if len(rs) > conf.LogResponseLength {
responseStr = rs[0:conf.LogResponseLength] + "......"
if len(rs) > rym_util.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......"
} else {
responseStr = rs
}
......@@ -102,18 +101,18 @@ func logEnd(c *gin.Context, data interface{}) {
token := GetToken(c)
if esSwitch && token != "" {
esLog(c, responseStr)
log.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6))
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6))
} else {
log.Info(fmt.Sprintf("返回数据:%s", responseStr))
log.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c)))
rym_util.Info(fmt.Sprintf("返回数据:%s", responseStr))
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c)))
}
}
func esLog(c *gin.Context, response string) {
log := es.ServerLogModel{
ServerName: conf.ServerName,
Env: conf.ENV,
ServerName: rym_util.ServerName,
Env: rym_util.ENV,
TimeStamp: util.MilSecond(time.Now()),
ThreadNo: fmt.Sprintf("%d", util.GetGID()),
ServerIp: GetServerIP(),
......
......@@ -7,9 +7,9 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"io"
"io/ioutil"
......@@ -53,7 +53,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
requestStr = p.Encode()
buf = strings.NewReader(requestStr)
}
log.Debug(fmt.Sprintf("请求参数:%s", requestStr))
rym_util.Debug(fmt.Sprintf("请求参数:%s", requestStr))
req, err := http.NewRequest(method, conf.AllinpayUrl, buf)
if err != nil {
return err
......@@ -70,7 +70,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
if err != nil {
return err
}
log.Debug(fmt.Sprintf("请求结果:%s", string(data)))
rym_util.Debug(fmt.Sprintf("请求结果:%s", string(data)))
ok, err := verifyData(data, c.tlPublicKey)
if err != nil {
return err
......@@ -145,7 +145,7 @@ func signWithPKCS1v15(param url.Values, privateKey *rsa.PrivateKey, hash crypto.
}
sort.Strings(pList)
var src = strings.Join(pList, "&")
log.Debug(fmt.Sprintf("签名数据:%s", src))
rym_util.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5S(src)
sig, err := RSASignWithKey([]byte(md5Str), privateKey, hash)
if err != nil {
......@@ -179,7 +179,7 @@ func (c *Client) getDefaultNotification(data map[string]string, sign string, res
}
sort.Strings(pList)
var src = strings.Join(pList, "&")
log.Debug(fmt.Sprintf("签名数据:%s", src))
rym_util.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5B([]byte(src))
signBytes, err := base64.StdEncoding.DecodeString(sign)
if err != nil {
......
......@@ -2,10 +2,10 @@ package main
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
lib_allinpay "git.168cad.top/zhengqiuyun/rym-util/a/lib/allinpay"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"time"
)
......@@ -46,9 +46,9 @@ func testGetPayeeFundsInTransitDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%v", response.Data))
rym_util.Info(fmt.Sprintf("%v", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -69,9 +69,9 @@ func testQueryInExpDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%v", response.Data))
rym_util.Info(fmt.Sprintf("%v", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -106,7 +106,7 @@ func testCreateMember() {
if err != nil {
exception.ThrowsErr(err)
}
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testBindWxOpenId() {
......@@ -123,7 +123,7 @@ func testBindWxOpenId() {
if err != nil {
exception.ThrowsErr(err)
}
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testGetMemberInfo() {
......@@ -137,7 +137,7 @@ func testGetMemberInfo() {
if err != nil {
exception.ThrowsErr(err)
}
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testConsumeApply() {
......@@ -176,9 +176,9 @@ func testConsumeApply() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data.PayInfo))
rym_util.Info(fmt.Sprintf("%s", response.Data.PayInfo))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -196,9 +196,9 @@ func testGetOrderDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data))
rym_util.Info(fmt.Sprintf("%s", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -214,9 +214,9 @@ func testQueryReserveFundBalance() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data))
rym_util.Info(fmt.Sprintf("%s", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -233,9 +233,9 @@ func testQueryBalance() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data))
rym_util.Info(fmt.Sprintf("%s", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -251,9 +251,9 @@ func testQueryBankCard() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data))
rym_util.Info(fmt.Sprintf("%s", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -275,7 +275,7 @@ func testWithDrawApply() {
p.BackUrl = conf.MainHost + "/pay/allin/pay/draw/notify"
bankCardNo := "324348812323"
encryptBankCardNo, err := lib_allinpay.AesEncryptECB(bankCardNo, conf.AllinpaySecretKey)
log.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo))
rym_util.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo))
if err != nil {
exception.ThrowsErrS("通联密钥错误,请检查配置")
}
......@@ -291,9 +291,9 @@ func testWithDrawApply() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
log.Info(fmt.Sprintf("%s", response.Data))
rym_util.Info(fmt.Sprintf("%s", response.Data))
} else {
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -315,5 +315,5 @@ func testGetTradeNotification() {
if err != nil {
exception.ThrowsErr(err)
}
log.Info(fmt.Sprintf("%v", rd))
rym_util.Info(fmt.Sprintf("%v", rd))
}
package es
const indexNamePrefix = "manage_client_log"
package es
import (
"context"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/olivere/elastic/v7"
_log "log"
"os"
)
var Client *elastic.Client
//初始化
func init() {
host := conf.EsHttpHost
var err error
errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags)
Client, err = elastic.NewClient(elastic.SetErrorLog(errorLog), elastic.SetURL(host))
if err != nil {
exception.ThrowsErrS(fmt.Sprintf("ES连接失败:%s", err.Error()))
}
info, code, err := Client.Ping(host).Do(context.Background())
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("Elasticsearch connect %d", code))
log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number))
}
package es
import (
"context"
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
log2 "git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"github.com/olivere/elastic/v7"
"sort"
"time"
)
func getCurrentIndex() string {
return indexNamePrefix + util.FormatDates(time.Now())
}
type ServerLogModel struct {
ServerName string `json:"serverName"`
Env string `json:"env"`
TimeStamp int64 `json:"timeStamp"`
ThreadNo string `json:"threadNo"`
ServerIp string `json:"serverIp"`
ClientIp string `json:"clientIp"`
Token string `json:"token"`
CustomerId uint32 `json:"customerId"`
ClientSource string `json:"clientSource"`
Url string `json:"url"`
Method string `json:"method"`
Request string `json:"request"`
Response string `json:"response"`
DealTimes int64 `json:"dealTimes"`
}
func InsertEsLog(log interface{}) {
indexName := getCurrentIndex()
put, err := Client.Index().
Index(indexName).
BodyJson(log).
Do(context.Background())
if err != nil {
log2.Error(fmt.Sprintf("insert es log fail:%s", err.Error()))
} else {
log2.Debug(fmt.Sprintf("insert es log success:%s", put.Id))
}
}
func GroupField(indexName, field string, size, minCount int, excludeValues []string) []byte {
if size == 0 {
size = 30
}
aggregationQuery := elastic.NewTermsAggregation().Field(field).MinDocCount(minCount).Size(size)
for _, excludeValue := range excludeValues {
aggregationQuery.ExcludeValues(excludeValue)
}
if log2.IsDebug() {
source, _ := aggregationQuery.Source()
queryByte, _ := json.Marshal(source)
log2.Info(fmt.Sprintf("查询条件:%s", string(queryByte)))
}
res, err := Client.Search(indexName).
Aggregation("group_field", aggregationQuery).
From(0).
Size(0).
Do(context.Background())
if err != nil {
exception.ThrowsErr(err)
}
bs, err := json.Marshal(res.Aggregations)
if err != nil {
exception.ThrowsErr(err)
}
return bs
}
func QueryLogs(indexName, filed, value string, from, size int) interface{} {
if from <= 0 {
from = 0
}
if size <= 0 {
size = 10
}
query := elastic.NewBoolQuery().Must(elastic.NewTermQuery(filed, value))
res, err := Client.Search(indexName).Query(query).
From(from).
Size(size).
Do(context.Background())
if err != nil {
exception.ThrowsErr(err)
}
return res
}
type IndexInfo struct {
Name string `json:"name"`
Count int `json:"count"`
Size string `json:"size"`
}
func AllIndex() interface{} {
res, err := Client.CatIndices().Do(context.Background())
if err != nil {
exception.ThrowsErr(err)
}
var keys []string
var sizeMap = make(map[string]IndexInfo)
for _, row := range res {
keys = append(keys, row.Index)
sizeMap[row.Index] = IndexInfo{row.Index, row.DocsCount, row.StoreSize}
}
sort.Strings(keys)
data := make([]IndexInfo, len(keys))
for i, key := range keys {
data[i] = sizeMap[key]
}
return data
}
func DeleteIndex(indexName string) interface{} {
res, err := Client.DeleteIndex(indexName).
Do(context.Background())
if err != nil {
exception.ThrowsErr(err)
}
return res
}
package conf
package rym_util
type Config struct {
ENV string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment