Skip to content

Commit

Permalink
Merge pull request #188 from retail-ai-inc/feature/fix-templates
Browse files Browse the repository at this point in the history
Update template files
  • Loading branch information
tanvir-retailai authored Apr 22, 2024
2 parents ba9f163 + 01426a3 commit 6ea7a3f
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 18 deletions.
12 changes: 5 additions & 7 deletions cmd/bean/internal/_tpl/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ type {{.HandlerNameLower}}Handler struct {
}{{else}}func New{{.HandlerNameUpper}}Handler() *{{.HandlerNameLower}}Handler {
return &{{.HandlerNameLower}}Handler{{"{}\n}"}}{{end}}

func (handler *{{.HandlerNameLower}}Handler) {{.HandlerNameUpper}}JSONResponse(c echo.Context) error {

// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 3 lines
// tctx := trace.NewTraceableContext(c.Request().Context())
// finish := trace.Start(tctx, "http.handler")
func (handler *{{.HandlerNameLower}}Handler) {{.HandlerNameUpper}}JSONResponse(c echo.Context) error {
// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 2 lines
// tctx, finish := trace.StartSpan(c.Request().Context(), "http.handler")
// defer finish()


{{if .ServiceExists}}output, err := handler.{{.HandlerNameLower}}Service.{{.HandlerNameUpper}}ServiceExampleFunc(c.Request().Context())
if err != nil {
return err
Expand All @@ -50,8 +49,7 @@ func (handler *{{.HandlerNameLower}}Handler) {{.HandlerNameUpper}}JSONResponse(c
// prevent this from happening and not crash the app.
async.ExecuteWithContext(func(asyncC context.Context) {
c.Logger().Debug(output)
traceableContext := trace.NewTraceableContext(asyncC)
asyncFinish := trace.Start(traceableContext, "http.async")
traceableContext, asyncFinish := trace.StartSpan(asyncC, "http.async")
defer asyncFinish()

// example function that you want to execute asynchronously.
Expand Down
4 changes: 2 additions & 2 deletions cmd/bean/internal/_tpl/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func New{{.RepoNameUpper}}Repository(dbDeps *bean.DBDeps) *DbInfra {
}

func (db *DbInfra) {{.RepoNameUpper}}ExampleFunc(ctx context.Context) (string, error) {
// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 3 lines
// finish := trace.Start(ctx, "db")
// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 2 lines
// _, finish := trace.StartSpan(ctx, "db")
// defer finish()
return "{{.RepoNameUpper}}", nil
}
4 changes: 2 additions & 2 deletions cmd/bean/internal/_tpl/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type {{.ServiceNameUpper}}Service interface {
return &{{.ServiceNameLower}}Service{{"{}\n}"}}{{end}}

func (service *{{.ServiceNameLower}}Service) {{.ServiceNameUpper}}ServiceExampleFunc(ctx context.Context) (string, error) {
// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 3 lines
// finish := trace.Start(ctx, "http.service")
// IMPORTANT: If you wanna trace the performance of your handler function then uncomment following 2 lines
// _, finish := trace.StartSpan(ctx, "http.service")
// defer finish()
return "{{.ServiceNameUpper}}Service", nil
}
2 changes: 1 addition & 1 deletion cmd/bean/internal/project/go.mod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-playground/validator/v10 v10.9.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/labstack/echo/v4 v4.6.3
github.com/retail-ai-inc/bean/v2 v2.1.1
github.com/retail-ai-inc/bean/v2 v2.2.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/bean/internal/project/handlers/example.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package handlers

import (
"context"
"net/http"
"time"

Expand Down Expand Up @@ -29,8 +30,7 @@ func NewExampleHandler(exampleSvc services.ExampleService) *exampleHandler {
}

func (handler *exampleHandler) JSONIndex(c echo.Context) error {
tctx := trace.NewTraceableContext(c.Request().Context())
finish := trace.Start(tctx, "http.handler")
tctx, finish := trace.StartSpan(c.Request().Context(), "http.handler")
defer finish()

dbName, err := handler.exampleService.GetMasterSQLTableList(tctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewExampleRepository(dbDeps *bean.DBDeps) *DbInfra {
}

func (db *DbInfra) GetMasterSQLTableName(ctx context.Context) (string, error) {
finish := trace.Start(ctx, "db")
_, finish := trace.StartSpan(ctx, "db")
defer finish()
return db.Conn.MasterMySQLDBName, nil
}
2 changes: 1 addition & 1 deletion cmd/bean/internal/project/services/example.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewExampleService(exampleRepo repositories.ExampleRepository) *exampleServi
}

func (service *exampleService) GetMasterSQLTableList(ctx context.Context) (string, error) {
finish := trace.Start(ctx, "http.service")
_, finish := trace.StartSpan(ctx, "http.service")
defer finish()
return service.exampleRepository.GetMasterSQLTableName(ctx)
}
3 changes: 1 addition & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,7 @@ data, err := helpers.SingleDoChan(c, "key", func() (string, error) {
- `fn` is a callback method for executing tasks. This is its defining type: `func(c context.Context) error`
```
async.ExecuteWithTimeout(c, time.Second*3, func(ctx context.Context) error {
asyncCtx := trace.NewTraceableContext(ctx)
asyncFinish := trace.Start(asyncCtx, "http.async")
asyncCtx, asyncFinish := trace.StartSpan(ctx, "http.async")
defer asyncFinish()
select {
Expand Down

0 comments on commit 6ea7a3f

Please sign in to comment.