feat: add middlewares
This commit is contained in:
@@ -31,6 +31,7 @@ func commandNewModuleE(cmd *cobra.Command, args []string) error {
|
||||
module := lo.Filter(strings.Split(args[0], "."), func(s string, _ int) bool {
|
||||
return s != ""
|
||||
})
|
||||
module = append([]string{"app/http"}, module...)
|
||||
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -42,12 +43,8 @@ func commandNewModuleE(cmd *cobra.Command, args []string) error {
|
||||
return errors.New("parse go.mod file failed")
|
||||
}
|
||||
|
||||
modulePath := module[0]
|
||||
if len(module) > 1 {
|
||||
modulePath = strings.Join(module, "/modules/")
|
||||
}
|
||||
moduleName := module[len(module)-1]
|
||||
modulePath = filepath.Join("app/http", modulePath)
|
||||
modulePath := filepath.Join(module...)
|
||||
log.Infof("new module: %s", modulePath)
|
||||
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
|
||||
9
templates/project/app/middlewares/mid_debug.go.tpl
Normal file
9
templates/project/app/middlewares/mid_debug.go.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func (f *Middlewares) DebugMode(c fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
31
templates/project/app/middlewares/mid_response.go.tpl
Normal file
31
templates/project/app/middlewares/mid_response.go.tpl
Normal file
@@ -0,0 +1,31 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"{{.ModuleName}}/app/errorx"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (f *Middlewares) ProcessResponse(c fiber.Ctx) error {
|
||||
err := c.Next()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("process response error")
|
||||
|
||||
if e, ok := err.(errorx.Response); ok {
|
||||
return e.Response(c)
|
||||
}
|
||||
|
||||
if e, ok := err.(*fiber.Error); ok {
|
||||
return errorx.Response{
|
||||
StatusCode: e.Code,
|
||||
Code: e.Code,
|
||||
Message: e.Message,
|
||||
}.Response(c)
|
||||
}
|
||||
|
||||
return errorx.Wrap(err).Response(c)
|
||||
|
||||
}
|
||||
return err
|
||||
}
|
||||
15
templates/project/app/middlewares/middlewares.go.tpl
Normal file
15
templates/project/app/middlewares/middlewares.go.tpl
Normal file
@@ -0,0 +1,15 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Middlewares struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (f *Middlewares) Prepare() error {
|
||||
f.log = log.WithField("module", "middleware")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user