init project
This commit is contained in:
1 parent
d63cc8d9f5
commit
ee07869839
39 files changed
+1560
-622
No files matched your search
@@ -2,11 +2,12 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.xiaotuo.co/micro/skeleton/internal/queue/jetstream"
|
||||
"git.xiaotuo.co/pkg/go-plugins/logger"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"micro_layout/internal/logger"
|
||||
"micro_layout/internal/queue/jetstream"
|
||||
)
|
||||
|
||||
type MessageLogic struct {
|
||||
@@ -22,6 +23,9 @@ func (lgc MessageLogic) jetStreamPublish(ctx context.Context, topic string, body
|
||||
WithField("topic", topic).
|
||||
WithField("body", string(body)).
|
||||
Debug("start")
|
||||
if Resources == nil || Resources.JetStreamBroker == nil {
|
||||
return errors.New("jetstream is disabled")
|
||||
}
|
||||
ack, err := Resources.JetStreamBroker.PublishMsg(jetstream.NewMsg(ctx, topic, body))
|
||||
if err != nil {
|
||||
log.WithFields(logger.WithContext(ctx)).
|
||||
|
||||
+18
-35
@@ -1,68 +1,51 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"git.xiaotuo.co/micro/skeleton/internal/storage"
|
||||
"github.com/nats-io/nats.go"
|
||||
"micro_layout/internal/storage"
|
||||
)
|
||||
|
||||
var Resources *resources
|
||||
|
||||
type resources struct {
|
||||
Env string
|
||||
Server string
|
||||
Storage *storage.Storage
|
||||
JetStreamBroker nats.JetStreamContext
|
||||
|
||||
MessageLogic MessageLogic
|
||||
MessageLogic MessageLogic
|
||||
UserLogic *UserLogic
|
||||
}
|
||||
|
||||
func Init(opts ...Option) error {
|
||||
options := &Options{}
|
||||
for _, o := range opts {
|
||||
o(options)
|
||||
for _, option := range opts {
|
||||
option(options)
|
||||
}
|
||||
|
||||
Resources = &resources{
|
||||
Env: options.env,
|
||||
Server: options.server,
|
||||
|
||||
JetStreamBroker: options.jetStreamBr,
|
||||
Env: options.env,
|
||||
Storage: options.storage,
|
||||
JetStreamBroker: options.jetStream,
|
||||
MessageLogic: newMessageLogic(),
|
||||
UserLogic: newUserLogic(),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
type Option func(*Options)
|
||||
|
||||
type Options struct {
|
||||
env string
|
||||
server string
|
||||
etcdServer string
|
||||
st *storage.Storage
|
||||
jetStreamBr nats.JetStreamContext
|
||||
tiktokAuthGateway string
|
||||
env string
|
||||
storage *storage.Storage
|
||||
jetStream nats.JetStreamContext
|
||||
}
|
||||
|
||||
func WithEnv(env string) Option {
|
||||
return func(o *Options) {
|
||||
o.env = env
|
||||
}
|
||||
return func(options *Options) { options.env = env }
|
||||
}
|
||||
|
||||
func WithStorage(st *storage.Storage) Option {
|
||||
return func(o *Options) {
|
||||
o.st = st
|
||||
}
|
||||
return func(options *Options) { options.storage = st }
|
||||
}
|
||||
|
||||
func WithEtcdServer(etcdServer string) Option {
|
||||
return func(o *Options) {
|
||||
o.etcdServer = etcdServer
|
||||
}
|
||||
}
|
||||
|
||||
func WithJetStreamBroker(br nats.JetStreamContext) Option {
|
||||
return func(o *Options) {
|
||||
o.jetStreamBr = br
|
||||
}
|
||||
func WithJetStreamBroker(broker nats.JetStreamContext) Option {
|
||||
return func(options *Options) { options.jetStream = broker }
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
|
||||
"micro_layout/internal/storage/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserLogic struct {
|
||||
mutex sync.RWMutex
|
||||
nextID int64
|
||||
users map[int64]model.User
|
||||
}
|
||||
|
||||
func newUserLogic() *UserLogic {
|
||||
return &UserLogic{
|
||||
nextID: 2,
|
||||
users: map[int64]model.User{1: {BaseModel: model.BaseModel{ID: 1}, Name: "demo", Status: "active"}},
|
||||
}
|
||||
}
|
||||
|
||||
var ErrInvalidUser = errors.New("name must contain between 1 and 100 characters")
|
||||
|
||||
func (logic *UserLogic) Create(ctx context.Context, name string) (model.User, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || utf8.RuneCountInString(name) > 100 {
|
||||
return model.User{}, ErrInvalidUser
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
item := model.User{Name: name, Status: "active"}
|
||||
if database := Resources.Storage.DB(); database != nil {
|
||||
err := database.WithContext(ctx).Create(&item).Error
|
||||
return item, err
|
||||
}
|
||||
logic.mutex.Lock()
|
||||
defer logic.mutex.Unlock()
|
||||
item.ID = logic.nextID
|
||||
logic.nextID++
|
||||
logic.users[item.ID] = item
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (logic *UserLogic) QueryInfo(ctx context.Context, id int64) (model.User, bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return model.User{}, false, err
|
||||
}
|
||||
if database := Resources.Storage.DB(); database != nil {
|
||||
var item model.User
|
||||
err := database.WithContext(ctx).First(&item, id).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return model.User{}, false, nil
|
||||
}
|
||||
return item, err == nil, err
|
||||
}
|
||||
logic.mutex.RLock()
|
||||
defer logic.mutex.RUnlock()
|
||||
item, exists := logic.users[id]
|
||||
return item, exists, nil
|
||||
}
|
||||
Reference in new issue
Block a user