feat: migrate workbench services
This commit is contained in:
+23
-54
@@ -5,72 +5,47 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.118im.com/im_team/im_workbench/internal/ai"
|
||||
"git.118im.com/im_team/im_workbench/internal/accounts"
|
||||
"git.118im.com/im_team/im_workbench/internal/auth"
|
||||
"git.118im.com/im_team/im_workbench/internal/config"
|
||||
"git.118im.com/im_team/im_workbench/internal/events"
|
||||
"git.118im.com/im_team/im_workbench/internal/finance"
|
||||
"git.118im.com/im_team/im_workbench/internal/httpapi"
|
||||
"git.118im.com/im_team/im_workbench/internal/managerconfig"
|
||||
"git.118im.com/im_team/im_workbench/internal/mall"
|
||||
"git.118im.com/im_team/im_workbench/internal/portal"
|
||||
"git.118im.com/im_team/im_workbench/internal/store"
|
||||
"git.118im.com/im_team/im_workbench/internal/tasks"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
store *store.Store
|
||||
redis *redis.Client
|
||||
events events.Publisher
|
||||
api *httpapi.API
|
||||
cancel context.CancelFunc
|
||||
store *store.Store
|
||||
redis *redis.Client
|
||||
api *httpapi.API
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) (*App, error) {
|
||||
uidKey := managerconfig.ResolveUIDKey(context.Background(), cfg.Manager, cfg.Auth.UIDKey)
|
||||
if err := httpapi.SetUIDKey(uidKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := store.SetUIDKey(uidKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.AI = managerconfig.ResolveAIConfig(context.Background(), cfg.Manager, cfg.AI)
|
||||
st, err := store.Open(cfg.MySQL, cfg.CoreMySQL)
|
||||
st, err := store.Open(cfg.MySQL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var redisClient *redis.Client
|
||||
if !cfg.Auth.SkipSessionInDev {
|
||||
redisClient = redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Redis.Addr,
|
||||
Username: cfg.Redis.Username,
|
||||
Password: cfg.Redis.Password,
|
||||
DB: cfg.Redis.DB,
|
||||
})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
_ = st.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
publisher, err := events.NewKafkaPublisher(cfg.Kafka)
|
||||
if err != nil {
|
||||
if redisClient != nil {
|
||||
_ = redisClient.Close()
|
||||
}
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Redis.Addr, Username: cfg.Redis.Username, Password: cfg.Redis.Password, DB: cfg.Redis.DB,
|
||||
})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
_ = redisClient.Close()
|
||||
_ = st.Close()
|
||||
return nil, err
|
||||
}
|
||||
aiService := ai.NewService(ai.NewClient(cfg.AI))
|
||||
api := httpapi.New(st, auth.New(cfg.Auth, redisClient), publisher, cfg.Auth, cfg.Manager, aiService)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
api.StartAIWorkers(ctx)
|
||||
return &App{
|
||||
store: st,
|
||||
redis: redisClient,
|
||||
events: publisher,
|
||||
api: api,
|
||||
cancel: cancel,
|
||||
}, nil
|
||||
authenticator := auth.New(cfg.Auth, redisClient)
|
||||
accountService := accounts.New(st, redisClient, cfg.Auth)
|
||||
portalService := portal.New(st)
|
||||
taskService := tasks.New(st)
|
||||
mallService := mall.New(st)
|
||||
financeService := finance.New(st, redisClient, cfg.Auth)
|
||||
return &App{store: st, redis: redisClient, api: httpapi.New(authenticator, accountService, portalService, taskService, mallService, financeService)}, nil
|
||||
}
|
||||
|
||||
func (a *App) Handler() http.Handler {
|
||||
@@ -82,15 +57,9 @@ func (a *App) Handler() http.Handler {
|
||||
}
|
||||
|
||||
func (a *App) Close() {
|
||||
if a.cancel != nil {
|
||||
a.cancel()
|
||||
}
|
||||
if a.redis != nil {
|
||||
_ = a.redis.Close()
|
||||
}
|
||||
if a.events != nil {
|
||||
_ = a.events.Close()
|
||||
}
|
||||
if a.store != nil {
|
||||
_ = a.store.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user