60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
|
|
"jimeng/client"
|
|
"jimeng/config"
|
|
"jimeng/handler"
|
|
"jimeng/store"
|
|
)
|
|
|
|
//go:embed web/*
|
|
var webFS embed.FS
|
|
|
|
func main() {
|
|
configPath := flag.String("config", "config.yaml", "配置文件路径")
|
|
flag.Parse()
|
|
|
|
// 加载配置
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
log.Fatalf("加载配置失败: %v", err)
|
|
}
|
|
|
|
// 创建存储
|
|
s, err := store.NewStore(cfg.Store.DataFile)
|
|
if err != nil {
|
|
log.Fatalf("创建存储失败: %v", err)
|
|
}
|
|
|
|
// 创建API客户端
|
|
c := client.NewClient(cfg.API.AccessKey, cfg.API.SecretKey, cfg.API.Host)
|
|
|
|
// 创建处理器
|
|
h := handler.NewHandler(c, s, cfg)
|
|
|
|
// 创建路由
|
|
mux := http.NewServeMux()
|
|
|
|
// 注册API路由
|
|
h.RegisterRoutes(mux)
|
|
|
|
// 注册静态文件服务
|
|
webContent, _ := fs.Sub(webFS, "web")
|
|
fileServer := http.FileServer(http.FS(webContent))
|
|
mux.Handle("/", fileServer)
|
|
|
|
// 启动服务器
|
|
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
|
log.Printf("服务器启动在 http://localhost%s", addr)
|
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
log.Fatalf("服务器启动失败: %v", err)
|
|
}
|
|
}
|