58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
|
|
package web
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"os/signal"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Start() {
|
||
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
// 初始化数据库和 Redis
|
||
|
|
// model.Init()
|
||
|
|
// defer model.Close()
|
||
|
|
|
||
|
|
// clients.InitRedis()
|
||
|
|
// defer clients.CloseRedis()
|
||
|
|
|
||
|
|
// 测试
|
||
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
w.Write([]byte("Hello, World!"))
|
||
|
|
})
|
||
|
|
|
||
|
|
// 同步节点
|
||
|
|
http.HandleFunc("/sync", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
// 更新节点
|
||
|
|
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
// 标记上线
|
||
|
|
http.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
// 标记下线
|
||
|
|
http.HandleFunc("/down", func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
// 启动服务
|
||
|
|
server := http.Server{Addr: ":8080"}
|
||
|
|
go func() {
|
||
|
|
<-ctx.Done()
|
||
|
|
server.Shutdown(ctx)
|
||
|
|
}()
|
||
|
|
|
||
|
|
if err := server.ListenAndServe(); err != nil {
|
||
|
|
slog.Error("启动服务失败", "error", err)
|
||
|
|
}
|
||
|
|
}
|