feat: add tenant commands
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -4,6 +4,6 @@ package consts
|
||||
// ENUM(
|
||||
// Tx = "__ctx_db:",
|
||||
// Jwt = "__jwt_token:",
|
||||
// Session = "__session_user:",
|
||||
// Claim = "__jwt_claim:",
|
||||
// )
|
||||
type CtxKey string
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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"},
|
||||
|
||||
@@ -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"
|
||||
|
||||
73
backend/common/service/tenants/tenants.go
Normal file
73
backend/common/service/tenants/tenants.go
Normal 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)
|
||||
})
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
1
backend/common/session.go
Normal file
1
backend/common/session.go
Normal file
@@ -0,0 +1 @@
|
||||
package common
|
||||
Reference in New Issue
Block a user