Files
mp-qvyun/backend_v1/cmd/serve.go
2024-11-28 23:18:11 +08:00

62 lines
1.6 KiB
Go

package cmd
import (
"fmt"
"git.ipao.vip/rogeecn/mp-qvyun/conf"
"git.ipao.vip/rogeecn/mp-qvyun/pkg/middlewares/fiberv3"
"git.ipao.vip/rogeecn/mp-qvyun/pkg/wechat"
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func CommandServe(root *cobra.Command) {
cmd := &cobra.Command{
Use: "serve",
Short: "start http server",
RunE: commandServe,
}
root.AddCommand(cmd)
}
func commandServe(cmd *cobra.Command, args []string) error {
wechatClient := wechat.New(
wechat.WithAppID(conf.C.Wechat.AppID),
wechat.WithAppSecret(conf.C.Wechat.AppSecret),
wechat.WithAESKey(conf.C.Wechat.AesKey),
wechat.WithToken(conf.C.Wechat.Token),
)
wechatMiddlewares := fiberv3.Init(wechatClient)
// create a new fiber server
app := fiber.New()
app.Use(LogAll)
app.Use(wechatMiddlewares.Verify)
app.Use(wechatMiddlewares.AuthUserInfo)
app.Use(wechatMiddlewares.SilentAuth)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello World")
})
// listen on port 3000
if err := app.Listen(fmt.Sprintf(":%d", conf.C.Port)); err != nil {
return errors.Wrap(err, "http server listen")
}
return nil
}
func LogAll(c fiber.Ctx) error {
log.Info("------------------------------------------")
log.Infof("Request Method: %s", c.Method())
log.Infof("Request Headers: %s", &c.Request().Header)
log.Infof("Request URL: %s", c.OriginalURL())
log.Infof("Request Query: %+v", c.Queries())
log.Infof("Request Body: %s", c.BodyRaw())
log.Info("------------------------------------------")
return c.Next()
}