feat: init repo

This commit is contained in:
Rogee
2024-12-18 19:42:10 +08:00
parent c3f965035c
commit b68316a778
46 changed files with 1770 additions and 0 deletions

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

View File

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

View File

@@ -0,0 +1,71 @@
package pkg
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

@@ -0,0 +1,28 @@
package db
import (
"context"
"database/sql"
"fmt"
"{{.ModuleName}}/pkg/consts"
"github.com/go-jet/jet/v2/qrm"
)
func FromContext(ctx context.Context, db *sql.DB) qrm.DB {
if tx, ok := ctx.Value(consts.CtxKeyTx).(*sql.Tx); ok {
return tx
}
return db
}
func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) error {
for _, name := range tableName {
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
if _, err := db.ExecContext(ctx, sql); err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,7 @@
package db
type Pagination struct {
Offset string `json:"offset,omitempty"`
OffsetID int64 `json:"-"`
Action int `json:"action"` // action: 0 : 1:
}

View File

@@ -0,0 +1,31 @@
package errorx
import (
"fmt"
"net/http"
"github.com/gofiber/fiber/v3"
)
type Response struct {
StatusCode int `json:"-"`
Code int `json:"code"`
Message string `json:"message"`
}
func Wrap(err error) Response {
return Response{http.StatusInternalServerError, http.StatusInternalServerError, err.Error()}
}
func (r Response) Error() string {
return fmt.Sprintf("[%d] %s", r.Code, r.Message)
}
func (r Response) Response(ctx fiber.Ctx) error {
return ctx.Status(r.StatusCode).JSON(r)
}
var (
RequestParseError = Response{http.StatusBadRequest, http.StatusBadRequest, "请求解析错误"}
InternalError = Response{http.StatusInternalServerError, http.StatusInternalServerError, "内部错误"}
)

View File

View File

@@ -0,0 +1,65 @@
package http
import (
"{{.ModuleName}}/pkg/service"
"{{.ModuleName}}/providers/app"
"{{.ModuleName}}/providers/hashids"
"{{.ModuleName}}/providers/http"
"{{.ModuleName}}/providers/jwt"
"{{.ModuleName}}/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
"github.com/gofiber/fiber/v3/middleware/favicon"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
func defaultProviders() container.Providers {
return service.Default(container.Providers{
http.DefaultProvider(),
postgres.DefaultProvider(),
jwt.DefaultProvider(),
hashids.DefaultProvider(),
}...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("serve"),
atom.Short("run http server"),
atom.RunE(Serve),
atom.Providers(defaultProviders()),
)
}
type Http struct {
dig.In
App *app.Config
Service *http.Service
Initials []contracts.Initial `group:"initials"`
Routes []contracts.HttpRoute `group:"routes"`
}
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)
}
http.Service.Engine.Use(favicon.New(favicon.Config{
Data: []byte{},
}))
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"
"{{.ModuleName}}/database"
"{{.ModuleName}}/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 "{{.ModuleName}}/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,13 @@
package service
import (
"{{.ModuleName}}/providers/app"
"git.ipao.vip/rogeecn/atom/container"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
app.DefaultProvider(),
}, providers...)
}

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)
})
}

View File

@@ -0,0 +1,26 @@
package utils
import (
"bufio"
"io"
)
// NewLogBuffer creates a buffer that can be used to capture output stream
// and write to a logger in real time
func NewLogBuffer(output func(string)) io.Writer {
reader, writer := io.Pipe()
go func() {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
output(scanner.Text())
}
}()
return writer
}
// NewCombinedBuffer combines multiple io.Writers
func NewCombinedBuffer(writers ...io.Writer) io.Writer {
return io.MultiWriter(writers...)
}