-
Notifications
You must be signed in to change notification settings - Fork 1
/
copilot.go
57 lines (45 loc) · 1.18 KB
/
copilot.go
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
package main
import (
"log"
"net/http"
"github.com/rs/cors"
)
// 启动 http 服务
func (a *App) StartHTTPServer() error {
mux := http.NewServeMux()
a.HandleFuncWarp(mux)
// 创建 CORS 处理器
corsHandler := cors.New(cors.Options{
AllowedOrigins: config.CsrfDomains,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 300,
})
a.httpServer = &http.Server{
Addr: config.Port,
Handler: corsHandler.Handler(mux),
}
// 异步启动服务
go func() {
if err := a.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("listen: %s\n", err)
}
}()
log.Println("Server started. Press Ctrl+C to stop.")
log.Println("👉 Local: http://localhost" + config.Port)
a.SendLogsToPage("Server started. ")
a.SendLogsToPage("👉 http://localhost" + config.Port)
a.SendLogsToPage("Use the browser open https://keyframeai.top/copilot/dashboard")
return nil
}
// 关闭 http 服务
func (a *App) StopHTTPServer() error {
if a.httpServer == nil {
return nil
}
if err := a.httpServer.Shutdown(a.ctx); err != nil {
return err
}
return nil
}