feat: add tenant commands

This commit is contained in:
Rogee
2024-12-07 18:48:05 +08:00
parent e15d8d9bb2
commit 8f88929575
25 changed files with 271 additions and 48 deletions

View File

@@ -18,8 +18,8 @@ const (
CtxKeyTx CtxKey = "__ctx_db:"
// CtxKeyJwt is a CtxKey of type Jwt.
CtxKeyJwt CtxKey = "__jwt_token:"
// CtxKeySession is a CtxKey of type Session.
CtxKeySession CtxKey = "__session_user:"
// CtxKeyClaim is a CtxKey of type Claim.
CtxKeyClaim CtxKey = "__jwt_claim:"
)
var ErrInvalidCtxKey = fmt.Errorf("not a valid CtxKey, try [%s]", strings.Join(_CtxKeyNames, ", "))
@@ -27,7 +27,7 @@ var ErrInvalidCtxKey = fmt.Errorf("not a valid CtxKey, try [%s]", strings.Join(_
var _CtxKeyNames = []string{
string(CtxKeyTx),
string(CtxKeyJwt),
string(CtxKeySession),
string(CtxKeyClaim),
}
// CtxKeyNames returns a list of possible string values of CtxKey.
@@ -42,7 +42,7 @@ func CtxKeyValues() []CtxKey {
return []CtxKey{
CtxKeyTx,
CtxKeyJwt,
CtxKeySession,
CtxKeyClaim,
}
}
@@ -59,9 +59,9 @@ func (x CtxKey) IsValid() bool {
}
var _CtxKeyValue = map[string]CtxKey{
"__ctx_db:": CtxKeyTx,
"__jwt_token:": CtxKeyJwt,
"__session_user:": CtxKeySession,
"__ctx_db:": CtxKeyTx,
"__jwt_token:": CtxKeyJwt,
"__jwt_claim:": CtxKeyClaim,
}
// ParseCtxKey attempts to convert a string to a CtxKey.

View File

@@ -4,6 +4,6 @@ package consts
// ENUM(
// Tx = "__ctx_db:",
// Jwt = "__jwt_token:",
// Session = "__session_user:",
// Claim = "__jwt_claim:",
// )
type CtxKey string

View File

@@ -1,21 +0,0 @@
package jwt
import (
"backend/providers/jwt"
"github.com/gofiber/fiber/v3"
)
func GetJwtToken(ctx fiber.Ctx) (string, error) {
headers, ok := ctx.GetReqHeaders()[jwt.HttpHeader]
if !ok {
return "", ctx.SendStatus(fiber.StatusUnauthorized)
}
if len(headers) == 0 {
return "", ctx.SendStatus(fiber.StatusUnauthorized)
}
token := headers[0]
token = token[len(jwt.TokenPrefix):]
return token, nil
}

View File

@@ -64,7 +64,7 @@ func Serve(cmd *cobra.Command, args []string) error {
http.Service.Engine.Use(mid.WeChatVerify)
http.Service.Engine.Use(mid.WeChatAuthUserInfo)
http.Service.Engine.Use(mid.WeChatSilentAuth)
http.Service.Engine.Use(mid.JwtParse)
http.Service.Engine.Use(mid.ParseJWT)
mounts := map[string][]string{
"/t/{tenant}": {"users", "medias"},

View File

@@ -1,9 +1,9 @@
package tasks
import (
"backend/modules/commands/discover"
"backend/modules/commands/store"
"backend/modules/medias"
"backend/modules/tasks/discover"
"backend/modules/tasks/store"
"backend/providers/app"
"backend/providers/postgres"
"backend/providers/storage"

View File

@@ -0,0 +1,73 @@
package tenants
import (
"time"
"backend/modules/commands/tenant"
"backend/modules/medias"
"backend/modules/users"
"backend/providers/app"
"backend/providers/postgres"
"backend/providers/storage"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func defaultProviders(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
app.DefaultProvider(),
storage.DefaultProvider(),
postgres.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("tenants"),
atom.Short("run tenants"),
atom.Command(
atom.Name("create"),
atom.Providers(defaultProviders().With(
medias.Provide,
users.Provide,
tenant.Provide,
)),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("slug", "", "slug")
}),
atom.RunE(func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(t *tenant.Create) error {
slug := cmd.Flag("slug").Value.String()
return t.RunE(args[0], slug)
})
}),
),
atom.Command(
atom.Name("expire"),
atom.Long("expire [slug] [2024-01-01]"),
atom.Providers(defaultProviders().With(
medias.Provide,
users.Provide,
tenant.Provide,
)),
atom.Arguments(func(cmd *cobra.Command) {
}),
atom.RunE(func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(t *tenant.Expire) error {
slug := args[0]
expireStr := args[1] // format 2024-01-01
// parse expire string as time.Time
expire, err := time.Parse("2006-01-02", expireStr)
if err != nil {
return errors.Wrapf(err, "parse expire time failed: %s", expireStr)
}
return t.RunE(slug, expire)
})
}),
),
)
}

View File

@@ -0,0 +1 @@
package common