2023-06-15 09:41:30 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"chatplus/core"
|
|
|
|
"chatplus/core/types"
|
|
|
|
"chatplus/handler"
|
2023-06-19 07:06:59 +08:00
|
|
|
"chatplus/handler/admin"
|
2023-10-11 15:46:40 +08:00
|
|
|
"chatplus/handler/chatimpl"
|
2023-06-15 09:41:30 +08:00
|
|
|
logger2 "chatplus/logger"
|
2023-07-02 20:51:13 +08:00
|
|
|
"chatplus/service"
|
2023-09-27 18:14:07 +08:00
|
|
|
"chatplus/service/fun"
|
|
|
|
"chatplus/service/mj"
|
2023-08-20 22:29:08 +08:00
|
|
|
"chatplus/service/oss"
|
2023-09-28 18:09:45 +08:00
|
|
|
"chatplus/service/sd"
|
2023-09-27 18:14:07 +08:00
|
|
|
"chatplus/service/wx"
|
2023-06-15 09:41:30 +08:00
|
|
|
"chatplus/store"
|
|
|
|
"context"
|
2023-06-22 11:08:44 +08:00
|
|
|
"embed"
|
2023-09-05 11:47:03 +08:00
|
|
|
"github.com/go-redis/redis/v8"
|
2023-06-22 11:08:44 +08:00
|
|
|
"io"
|
2023-06-15 09:41:30 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-06-27 18:29:46 +08:00
|
|
|
"strconv"
|
2023-06-15 09:41:30 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2023-06-19 07:06:59 +08:00
|
|
|
|
|
|
|
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"gorm.io/gorm"
|
2023-06-15 09:41:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger = logger2.GetLogger()
|
|
|
|
|
2023-06-22 11:08:44 +08:00
|
|
|
//go:embed res/ip2region.xdb
|
|
|
|
var xdbFS embed.FS
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
// AppLifecycle 应用程序生命周期
|
|
|
|
type AppLifecycle struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnStart 应用程序启动时执行
|
|
|
|
func (l *AppLifecycle) OnStart(context.Context) error {
|
|
|
|
log.Println("AppLifecycle OnStart")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnStop 应用程序停止时执行
|
|
|
|
func (l *AppLifecycle) OnStop(context.Context) error {
|
|
|
|
log.Println("AppLifecycle OnStop")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-06-27 18:29:46 +08:00
|
|
|
configFile := os.Getenv("CONFIG_FILE")
|
2023-06-28 05:51:55 +08:00
|
|
|
if configFile == "" {
|
|
|
|
configFile = "config.toml"
|
|
|
|
}
|
|
|
|
var debug bool
|
|
|
|
debugEnv := os.Getenv("DEBUG")
|
|
|
|
if debugEnv == "" {
|
|
|
|
debug = true
|
|
|
|
} else {
|
|
|
|
debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
|
|
|
|
}
|
2023-06-15 09:41:30 +08:00
|
|
|
logger.Info("Loading config file: ", configFile)
|
2023-06-25 11:34:55 +08:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
logger.Error("Panic Error:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
app := fx.New(
|
|
|
|
// 初始化配置应用配置
|
|
|
|
fx.Provide(func() *types.AppConfig {
|
|
|
|
config, err := core.LoadConfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-27 18:29:46 +08:00
|
|
|
config.Path = configFile
|
2023-07-25 15:02:43 +08:00
|
|
|
if debug {
|
|
|
|
_ = core.SaveConfig(config)
|
|
|
|
}
|
2023-06-15 09:41:30 +08:00
|
|
|
return config
|
|
|
|
}),
|
|
|
|
// 创建应用服务
|
|
|
|
fx.Provide(core.NewServer),
|
|
|
|
// 初始化
|
2023-09-05 11:47:03 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, client *redis.Client) {
|
|
|
|
s.Init(debug, client)
|
2023-06-15 09:41:30 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
// 初始化数据库
|
|
|
|
fx.Provide(store.NewGormConfig),
|
|
|
|
fx.Provide(store.NewMysql),
|
2023-06-19 18:23:09 +08:00
|
|
|
fx.Provide(store.NewLevelDB),
|
2023-09-04 06:43:15 +08:00
|
|
|
fx.Provide(store.NewRedisClient),
|
2023-06-15 09:41:30 +08:00
|
|
|
|
|
|
|
// 创建 Ip2Region 查询对象
|
|
|
|
fx.Provide(func() (*xdb.Searcher, error) {
|
2023-06-22 11:08:44 +08:00
|
|
|
file, err := xdbFS.Open("res/ip2region.xdb")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cBuff, err := io.ReadAll(file)
|
2023-06-15 09:41:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return xdb.NewWithBuffer(cBuff)
|
|
|
|
}),
|
|
|
|
|
2023-07-10 18:59:53 +08:00
|
|
|
// 创建函数
|
2023-09-27 18:14:07 +08:00
|
|
|
fx.Provide(fun.NewFunctions),
|
2023-07-10 18:59:53 +08:00
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
// 创建控制器
|
|
|
|
fx.Provide(handler.NewChatRoleHandler),
|
|
|
|
fx.Provide(handler.NewUserHandler),
|
2023-10-11 15:46:40 +08:00
|
|
|
fx.Provide(chatimpl.NewChatHandler),
|
2023-06-27 12:11:55 +08:00
|
|
|
fx.Provide(handler.NewUploadHandler),
|
2023-07-25 17:00:24 +08:00
|
|
|
fx.Provide(handler.NewSmsHandler),
|
2023-07-21 18:26:51 +08:00
|
|
|
fx.Provide(handler.NewRewardHandler),
|
2023-07-25 17:00:24 +08:00
|
|
|
fx.Provide(handler.NewCaptchaHandler),
|
2023-08-11 18:46:56 +08:00
|
|
|
fx.Provide(handler.NewMidJourneyHandler),
|
2023-09-04 16:32:20 +08:00
|
|
|
fx.Provide(handler.NewChatModelHandler),
|
2023-09-28 18:09:45 +08:00
|
|
|
fx.Provide(handler.NewSdJobHandler),
|
2023-06-15 09:41:30 +08:00
|
|
|
|
2023-06-27 12:11:55 +08:00
|
|
|
fx.Provide(admin.NewConfigHandler),
|
2023-06-19 07:06:59 +08:00
|
|
|
fx.Provide(admin.NewAdminHandler),
|
|
|
|
fx.Provide(admin.NewApiKeyHandler),
|
2023-06-19 18:23:09 +08:00
|
|
|
fx.Provide(admin.NewUserHandler),
|
|
|
|
fx.Provide(admin.NewChatRoleHandler),
|
2023-07-24 15:59:29 +08:00
|
|
|
fx.Provide(admin.NewRewardHandler),
|
2023-08-02 16:37:47 +08:00
|
|
|
fx.Provide(admin.NewDashboardHandler),
|
2023-09-04 16:32:20 +08:00
|
|
|
fx.Provide(admin.NewChatModelHandler),
|
2023-06-19 07:06:59 +08:00
|
|
|
|
2023-07-02 20:51:13 +08:00
|
|
|
// 创建服务
|
|
|
|
fx.Provide(service.NewAliYunSmsService),
|
2023-07-25 17:00:24 +08:00
|
|
|
fx.Provide(func(config *types.AppConfig) *service.CaptchaService {
|
|
|
|
return service.NewCaptchaService(config.ApiConfig)
|
|
|
|
}),
|
2023-08-20 22:29:08 +08:00
|
|
|
fx.Provide(oss.NewUploaderManager),
|
2023-09-27 18:14:07 +08:00
|
|
|
fx.Provide(mj.NewService),
|
2023-07-02 20:51:13 +08:00
|
|
|
|
2023-09-27 18:14:07 +08:00
|
|
|
// 微信机器人服务
|
|
|
|
fx.Provide(wx.NewWeChatBot),
|
|
|
|
fx.Invoke(func(config *types.AppConfig, bot *wx.Bot) {
|
|
|
|
if config.WeChatBot {
|
|
|
|
err := bot.Run()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("微信登录失败:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
// MidJourney 机器人
|
|
|
|
fx.Provide(mj.NewBot),
|
|
|
|
fx.Provide(mj.NewClient),
|
|
|
|
fx.Invoke(func(config *types.AppConfig, bot *mj.Bot) {
|
|
|
|
if config.MjConfig.Enabled {
|
|
|
|
err := bot.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("MidJourney 服务启动失败:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2023-10-09 12:05:51 +08:00
|
|
|
fx.Invoke(func(config *types.AppConfig, mjService *mj.Service) {
|
|
|
|
if config.MjConfig.Enabled {
|
|
|
|
go func() {
|
|
|
|
mjService.Run()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}),
|
2023-09-27 18:14:07 +08:00
|
|
|
|
2023-09-28 18:09:45 +08:00
|
|
|
// Stable Diffusion 机器人
|
|
|
|
fx.Provide(sd.NewService),
|
2023-10-09 12:05:51 +08:00
|
|
|
fx.Invoke(func(config *types.AppConfig, service *sd.Service) {
|
|
|
|
if config.SdConfig.Enabled {
|
|
|
|
go func() {
|
|
|
|
service.Run()
|
|
|
|
}()
|
|
|
|
}
|
2023-09-28 18:09:45 +08:00
|
|
|
}),
|
2023-06-15 09:41:30 +08:00
|
|
|
// 注册路由
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.ChatRoleHandler) {
|
2023-06-20 11:46:13 +08:00
|
|
|
group := s.Engine.Group("/api/role/")
|
2023-06-15 09:41:30 +08:00
|
|
|
group.GET("list", h.List)
|
2023-10-16 10:46:10 +08:00
|
|
|
group.POST("update", h.UpdateRole)
|
2023-06-15 09:41:30 +08:00
|
|
|
}),
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.UserHandler) {
|
|
|
|
group := s.Engine.Group("/api/user/")
|
|
|
|
group.POST("register", h.Register)
|
|
|
|
group.POST("login", h.Login)
|
|
|
|
group.GET("logout", h.Logout)
|
|
|
|
group.GET("session", h.Session)
|
|
|
|
group.GET("profile", h.Profile)
|
|
|
|
group.POST("profile/update", h.ProfileUpdate)
|
2023-06-16 17:28:21 +08:00
|
|
|
group.POST("password", h.Password)
|
2023-07-03 15:18:15 +08:00
|
|
|
group.POST("bind/mobile", h.BindMobile)
|
2023-06-15 09:41:30 +08:00
|
|
|
}),
|
2023-10-11 15:46:40 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *chatimpl.ChatHandler) {
|
2023-06-15 09:41:30 +08:00
|
|
|
group := s.Engine.Group("/api/chat/")
|
|
|
|
group.Any("new", h.ChatHandle)
|
|
|
|
group.GET("list", h.List)
|
2023-08-04 12:08:07 +08:00
|
|
|
group.GET("detail", h.Detail)
|
2023-06-15 09:41:30 +08:00
|
|
|
group.POST("update", h.Update)
|
|
|
|
group.GET("remove", h.Remove)
|
|
|
|
group.GET("history", h.History)
|
|
|
|
group.GET("clear", h.Clear)
|
2023-09-11 13:34:20 +08:00
|
|
|
group.POST("tokens", h.Tokens)
|
2023-06-15 09:41:30 +08:00
|
|
|
group.GET("stop", h.StopGenerate)
|
|
|
|
}),
|
2023-06-27 12:11:55 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.UploadHandler) {
|
|
|
|
s.Engine.POST("/api/upload", h.Upload)
|
|
|
|
}),
|
2023-07-25 17:00:24 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.SmsHandler) {
|
|
|
|
group := s.Engine.Group("/api/sms/")
|
|
|
|
group.GET("status", h.Status)
|
2023-07-27 10:53:14 +08:00
|
|
|
group.POST("code", h.SendCode)
|
2023-07-25 17:00:24 +08:00
|
|
|
}),
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.CaptchaHandler) {
|
|
|
|
group := s.Engine.Group("/api/captcha/")
|
|
|
|
group.GET("get", h.Get)
|
|
|
|
group.POST("check", h.Check)
|
2023-07-02 20:51:13 +08:00
|
|
|
}),
|
2023-07-21 18:26:51 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.RewardHandler) {
|
|
|
|
group := s.Engine.Group("/api/reward/")
|
2023-07-21 22:29:14 +08:00
|
|
|
group.POST("verify", h.Verify)
|
2023-07-21 18:26:51 +08:00
|
|
|
}),
|
2023-08-11 18:46:56 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.MidJourneyHandler) {
|
2023-09-12 18:01:24 +08:00
|
|
|
group := s.Engine.Group("/api/mj/")
|
2023-09-17 18:03:45 +08:00
|
|
|
group.POST("image", h.Image)
|
2023-09-12 18:01:24 +08:00
|
|
|
group.POST("upscale", h.Upscale)
|
|
|
|
group.POST("variation", h.Variation)
|
2023-09-15 17:40:39 +08:00
|
|
|
group.GET("jobs", h.JobList)
|
2023-09-20 06:59:30 +08:00
|
|
|
group.Any("client", h.Client)
|
2023-08-11 18:46:56 +08:00
|
|
|
}),
|
2023-09-28 18:09:45 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.SdJobHandler) {
|
|
|
|
group := s.Engine.Group("/api/sd")
|
|
|
|
group.POST("image", h.Image)
|
|
|
|
group.GET("jobs", h.JobList)
|
|
|
|
group.Any("client", h.Client)
|
|
|
|
}),
|
2023-06-19 15:58:52 +08:00
|
|
|
|
2023-06-27 12:11:55 +08:00
|
|
|
// 管理后台控制器
|
2023-06-19 15:58:52 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.ConfigHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/config/")
|
2023-06-15 09:41:30 +08:00
|
|
|
group.POST("update", h.Update)
|
|
|
|
group.GET("get", h.Get)
|
|
|
|
}),
|
2023-06-19 07:06:59 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.ManagerHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/")
|
|
|
|
group.POST("login", h.Login)
|
|
|
|
group.GET("logout", h.Logout)
|
2023-06-19 11:09:23 +08:00
|
|
|
group.GET("session", h.Session)
|
2023-06-22 11:08:44 +08:00
|
|
|
group.GET("migrate", h.Migrate)
|
2023-06-19 07:06:59 +08:00
|
|
|
}),
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.ApiKeyHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/apikey/")
|
2023-06-20 18:05:33 +08:00
|
|
|
group.POST("save", h.Save)
|
2023-06-19 07:06:59 +08:00
|
|
|
group.GET("list", h.List)
|
2023-06-20 18:05:33 +08:00
|
|
|
group.GET("remove", h.Remove)
|
2023-06-19 07:06:59 +08:00
|
|
|
}),
|
2023-06-19 18:23:09 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.UserHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/user/")
|
|
|
|
group.GET("list", h.List)
|
2023-08-01 16:02:49 +08:00
|
|
|
group.POST("save", h.Save)
|
2023-06-20 07:08:37 +08:00
|
|
|
group.GET("remove", h.Remove)
|
2023-06-21 06:53:41 +08:00
|
|
|
group.GET("loginLog", h.LoginLog)
|
2023-07-26 15:22:11 +08:00
|
|
|
group.POST("resetPass", h.ResetPass)
|
2023-06-19 18:23:09 +08:00
|
|
|
}),
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.ChatRoleHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/role/")
|
|
|
|
group.GET("list", h.List)
|
2023-06-20 18:05:33 +08:00
|
|
|
group.POST("save", h.Save)
|
2023-09-04 16:32:20 +08:00
|
|
|
group.POST("sort", h.Sort)
|
2023-06-20 11:46:13 +08:00
|
|
|
group.GET("remove", h.Remove)
|
2023-06-19 18:23:09 +08:00
|
|
|
}),
|
2023-07-24 15:59:29 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.RewardHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/reward/")
|
|
|
|
group.GET("list", h.List)
|
|
|
|
}),
|
2023-08-02 16:37:47 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.DashboardHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/dashboard/")
|
|
|
|
group.GET("stats", h.Stats)
|
|
|
|
}),
|
2023-09-04 16:32:20 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, h *handler.ChatModelHandler) {
|
|
|
|
group := s.Engine.Group("/api/model/")
|
|
|
|
group.GET("list", h.List)
|
|
|
|
}),
|
|
|
|
fx.Invoke(func(s *core.AppServer, h *admin.ChatModelHandler) {
|
|
|
|
group := s.Engine.Group("/api/admin/model/")
|
|
|
|
group.POST("save", h.Save)
|
|
|
|
group.GET("list", h.List)
|
|
|
|
group.POST("enable", h.Enable)
|
|
|
|
group.POST("sort", h.Sort)
|
|
|
|
group.GET("remove", h.Remove)
|
|
|
|
}),
|
2023-06-19 07:06:59 +08:00
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
fx.Invoke(func(s *core.AppServer, db *gorm.DB) {
|
|
|
|
err := s.Run(db)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
// 注册生命周期回调函数
|
|
|
|
fx.Invoke(func(lifecycle fx.Lifecycle, lc *AppLifecycle) {
|
|
|
|
lifecycle.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
return lc.OnStart(ctx)
|
|
|
|
},
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return lc.OnStop(ctx)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
// 启动应用程序
|
|
|
|
go func() {
|
|
|
|
if err := app.Start(context.Background()); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 监听退出信号
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-quit
|
|
|
|
|
|
|
|
// 关闭应用程序
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := app.Stop(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|