add backend tpl
This commit is contained in:
40
backend_v1/cmd/migrate.go
Normal file
40
backend_v1/cmd/migrate.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.ipao.vip/rogeecn/mp-qvyun/conf"
|
||||
"git.ipao.vip/rogeecn/mp-qvyun/database"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CommandMigrate(root *cobra.Command) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "migrate",
|
||||
RunE: commandMigrate,
|
||||
}
|
||||
root.AddCommand(cmd)
|
||||
}
|
||||
|
||||
func commandMigrate(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
args = append(args, "up")
|
||||
}
|
||||
|
||||
db, err := database.GetDB(conf.C.Database)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get db")
|
||||
}
|
||||
|
||||
action, args := args[0], args[1:]
|
||||
log.Infof("migration action: %s args: %+v", action, args)
|
||||
|
||||
goose.SetBaseFS(database.MigrationFS)
|
||||
goose.SetTableName("migrations")
|
||||
|
||||
return goose.RunContext(context.Background(), action, db, "migrations", args...)
|
||||
}
|
||||
61
backend_v1/cmd/serve.go
Normal file
61
backend_v1/cmd/serve.go
Normal file
@@ -0,0 +1,61 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user