fix: issues

This commit is contained in:
Rogee
2024-12-10 10:24:31 +08:00
parent 0c9cb498d5
commit cb1cdee87e
32 changed files with 226 additions and 61 deletions

8
backend/pkg/consts/consts.go Executable file
View File

@@ -0,0 +1,8 @@
package consts
// Format
//
// // swagger:enum CacheKey
// // ENUM(
// // VerifyCode = "code:__CHANNEL__:%s",
// // )

View File

@@ -0,0 +1,179 @@
// Code generated by go-enum DO NOT EDIT.
// Version: -
// Revision: -
// Build Date: -
// Built By: -
package consts
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
)
const (
// CtxKeyTx is a CtxKey of type Tx.
CtxKeyTx CtxKey = "__ctx_db:"
// CtxKeyJwt is a CtxKey of type Jwt.
CtxKeyJwt CtxKey = "__jwt_token:"
// CtxKeyClaim is a CtxKey of type Claim.
CtxKeyClaim CtxKey = "__jwt_claim:"
)
var ErrInvalidCtxKey = fmt.Errorf("not a valid CtxKey, try [%s]", strings.Join(_CtxKeyNames, ", "))
var _CtxKeyNames = []string{
string(CtxKeyTx),
string(CtxKeyJwt),
string(CtxKeyClaim),
}
// CtxKeyNames returns a list of possible string values of CtxKey.
func CtxKeyNames() []string {
tmp := make([]string, len(_CtxKeyNames))
copy(tmp, _CtxKeyNames)
return tmp
}
// CtxKeyValues returns a list of the values for CtxKey
func CtxKeyValues() []CtxKey {
return []CtxKey{
CtxKeyTx,
CtxKeyJwt,
CtxKeyClaim,
}
}
// String implements the Stringer interface.
func (x CtxKey) String() string {
return string(x)
}
// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x CtxKey) IsValid() bool {
_, err := ParseCtxKey(string(x))
return err == nil
}
var _CtxKeyValue = map[string]CtxKey{
"__ctx_db:": CtxKeyTx,
"__jwt_token:": CtxKeyJwt,
"__jwt_claim:": CtxKeyClaim,
}
// ParseCtxKey attempts to convert a string to a CtxKey.
func ParseCtxKey(name string) (CtxKey, error) {
if x, ok := _CtxKeyValue[name]; ok {
return x, nil
}
return CtxKey(""), fmt.Errorf("%s is %w", name, ErrInvalidCtxKey)
}
var errCtxKeyNilPtr = errors.New("value pointer is nil") // one per type for package clashes
// Scan implements the Scanner interface.
func (x *CtxKey) Scan(value interface{}) (err error) {
if value == nil {
*x = CtxKey("")
return
}
// A wider range of scannable types.
// driver.Value values at the top of the list for expediency
switch v := value.(type) {
case string:
*x, err = ParseCtxKey(v)
case []byte:
*x, err = ParseCtxKey(string(v))
case CtxKey:
*x = v
case *CtxKey:
if v == nil {
return errCtxKeyNilPtr
}
*x = *v
case *string:
if v == nil {
return errCtxKeyNilPtr
}
*x, err = ParseCtxKey(*v)
default:
return errors.New("invalid type for CtxKey")
}
return
}
// Value implements the driver Valuer interface.
func (x CtxKey) Value() (driver.Value, error) {
return x.String(), nil
}
// Set implements the Golang flag.Value interface func.
func (x *CtxKey) Set(val string) error {
v, err := ParseCtxKey(val)
*x = v
return err
}
// Get implements the Golang flag.Getter interface func.
func (x *CtxKey) Get() interface{} {
return *x
}
// Type implements the github.com/spf13/pFlag Value interface.
func (x *CtxKey) Type() string {
return "CtxKey"
}
type NullCtxKey struct {
CtxKey CtxKey
Valid bool
}
func NewNullCtxKey(val interface{}) (x NullCtxKey) {
err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
_ = err // make any errcheck linters happy
return
}
// Scan implements the Scanner interface.
func (x *NullCtxKey) Scan(value interface{}) (err error) {
if value == nil {
x.CtxKey, x.Valid = CtxKey(""), false
return
}
err = x.CtxKey.Scan(value)
x.Valid = (err == nil)
return
}
// Value implements the driver Valuer interface.
func (x NullCtxKey) Value() (driver.Value, error) {
if !x.Valid {
return nil, nil
}
// driver.Value accepts int64 for int values.
return string(x.CtxKey), nil
}
type NullCtxKeyStr struct {
NullCtxKey
}
func NewNullCtxKeyStr(val interface{}) (x NullCtxKeyStr) {
x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
return
}
// Value implements the driver Valuer interface.
func (x NullCtxKeyStr) Value() (driver.Value, error) {
if !x.Valid {
return nil, nil
}
return x.CtxKey.String(), nil
}

View File

@@ -0,0 +1,9 @@
package consts
// swagger:enum CacheKey
// ENUM(
// Tx = "__ctx_db:",
// Jwt = "__jwt_token:",
// Claim = "__jwt_claim:",
// )
type CtxKey string

13
backend/pkg/dao.go Executable file
View File

@@ -0,0 +1,13 @@
package common
func WrapLike(v string) string {
return "%" + v + "%"
}
func WrapLikeLeft(v string) string {
return "%" + v
}
func WrapLikeRight(v string) string {
return "%" + v
}

71
backend/pkg/data_structures.go Executable file
View File

@@ -0,0 +1,71 @@
package common
import (
"strings"
"github.com/samber/lo"
)
type SortQueryFilter struct {
Asc *string `json:"asc" form:"asc"`
Desc *string `json:"desc" form:"desc"`
}
func (s *SortQueryFilter) AscFields() []string {
if s.Asc == nil {
return nil
}
return strings.Split(*s.Asc, ",")
}
func (s *SortQueryFilter) DescFields() []string {
if s.Desc == nil {
return nil
}
return strings.Split(*s.Desc, ",")
}
func (s *SortQueryFilter) DescID() *SortQueryFilter {
if s.Desc == nil {
s.Desc = lo.ToPtr("id")
}
items := s.DescFields()
if lo.Contains(items, "id") {
return s
}
items = append(items, "id")
s.Desc = lo.ToPtr(strings.Join(items, ","))
return s
}
type PageDataResponse struct {
PageQueryFilter `json:",inline"`
Total int64 `json:"total"`
Items interface{} `json:"items"`
}
type PageQueryFilter struct {
Page int `json:"page" form:"page"`
Limit int `json:"limit" form:"limit"`
}
func (filter *PageQueryFilter) Offset() int {
return (filter.Page - 1) * filter.Limit
}
func (filter *PageQueryFilter) Format() *PageQueryFilter {
if filter.Page <= 0 {
filter.Page = 1
}
if filter.Limit <= 0 {
filter.Limit = 10
}
if filter.Limit > 50 {
filter.Limit = 50
}
return filter
}

View File

@@ -5,7 +5,7 @@ import (
"database/sql"
"fmt"
"backend/common/consts"
"backend/pkg/consts"
"github.com/go-jet/jet/v2/qrm"
)

View File

@@ -0,0 +1,19 @@
package errorx
import (
"fmt"
)
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (r Response) Error() string {
return fmt.Sprintf("%d: %s", r.Code, r.Message)
}
var (
RequestParseError = Response{400, "请求解析错误"}
InternalError = Response{500, "内部错误"}
)

View File

@@ -0,0 +1,107 @@
package media_store
import (
"encoding/json"
"os"
"path/filepath"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type Store []VideoInfo
type VideoInfo struct {
Hash string
Name string
Duration int64
}
// Price
func (info VideoInfo) Price() int64 {
min := int64(10)
// if duration is less than 300 seconds, return 10
if info.Duration < 5*60 {
return min * 100
}
// 每多一分钟,价格加 3 如果余数大于 30 秒,按一分钟计算
cells := (info.Duration - 5*30) / 60
if info.Duration%60 > 30 {
cells++
}
return (min + cells*3) * 100
}
func NewStore(path string) (Store, error) {
mapFile := filepath.Join(path, "map.json")
log.Infof("read in from: %s", mapFile)
if _, err := os.Stat(mapFile); err != nil {
if os.IsNotExist(err) {
if err := os.WriteFile(mapFile, []byte("[]"), os.ModePerm); err != nil {
return nil, errors.Wrapf(err, "write file: %s", mapFile)
}
}
}
b, err := os.ReadFile(mapFile)
if err != nil {
return nil, errors.Wrapf(err, "read file: %s", mapFile)
}
var store Store
err = json.Unmarshal(b, &store)
if err != nil {
return nil, errors.Wrapf(err, "unmarshal json: %s", mapFile)
}
return store, nil
}
func (s Store) Save(path string) error {
mapFile := filepath.Join(path, "map.json")
b, err := json.Marshal(s)
if err != nil {
return errors.Wrapf(err, "marshal json: %s", mapFile)
}
if err := os.WriteFile(mapFile, b, os.ModePerm); err != nil {
return errors.Wrapf(err, "write file: %s", mapFile)
}
return nil
}
func (s Store) Append(info VideoInfo) Store {
return append(s, info)
}
func (s Store) Hashes() []string {
var hashes []string
for _, m := range s {
hashes = append(hashes, m.Hash)
}
return hashes
}
// Exists
func (s Store) HashExists(hash string) (VideoInfo, bool) {
for _, m := range s {
if m.Hash == hash {
return m, true
}
}
return VideoInfo{}, false
}
func (s Store) Update(info VideoInfo) {
for i, m := range s {
if m.Hash == info.Hash {
s[i] = info
break
}
}
}

View File

@@ -2,6 +2,8 @@ package path
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
@@ -35,3 +37,10 @@ func DirExists(path string) bool {
}
return st.IsDir()
}
func SplitNameExt(name string) (string, string) {
ext := filepath.Ext(name)
name = name[:len(name)-len(ext)]
return name, strings.TrimLeft(ext, ".")
}

View File

@@ -0,0 +1,79 @@
package http
import (
"backend/modules/medias"
"backend/modules/middlewares"
"backend/modules/users"
"backend/providers/app"
"backend/providers/hashids"
"backend/providers/http"
"backend/providers/jwt"
"backend/providers/postgres"
"backend/providers/storage"
"backend/providers/wechat"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
func defaultProviders(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
app.DefaultProvider(),
http.DefaultProvider(),
postgres.DefaultProvider(),
jwt.DefaultProvider(),
hashids.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("serve"),
atom.Short("run http server"),
atom.RunE(Serve),
atom.Providers(defaultProviders(
storage.DefaultProvider(),
wechat.DefaultProvider(),
).With(
middlewares.Provide,
users.Provide,
medias.Provide,
)),
)
}
type Http struct {
dig.In
App *app.Config
Service *http.Service
Initials []contracts.Initial `group:"initials"`
Routes []contracts.HttpRoute `group:"routes"`
Middlewares *middlewares.Middlewares
}
func Serve(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(http Http) error {
if http.App.Mode == app.AppModeDevelopment {
log.SetLevel(log.DebugLevel)
}
mid := http.Middlewares
http.Service.Engine.Use(mid.DebugMode)
http.Service.Engine.Use(mid.WeChatVerify)
http.Service.Engine.Use(mid.WeChatAuthUserInfo)
http.Service.Engine.Use(mid.WeChatSilentAuth)
http.Service.Engine.Use(mid.ParseJWT)
group := http.Service.Engine.Group("/v1")
for _, route := range http.Routes {
route.Register(group)
}
return http.Service.Serve()
})
}

View File

@@ -0,0 +1,52 @@
package migrate
import (
"context"
"database/sql"
"backend/database"
"backend/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/pressly/goose/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
postgres.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("migrate"),
atom.Short("run migrations"),
atom.RunE(Serve),
atom.Providers(Default()),
)
}
type Migrate struct {
dig.In
DB *sql.DB
}
func Serve(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(migrate Migrate) error {
if len(args) == 0 {
args = append(args, "up")
}
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, migrate.DB, "migrations", args...)
})
}

View File

@@ -0,0 +1,129 @@
package model
import (
"database/sql"
"fmt"
"strings"
db "backend/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
pg "github.com/go-jet/jet/v2/postgres"
"github.com/gofiber/fiber/v3/log"
_ "github.com/lib/pq"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/dig"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
db.DefaultProvider(),
}, providers...)
}
func Options() []atom.Option {
return []atom.Option{
atom.Name("model"),
atom.Short("run model generator"),
atom.RunE(Serve),
atom.Providers(Default()),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("path", "./database/models", "generate to path")
cmd.Flags().String("transform", "./database/.transform.yaml", "transform config")
}),
}
}
func Command() atom.Option {
return atom.Command(Options()...)
}
type Migrate struct {
dig.In
DB *sql.DB
Config *db.Config
}
type Transformer struct {
Ignores []string `mapstructure:"ignores"`
Types map[string]map[string]string `mapstructure:"types"`
}
func Serve(cmd *cobra.Command, args []string) error {
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(cmd.Flag("transform").Value.String())
if err := v.ReadInConfig(); err != nil {
return err
}
var conf Transformer
if err := v.Unmarshal(&conf); err != nil {
return err
}
return container.Container.Invoke(func(migrate Migrate) error {
return postgres.GenerateDSN(
migrate.Config.DSN(),
migrate.Config.Schema,
cmd.Flag("path").Value.String(),
template.Default(pg.Dialect).
UseSchema(func(schema metadata.Schema) template.Schema {
return template.
DefaultSchema(schema).
UseModel(
template.
DefaultModel().
UseTable(func(table metadata.Table) template.TableModel {
if lo.Contains(conf.Ignores, table.Name) {
table := template.DefaultTableModel(table)
table.Skip = true
return table
}
return template.DefaultTableModel(table).UseField(func(column metadata.Column) template.TableModelField {
defaultTableModelField := template.DefaultTableModelField(column)
defaultTableModelField = defaultTableModelField.UseTags(fmt.Sprintf(`json:"%s"`, column.Name))
if schema.Name != migrate.Config.Schema {
return defaultTableModelField
}
fields, ok := conf.Types[table.Name]
if !ok {
return defaultTableModelField
}
toType, ok := fields[column.Name]
if !ok {
return defaultTableModelField
}
splits := strings.Split(toType, ".")
typeName := splits[len(splits)-1]
pkgSplits := strings.Split(splits[0], "/")
typePkg := pkgSplits[len(pkgSplits)-1]
defaultTableModelField = defaultTableModelField.
UseType(template.Type{
Name: fmt.Sprintf("%s.%s", typePkg, typeName),
ImportPath: splits[0],
})
log.Infof("Convert table %s field %s type to : %s", table.Name, column.Name, toType)
return defaultTableModelField
})
}),
)
}),
)
})
}

View File

@@ -0,0 +1,62 @@
package tasks
import (
"backend/modules/commands/discover"
"backend/modules/commands/store"
"backend/modules/medias"
"backend/providers/app"
"backend/providers/postgres"
"backend/providers/storage"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"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("tasks"),
atom.Short("run tasks"),
atom.Command(
atom.Name("discover"),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("from", "", "from path")
cmd.Flags().String("to", "", "to path")
}),
atom.Providers(defaultProviders().With(
medias.Provide,
discover.Provide,
)),
atom.RunE(func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(task *discover.DiscoverMedias) error {
from, to := cmd.Flag("from").Value.String(), cmd.Flag("to").Value.String()
return task.RunE(from, to)
})
}),
),
atom.Command(
atom.Name("store"),
atom.Providers(defaultProviders().With(
medias.Provide,
store.Provide,
)),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("from", "", "from path")
}),
atom.RunE(func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(task *store.StoreMedias) error {
from := cmd.Flag("from").Value.String()
return task.RunE(from)
})
}),
),
)
}

View File

@@ -0,0 +1,74 @@
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("租户相关操作"),
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.Short("设置用户的过期时间"),
atom.Example("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,29 @@
package testx
import (
"os"
"testing"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/rogeecn/fabfile"
. "github.com/smartystreets/goconvey/convey"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{}, providers...)
}
func Serve(providers container.Providers, t *testing.T, invoke any) {
Convey("tests boot up", t, func() {
file := fabfile.MustFind("config.toml")
localEnv := os.Getenv("ENV_LOCAL")
if localEnv != "" {
file = fabfile.MustFind("config." + localEnv + ".toml")
}
So(atom.LoadProviders(file, providers), ShouldBeNil)
So(container.Container.Invoke(invoke), ShouldBeNil)
})
}

1
backend/pkg/session.go Normal file
View File

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