Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf513 committed Nov 22, 2019
0 parents commit 6c166fb
Show file tree
Hide file tree
Showing 26 changed files with 1,393 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app

const (
Success = 0
Error = 1
InvalidParams = 400
NotFound = 404
InterError = 500
)
20 changes: 20 additions & 0 deletions app/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app

var Message = map[int]string{
Success: "成功",
Error: "失败",
InvalidParams: "请求参数错误",
InterError: "内部请求异常",
NotFound: "数据未找到",
}

// GetMsg 根据错误码获取错误信息
func GetMsg(code int) string {
msg, ok := Message[code]
if ok {
return msg
}

return Message[Error]
}

59 changes: 59 additions & 0 deletions app/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package app

import (
"github.com/gin-gonic/gin"

"go-micro-pkg/jaeger"
"go-micro-pkg/log"
)

type Response struct {
C *gin.Context
}

type response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}

func (g *Response) Success(data interface{}, msg ...string) {
message := ""
if len(msg) > 0 {
message = msg[0]
} else {
message = GetMsg(Success)
}
g.C.JSON(200, response{
Code: Success,
Msg: message,
Data: data,
})
}

func (g *Response) Error(httpCode int, errCode int, message string, data interface{}) {
log.Trace(g.C.Request.Context()).Sampler()
g.C.JSON(httpCode, response{
Code: errCode,
Msg: message,
Data: data,
})
}

func (g *Response) Fail(errCode int, errMsg string, message ...string) {
if errMsg != "" {
log.Trace(g.C.Request.Context()).Sampler()
log.Error(g.C.Request.Context(), errMsg, "response")
}
var msg string
if len(message) > 0 {
msg = message[0]
} else {
msg = GetMsg(errCode)
}
g.C.JSON(200, response{
Code: errCode,
Msg: msg,
Data: jaeger.GetTraceId(g.C.Request.Context()),
})
}
15 changes: 15 additions & 0 deletions config/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

type Etcd struct {
Host string
}

var etcd Etcd

func GetEtcd() Etcd {
return etcd
}

func SetEtcd(conf Etcd) {
etcd = conf
}
16 changes: 16 additions & 0 deletions config/jaeger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

type Jaeger struct {
Name string `json:"name"`
Address string `json:"address"`
}

var jaeger Jaeger

func GetJaeger() Jaeger {
return jaeger
}

func SetJaeger(j Jaeger) {
jaeger = j
}
25 changes: 25 additions & 0 deletions config/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import "time"

type DB struct {
Type string
Host string
Username string
Password string
DBName string
MaxIdleConn int `json:"max_idle_conn"`
MaxOpenConn int `json:"max_open_conn"`
MaxLifeTime time.Duration `json:"max_lifetime"`
Debug bool
}

var dbs = make(map[string]DB)

func GetDBs() map[string]DB {
return dbs
}

func SetDBs(s string, d DB) {
dbs[s] = d
}
25 changes: 25 additions & 0 deletions config/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import "time"

type Redis struct {
Server string
MaxIdle int `json:"max_idle"`
MaxActive int `json:"max_active"`
ConnectTimeout time.Duration `json:"connect_timeout"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
IdleTimeout time.Duration `json:"idle_timeout"`
Password string `json:"password"`
SelectDB int `json:"select_db"`
}

var redis = make(map[string]Redis)

func GetRedis() map[string]Redis {
return redis
}

func SetRedis(s string, r Redis) {
redis[s] = r
}
15 changes: 15 additions & 0 deletions config/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config

type Sentry struct {
Dns string `json:"dns"`
}

var sentry Sentry

func SetSentry(s Sentry) {
sentry = s
}

func GetSentry() Sentry {
return sentry
}
17 changes: 17 additions & 0 deletions config/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

type Server struct {
Name string
Env string
Mode string
}

var server Server

func GetServer() Server {
return server
}

func SetServer(s Server) {
server = s
}
20 changes: 20 additions & 0 deletions config/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

type Logger struct {
FilePath string `json:"file_path"`
MaxSize int `json:"max_size"`
MaxBackups int `json:"max_backups"`
MaxAge int `json:"max_age"`
Compress bool
Level string
}

var logger Logger

func SetLogger(l Logger) {
logger = l
}

func GetLogger() Logger {
return logger
}
17 changes: 17 additions & 0 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package etcd

import (
"github.com/micro/go-micro/registry"
"github.com/micro/go-plugins/registry/etcdv3"

"go-micro-pkg/config"
)

// Init 初始化 etcd
func Init() registry.Registry {
return etcdv3.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
config.GetEtcd().Host,
}
})
}
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module go-micro-pkg

go 1.13

replace github.com/ugorji/go v1.1.4 => github.com/ugorji/go/codec v0.0.0-20190204201341-e444a5086c43

require (
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/getsentry/sentry-go v0.3.1
github.com/gin-gonic/gin v1.4.0
github.com/gogo/protobuf v1.3.1
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jinzhu/gorm v1.9.11
github.com/micro/go-micro v1.16.0
github.com/micro/go-plugins v1.5.1
github.com/opentracing/opentracing-go v1.1.0
github.com/uber/jaeger-client-go v2.20.1+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
go.uber.org/zap v1.13.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
49 changes: 49 additions & 0 deletions jaeger/jaeger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package jaeger

import (
"context"
"io"

"github.com/micro/go-micro/util/log"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
)


var sampler jaeger.Sampler

// Connect 创建一个jaeger Tracer
func Connect(serverName, host string) (opentracing.Tracer, io.Closer) {
sender, err := jaeger.NewUDPTransport(host, 0)
if err != nil {
log.Fatal("jaeger.connect was failed")
}
var tracer, closer = jaeger.NewTracer(
serverName,
GetSampler(),
jaeger.NewRemoteReporter(sender),
)
opentracing.SetGlobalTracer(tracer)
return tracer, closer
}

func SetSampler(s jaeger.Sampler) {
sampler = s
}

func GetSampler() jaeger.Sampler {
if sampler == nil {
return jaeger.NewConstSampler(true) // 全量追踪
}

return sampler
}

func GetTraceId(ctx context.Context) string {
span := opentracing.SpanFromContext(ctx)
if span != nil {
return span.Context().(jaeger.SpanContext).TraceID().String()
}

return ""
}
55 changes: 55 additions & 0 deletions log/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package log

import (
"context"
"errors"

"go.uber.org/zap"

"go-micro-pkg/config"
"go-micro-pkg/jaeger"
zap2 "go-micro-pkg/zap"
)

var fields []zap.Field

func SetFields(fds []zap.Field) {
fields = fds
}

func otherFields(ctx context.Context, category string) []zap.Field {
if len(fields) == 0 {
return []zap.Field{
zap.String("traceId", jaeger.GetTraceId(ctx)),
zap.String("app", config.GetServer().Name),
zap.String("env", config.GetServer().Env),
zap.String("mode", config.GetServer().Mode),
zap.String("category", category),
}
} else {
return fields
}
}

func Info(ctx context.Context, msg, category string) {
zap2.GetLogger().Info(msg, otherFields(ctx, category)...)
}

func Warn(ctx context.Context, msg, category string) {
SentryException(errors.New(msg))
zap2.GetLogger().Warn(msg, otherFields(ctx, category)...)
}

func Error(ctx context.Context, msg, category string) {
SentryException(errors.New(msg))
zap2.GetLogger().Error(msg, otherFields(ctx, category)...)
}

func Debug(ctx context.Context, msg, category string) {
zap2.GetLogger().Debug(msg, otherFields(ctx, category)...)
}

func Fatal(ctx context.Context, msg, category string) {
SentryException(errors.New(msg))
zap2.GetLogger().Fatal(msg, otherFields(ctx, category)...)
}
21 changes: 21 additions & 0 deletions log/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package log

import (
"time"

"github.com/getsentry/sentry-go"
)

const sentryFlushTimeout = 3 * time.Second

// SentryMessage 只会记录 msg,不会记录 stack
func SentryMessage(msg string) {
sentry.CaptureMessage(msg)
sentry.Flush(sentryFlushTimeout)
}

// SentryMessage 记录 msg 和 stack
func SentryException(err error) {
sentry.CaptureException(err)
sentry.Flush(sentryFlushTimeout)
}
Loading

0 comments on commit 6c166fb

Please sign in to comment.