Skip to content

Commit

Permalink
feat: 添加traceid
Browse files Browse the repository at this point in the history
  • Loading branch information
2637309949 committed Nov 30, 2021
1 parent efe5e65 commit f72f928
Show file tree
Hide file tree
Showing 132 changed files with 1,237 additions and 904 deletions.
9 changes: 5 additions & 4 deletions cmd/dolphin/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bytes"
"context"
"fmt"
"html/template"
"io"
Expand All @@ -18,8 +19,8 @@ import (
"github.com/2637309949/dolphin/cmd/dolphin/modules"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
)

Expand Down Expand Up @@ -122,11 +123,11 @@ func (gen *Gen) Build(cfg *parser.TmplCfg) error {
if _, err = os.Stat(genFilePath); !os.IsNotExist(err) {
if cfg.Overlap == parser.OverlapInc {
genFilePath = genFilePath + ".new"
logrus.Info(fmt.Sprintf("%s inc generate", genFilePath))
logrus.Info(context.TODO(), fmt.Sprintf("%s inc generate", genFilePath))
} else if cfg.Overlap == parser.OverlapWrite {
logrus.Warn(fmt.Sprintf("%s over generate", genFilePath))
logrus.Warn(context.TODO(), fmt.Sprintf("%s over generate", genFilePath))
} else if cfg.Overlap == parser.OverlapSkip {
logrus.Info(fmt.Sprintf("%s skip generate", genFilePath))
logrus.Info(context.TODO(), fmt.Sprintf("%s skip generate", genFilePath))
return nil
}
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/dolphin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
Expand All @@ -18,8 +19,8 @@ import (
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/2637309949/dolphin/packages/web"

"github.com/2637309949/dolphin/packages/logrus"
"github.com/shurcooL/vfsgen"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -107,14 +108,14 @@ func InitViper(cmd *cobra.Command, _ []string) {
viper.SetDefault("swag.scope.admin", "Grants read and write access to administrative information")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
logrus.Warn("configuration file not found")
logrus.Warn(context.TODO(), "configuration file not found")
}
utils.ViperSetDefault("oauth.server", strings.TrimSpace(viper.GetString("oauth.server")), fmt.Sprintf("http://localhost:%v", viper.GetString("http.port")))
utils.ViperSetDefault("oauth.client", strings.TrimSpace(viper.GetString("oauth.client")), viper.GetString("http.port"))
utils.ViperSetDefault("app.host", strings.TrimSpace(viper.GetString("app.host")), fmt.Sprintf("localhost:%v", viper.GetString("http.port")))
if viper.GetBool("app.viper") {
if err := viper.WriteConfig(); err != nil {
logrus.Warn("failed to save configuration file")
logrus.Warn(context.TODO(), "failed to save configuration file")
}
}
}
Expand Down Expand Up @@ -218,7 +219,7 @@ var (
if err != nil {
return err
}
logrus.Infof("new project success, cd to %v dir and run `dolphin build`", args[0])
logrus.Infof(context.TODO(), "new project success, cd to %v dir and run `dolphin build`", args[0])
return nil
},
}
Expand Down Expand Up @@ -264,6 +265,6 @@ func main() {
rootCmd.AddCommand(serve)
rootCmd.AddCommand(assert)
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
logrus.Fatal(context.TODO(), err)
}
}
5 changes: 3 additions & 2 deletions cmd/dolphin/modules/more.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package modules

import (
"context"
"html/template"
"io/ioutil"
"path"
Expand All @@ -13,8 +14,8 @@ import (
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/template/dist"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/thoas/go-funk"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (m *More) Build(dir string, args []string, appParser *parser.AppParser) ([]
return []*parser.TmplCfg{}, err
}
if len(args) < 1 {
logrus.Warn("Please give the path to generate the table")
logrus.Warn(context.TODO(), "Please give the path to generate the table")
return tmplCfgs, nil
}
ctrPath := utils.SearchFileInDirWithSuffix(dir, ".xml", func(s string) bool {
Expand Down
5 changes: 3 additions & 2 deletions cmd/dolphin/modules/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package modules

import (
"context"
"errors"
"fmt"
"html/template"
Expand All @@ -15,9 +16,9 @@ import (
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/template/dist"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/2637309949/dolphin/packages/xormplus/xorm"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -86,7 +87,7 @@ func (app *Reverse) Build(dir string, args []string, appParser *parser.AppParser
return tmplCfgs, err
}
for i2 := range tables {
logrus.Infoln(tables[i2].Name)
logrus.Infoln(context.TODO(), tables[i2].Name)
meta := parser.Table{}
meta.Name = tables[i2].Name
meta.Desc = strings.ReplaceAll(tables[i2].Comment, "\n", "")
Expand Down
34 changes: 17 additions & 17 deletions cmd/dolphin/template/assets/ctr.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/gin-gonic/gin/binding"
"github.com/thoas/go-funk"
"github.com/sirupsen/logrus"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/2637309949/dolphin/packages/null"
"{{.PackageName}}/util/errors"
"time"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
db := ctx.MustDB()
ret, err := ctr.Srv.DB.PageSearch(db, "{{$.Controller.Name}}", "{{.Name}}", "{{.Table}}", q.Value())
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -87,7 +87,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
db := ctx.MustDB()
ret, err := ctr.Srv.DB.TreeSearch(db, "{{$.Controller.Name}}", "{{.Name}}", "{{.Table}}", q.Value())
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
db := ctx.MustDB()
ret, err := db.Insert(&payload)
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -135,7 +135,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
{{- $bp := index .Params 0}}
var payload {{$bp.Ref $bp.Type}}
if err := ctx.ShouldBindWith(&payload); err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -148,7 +148,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
IsDelete: null.IntFrom(1),
})
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -161,7 +161,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
IsDelete: null.IntFrom(1),
})
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -176,7 +176,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
var r int64
{{- end}}
if err := ctx.ShouldBindWith(&payload); err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -192,22 +192,22 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
payload[i].Updater = null.IntFromStr(ctx.MustToken().GetUserID())
r, err = s.ID(payload[i].ID.Int64).Update(&payload[i])
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
s.Rollback()
ctx.Fail(err)
return
}
ret = append(ret, r)
}
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
s.Rollback()
ctx.Fail(err)
return
}
err = s.Commit()
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -219,7 +219,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
db := ctx.MustDB()
ret, err := db.ID(payload.ID.Int64).Update(&payload)
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -229,13 +229,13 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
var one {{$.Viper.GetString "dir.types" }}.{{.ToUpperCase .Table}}
err := ctx.ShouldBindWith(&one)
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}

if ext, err := db.Get(&one); err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
} else if !ext {
Expand All @@ -255,7 +255,7 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
db := ctx.MustDB()
ret, err := ctr.Srv.TODO(ctx, db, struct{}{})
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand All @@ -268,15 +268,15 @@ func (ctr *{{$.Controller.ToUpperCase $.Controller.Name}}) {{$.Controller.ToUppe
var payload struct{}
{{- end}}
if err := ctx.ShouldBindWith(&payload); err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}

db := ctx.MustDB()
ret, err := ctr.Srv.TODO(ctx, db, struct{}{})
if err != nil {
logrus.Error(err)
logrus.Error(ctx, err)
ctx.Fail(err)
return
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/dolphin/template/assets/srv.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"{{.PackageName}}/{{$.Viper.GetString "dir.svc" }}"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/2637309949/dolphin/packages/xormplus/xorm"
)

Expand Down Expand Up @@ -48,10 +48,10 @@ func (srv *{{$.Controller.ToUpperCase $.Controller.Name}}) TODO(ctx context.Cont
for range ticker.C {
select {
case {{.lt}}-cwt.Done():
logrus.Infoln("child process interrupt...")
logrus.Infoln(ctx, "child process interrupt...")
return {{.lt}}-chi, cwt.Err()
default:
logrus.Infoln("awaiting job...")
logrus.Infoln(ctx, "awaiting job...")
}
}
return nil, errors.New("no implementation found")
Expand Down
Loading

0 comments on commit f72f928

Please sign in to comment.