-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (70 loc) · 2.46 KB
/
Copy pathmain.go
File metadata and controls
81 lines (70 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package main provides the entry point for Kiro API Proxy.
//
// Kiro API Proxy is a reverse proxy service that translates Kiro API requests
// into OpenAI and Anthropic (Claude) compatible formats. Key features include:
// - Multi-account pool with round-robin load balancing
// - Automatic OAuth token refresh
// - Streaming response support for real-time AI interactions
// - Admin panel for account and configuration management
//
// The service exposes the following endpoints:
// - /v1/messages - Claude API compatible endpoint
// - /v1/chat/completions - OpenAI API compatible endpoint
// - /admin - Web-based administration panel
package main
import (
"fmt"
"kiro-go/config"
"kiro-go/logger"
"kiro-go/pool"
"kiro-go/proxy"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
func main() {
// 配置文件路径,支持环境变量覆盖
configPath := "data/config.json"
if envPath := os.Getenv("CONFIG_PATH"); envPath != "" {
configPath = envPath
}
// 确保数据目录存在
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
log.Fatalf("Failed to create data directory: %v", err)
}
// 加载配置
if err := config.Init(configPath); err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Initialize log level: LOG_LEVEL env var takes priority over config, defaulting to "info".
logger.Init(config.GetLogLevel())
// 环境变量覆盖密码
if envPassword := os.Getenv("ADMIN_PASSWORD"); envPassword != "" {
config.SetPassword(envPassword)
}
// 初始化账号池
pool.GetPool()
// 创建 HTTP 处理器(包含后台刷新任务)
handler := proxy.NewHandler()
// 启动服务器
addr := fmt.Sprintf("%s:%d", config.GetHost(), config.GetPort())
logger.Infof("Kiro-Go starting on http://%s (log level: %s)", addr, logger.LevelName(logger.GetLevel()))
logger.Infof("Admin panel: http://%s/admin", addr)
logger.Infof("Claude API: http://%s/v1/messages", addr)
logger.Infof("OpenAI API: http://%s/v1/chat/completions", addr)
// WriteTimeout intentionally 0: SSE streams can run for minutes while the
// upstream model produces tokens. ReadHeaderTimeout + ReadTimeout still
// guard against slowloris-style header/body stalls.
srv := &http.Server{
Addr: addr,
Handler: handler,
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
if err := srv.ListenAndServe(); err != nil {
logger.Fatalf("Server failed: %v", err)
}
}