端口分配时加锁;网关上线后保存平台恢复的节点与授权数据;现在新节点连接后会按需向平台报告更新
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"proxy-server/gateway/app"
|
||||
"proxy-server/gateway/core"
|
||||
@@ -23,9 +24,10 @@ func Online(name string) (err error) {
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Id int32 `json:"id"`
|
||||
Secret string `json:"secret"`
|
||||
Permits []core.PermitDef `json:"permits"`
|
||||
Id int32 `json:"id"`
|
||||
Secret string `json:"secret"`
|
||||
Permits []*core.PermitDef `json:"permits"`
|
||||
Edges []*core.Edge `json:"edges"`
|
||||
}
|
||||
err = json.Unmarshal([]byte(resp), &body)
|
||||
if err != nil {
|
||||
@@ -35,25 +37,20 @@ func Online(name string) (err error) {
|
||||
app.Id = body.Id
|
||||
app.PlatformSecret = body.Secret
|
||||
for _, def := range body.Permits {
|
||||
app.Permits.Store(def.Id, &def.Permit)
|
||||
app.StorePermit(def)
|
||||
}
|
||||
for _, edge := range body.Edges {
|
||||
err := app.StoreEdge(edge)
|
||||
if err != nil {
|
||||
slog.Error("存储边缘节点失败", "err", err, "edge", edge)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Offline(name string) (err error) {
|
||||
func Offline() (err error) {
|
||||
_, err = call(env.EndpointOffline, map[string]any{
|
||||
"name": name,
|
||||
"version": core.Version,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func Assigned(edgeId int32, port uint16) (err error) {
|
||||
_, err = call(env.EndpointAssigned, map[string]any{
|
||||
"proxy": app.Id,
|
||||
"edge": edgeId,
|
||||
"port": port,
|
||||
"id": app.Id,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -78,7 +75,10 @@ func call(endpoint string, body any) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("请求失败,状态码:%d", resp.StatusCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user