init project

This commit is contained in:
admin committed 2025-12-05 17:23:29 +08:00
commit d63cc8d9f5
33 files changed
+1529

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
package logic
import (
"context"
"time"
"git.xiaotuo.co/micro/skeleton/internal/queue/jetstream"
"git.xiaotuo.co/pkg/go-plugins/logger"
log "github.com/sirupsen/logrus"
)
type MessageLogic struct {
}
func newMessageLogic() MessageLogic {
return MessageLogic{}
}
func (lgc MessageLogic) jetStreamPublish(ctx context.Context, topic string, body []byte) (err error) {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
Debug("start")
ack, err := Resources.JetStreamBroker.PublishMsg(jetstream.NewMsg(ctx, topic, body))
if err != nil {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
Errorf("failed: %v", err)
} else {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
WithField("ack_sequence", ack.Sequence).
WithField("ack_duplicate", ack.Duplicate).
Debugf("success")
}
return err
}
func (lgc MessageLogic) QueueReceiveStartLog(ctx context.Context, topic string, payload []byte, reply string) {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("body", string(payload)).
WithField("reply", reply).
Debug("start")
}
func (lgc MessageLogic) QueueReceiveEndLog(ctx context.Context, topic string, err error, startTime time.Time, notifyTime, maxTime time.Duration) {
runTime := time.Now().Unix() - startTime.Unix()
slow := false
if float64(runTime) >= notifyTime.Seconds() {
slow = true
}
if err != nil {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("run_time", runTime).
WithField("max_time", maxTime.Seconds()).
WithField("slow", slow).
Errorf("failed: %v", err)
return
}
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("run_time", runTime).
WithField("max_time", maxTime.Seconds()).
WithField("slow", slow).
Debugf("success")
return
}
+68
View File
@@ -0,0 +1,68 @@
package logic
import (
"git.xiaotuo.co/micro/skeleton/internal/storage"
"github.com/nats-io/nats.go"
)
var Resources *resources
type resources struct {
Env string
Server string
JetStreamBroker nats.JetStreamContext
MessageLogic MessageLogic
}
func Init(opts ...Option) error {
options := &Options{}
for _, o := range opts {
o(options)
}
Resources = &resources{
Env: options.env,
Server: options.server,
JetStreamBroker: options.jetStreamBr,
MessageLogic: newMessageLogic(),
}
return nil
}
type Option func(o *Options)
type Options struct {
env string
server string
etcdServer string
st *storage.Storage
jetStreamBr nats.JetStreamContext
tiktokAuthGateway string
}
func WithEnv(env string) Option {
return func(o *Options) {
o.env = env
}
}
func WithStorage(st *storage.Storage) Option {
return func(o *Options) {
o.st = 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
}
}