| 
									
										
										
										
											2025-09-21 18:27:25 +08:00
										 |  |  |  | package main | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"context" | 
					
						
							|  |  |  |  | 	"flag" | 
					
						
							|  |  |  |  | 	"fmt" | 
					
						
							|  |  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2025-09-30 17:44:18 +08:00
										 |  |  |  | 	"tydata-server/app/main/api/internal/config" | 
					
						
							|  |  |  |  | 	"tydata-server/app/main/api/internal/handler" | 
					
						
							|  |  |  |  | 	"tydata-server/app/main/api/internal/middleware" | 
					
						
							|  |  |  |  | 	"tydata-server/app/main/api/internal/queue" | 
					
						
							|  |  |  |  | 	"tydata-server/app/main/api/internal/service" | 
					
						
							|  |  |  |  | 	"tydata-server/app/main/api/internal/svc" | 
					
						
							| 
									
										
										
										
											2025-09-21 18:27:25 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"github.com/zeromicro/go-zero/core/conf" | 
					
						
							|  |  |  |  | 	"github.com/zeromicro/go-zero/rest" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func main() { | 
					
						
							|  |  |  |  | 	// 读取环境变量 ENV,默认为 "prod" | 
					
						
							|  |  |  |  | 	env := os.Getenv("ENV") | 
					
						
							|  |  |  |  | 	if env == "" { | 
					
						
							|  |  |  |  | 		env = "production" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 根据 ENV 加载不同的配置文件 | 
					
						
							|  |  |  |  | 	var defaultConfigFile string | 
					
						
							|  |  |  |  | 	if env == "development" { | 
					
						
							|  |  |  |  | 		defaultConfigFile = "app/main/api/etc/main.dev.yaml" | 
					
						
							|  |  |  |  | 	} else { | 
					
						
							|  |  |  |  | 		defaultConfigFile = "etc/main.yaml" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	configFile := flag.String("f", defaultConfigFile, "the config file") | 
					
						
							|  |  |  |  | 	flag.Parse() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	var c config.Config | 
					
						
							|  |  |  |  | 	conf.MustLoad(*configFile, &c) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	svcContext := svc.NewServiceContext(c) | 
					
						
							|  |  |  |  | 	defer svcContext.Close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 启动 asynq 消费者 | 
					
						
							|  |  |  |  | 	go func() { | 
					
						
							|  |  |  |  | 		ctx := context.Background() | 
					
						
							|  |  |  |  | 		// 初始化 cron job 或异步任务队列 | 
					
						
							|  |  |  |  | 		asynq := queue.NewCronJob(ctx, svcContext) | 
					
						
							|  |  |  |  | 		mux := asynq.Register() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// 启动 asynq 消费者 | 
					
						
							|  |  |  |  | 		if err := svcContext.AsynqServer.Run(mux); err != nil { | 
					
						
							|  |  |  |  | 			logx.WithContext(ctx).Errorf("异步任务启动失败: %v", err) | 
					
						
							|  |  |  |  | 			os.Exit(1) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		fmt.Println("异步任务启动!!!") | 
					
						
							|  |  |  |  | 	}() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	server := rest.MustNewServer(c.RestConf) | 
					
						
							|  |  |  |  | 	server.Use(middleware.GlobalSourceInterceptor) | 
					
						
							|  |  |  |  | 	defer server.Stop() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	handler.RegisterHandlers(server, svcContext) | 
					
						
							| 
									
										
										
										
											2025-09-30 17:44:18 +08:00
										 |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 自动注册API到数据库 | 
					
						
							|  |  |  |  | 	apiRegistry := service.NewApiRegistryService(svcContext.AdminApiModel) | 
					
						
							|  |  |  |  | 	routes := server.Routes() | 
					
						
							|  |  |  |  | 	if err := apiRegistry.RegisterAllApis(context.Background(), routes); err != nil { | 
					
						
							|  |  |  |  | 		logx.Errorf("API注册失败: %v", err) | 
					
						
							|  |  |  |  | 	} else { | 
					
						
							|  |  |  |  | 		logx.Infof("API注册成功,共注册 %d 个路由", len(routes)) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2025-09-21 18:27:25 +08:00
										 |  |  |  | 	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) | 
					
						
							|  |  |  |  | 	server.Start() | 
					
						
							|  |  |  |  | } |