Files
91-api-server/cmd/serve.go
2026-06-10 02:33:35 +00:00

58 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cmd
import (
"fmt"
"log"
"api-server/internal/config"
"api-server/internal/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// serveCmd 启动 HTTP 服务
var serveCmd = &cobra.Command{
Use: "serve",
Short: "启动 HTTP API 服务",
Long: `启动 HTTP API 服务,包括:
- RESTful API 接口
- 统计分析 API/api/stats/*
- 嵌入式监控仪表盘(/
- 请求日志与并发监控`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(`
╔══════════════════════════════════════╗
║ api-server - 量化级 API 服务 ║
║ Go + Gin + Cobra + ip2region ║
╚══════════════════════════════════════╝`)
// 加载配置
cfg := config.Load(viper.GetViper())
if cfg.Server.Mode == "debug" {
log.Println("[config] 运行模式: debug")
}
// 创建并启动服务
srv, err := server.New(cfg)
if err != nil {
return fmt.Errorf("创建服务失败: %w", err)
}
return srv.Run()
},
}
// versionCmd 版本信息
var versionCmd = &cobra.Command{
Use: "version",
Short: "显示版本信息",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("api-server v1.0.0")
fmt.Println(" Go: go1.22+")
fmt.Println(" Web: Gin + embedded single-page app")
fmt.Println(" Stats: ip2region + in-memory analytics engine")
},
}