geekai/api/main.go

261 lines
6.6 KiB
Go
Raw Normal View History

package main
import (
"chatplus/core"
"chatplus/core/types"
"chatplus/handler"
"chatplus/handler/admin"
logger2 "chatplus/logger"
2023-07-20 17:46:32 +08:00
"chatplus/modules/wexin"
"chatplus/service"
2023-07-10 18:59:53 +08:00
"chatplus/service/function"
"chatplus/store"
"context"
2023-06-22 11:08:44 +08:00
"embed"
"io"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
"go.uber.org/fx"
"gorm.io/gorm"
)
var logger = logger2.GetLogger()
2023-06-22 11:08:44 +08:00
//go:embed res/ip2region.xdb
var xdbFS embed.FS
// 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() {
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"))
}
logger.Info("Loading config file: ", configFile)
defer func() {
if err := recover(); err != nil {
logger.Error("Panic Error:", err)
}
}()
app := fx.New(
// 初始化配置应用配置
fx.Provide(func() *types.AppConfig {
config, err := core.LoadConfig(configFile)
if err != nil {
log.Fatal(err)
}
config.Path = configFile
return config
}),
// 创建应用服务
fx.Provide(core.NewServer),
// 初始化
fx.Invoke(func(s *core.AppServer) {
s.Init(debug)
}),
// 初始化数据库
fx.Provide(store.NewGormConfig),
fx.Provide(store.NewMysql),
fx.Provide(store.NewLevelDB),
// 创建 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)
if err != nil {
return nil, err
}
return xdb.NewWithBuffer(cBuff)
}),
2023-07-20 17:46:32 +08:00
// 创建微信机器人
fx.Provide(wexin.NewWeChatBot),
2023-07-21 18:26:51 +08:00
fx.Invoke(func(bot *wexin.WeChatBot) {
go func() {
err := bot.Login()
2023-07-21 22:29:14 +08:00
if err != nil {
log.Fatal(err)
}
2023-07-21 18:26:51 +08:00
}()
2023-07-20 17:46:32 +08:00
}),
2023-07-10 18:59:53 +08:00
// 创建函数
fx.Provide(func(config *types.AppConfig) (function.FuncZaoBao, error) {
return function.NewZaoBao(config.FunApiToken), nil
2023-07-15 21:52:30 +08:00
}),
fx.Provide(func(config *types.AppConfig) (function.FuncWeiboHot, error) {
return function.NewWeiboHot(config.FunApiToken), nil
2023-07-15 21:52:30 +08:00
}),
fx.Provide(func(config *types.AppConfig) (function.FuncHeadlines, error) {
return function.NewHeadLines(config.FunApiToken), nil
2023-07-10 18:59:53 +08:00
}),
// 创建控制器
fx.Provide(handler.NewChatRoleHandler),
fx.Provide(handler.NewUserHandler),
fx.Provide(handler.NewChatHandler),
fx.Provide(handler.NewUploadHandler),
fx.Provide(handler.NewVerifyHandler),
2023-07-21 18:26:51 +08:00
fx.Provide(handler.NewRewardHandler),
fx.Provide(admin.NewConfigHandler),
fx.Provide(admin.NewAdminHandler),
fx.Provide(admin.NewApiKeyHandler),
fx.Provide(admin.NewUserHandler),
fx.Provide(admin.NewChatRoleHandler),
2023-07-24 15:59:29 +08:00
fx.Provide(admin.NewRewardHandler),
// 创建服务
fx.Provide(service.NewAliYunSmsService),
// 注册路由
fx.Invoke(func(s *core.AppServer, h *handler.ChatRoleHandler) {
group := s.Engine.Group("/api/role/")
group.GET("list", h.List)
}),
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)
group.POST("password", h.Password)
group.POST("bind/mobile", h.BindMobile)
}),
fx.Invoke(func(s *core.AppServer, h *handler.ChatHandler) {
group := s.Engine.Group("/api/chat/")
group.Any("new", h.ChatHandle)
group.GET("list", h.List)
group.POST("update", h.Update)
group.GET("remove", h.Remove)
group.GET("history", h.History)
group.GET("clear", h.Clear)
group.GET("tokens", h.Tokens)
group.GET("stop", h.StopGenerate)
}),
fx.Invoke(func(s *core.AppServer, h *handler.UploadHandler) {
s.Engine.POST("/api/upload", h.Upload)
}),
fx.Invoke(func(s *core.AppServer, h *handler.VerifyHandler) {
group := s.Engine.Group("/api/verify/")
group.GET("token", h.Token)
group.POST("sms", h.SendMsg)
}),
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
}),
// 管理后台控制器
fx.Invoke(func(s *core.AppServer, h *admin.ConfigHandler) {
group := s.Engine.Group("/api/admin/config/")
group.POST("update", h.Update)
group.GET("get", h.Get)
}),
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)
}),
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)
group.GET("list", h.List)
2023-06-20 18:05:33 +08:00
group.GET("remove", h.Remove)
}),
fx.Invoke(func(s *core.AppServer, h *admin.UserHandler) {
group := s.Engine.Group("/api/admin/user/")
group.GET("list", h.List)
group.POST("update", h.Update)
group.GET("remove", h.Remove)
group.GET("loginLog", h.LoginLog)
}),
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)
group.POST("sort", h.SetSort)
group.GET("remove", h.Remove)
}),
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)
}),
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)
}
}