feat(report): add module registration and port management functionality
This commit is contained in:
parent
d8e2e81688
commit
2ed379d5e8
@ -1,12 +0,0 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="go build github.com/perfect-panel/server" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="server" />
|
||||
<working_directory value="$PROJECT_DIR$" />
|
||||
<parameters value="run --config etc/ppanel-dev.yaml" />
|
||||
<kind value="PACKAGE" />
|
||||
<package value="github.com/perfect-panel/server" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<filePath value="$PROJECT_DIR$/ppanel.go" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
22
internal/report/report.go
Normal file
22
internal/report/report.go
Normal file
@ -0,0 +1,22 @@
|
||||
package report
|
||||
|
||||
const (
|
||||
GatewayURL = "http://127.0.0.1:%d" // 网关地址
|
||||
RegisterAPI = "/basic/register" // 模块注册接口
|
||||
)
|
||||
|
||||
// RegisterRequest 模块注册请求参数
|
||||
type RegisterRequest struct {
|
||||
Secret string `json:"secret"` // 通讯密钥
|
||||
ProxyPath string `json:"proxy_path"` // 代理路径
|
||||
ServiceURL string `json:"service_url"` // 服务地址
|
||||
Repository string `json:"repository"` // 服务代码仓库
|
||||
ServiceName string `json:"service_name"` // 服务名称
|
||||
ServiceVersion string `json:"service_version"` // 服务版本
|
||||
}
|
||||
|
||||
// RegisterResponse 模块注册响应参数
|
||||
type RegisterResponse struct {
|
||||
Success bool `json:"success"` // 注册是否成功
|
||||
Message string `json:"message"` // 返回信息
|
||||
}
|
||||
51
internal/report/tool.go
Normal file
51
internal/report/tool.go
Normal file
@ -0,0 +1,51 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// FreePort returns a free TCP port by opening a listener on port 0.
|
||||
func FreePort() (int, error) {
|
||||
l, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer l.Close()
|
||||
// Get the assigned port
|
||||
addr := l.Addr().(*net.TCPAddr)
|
||||
return addr.Port, nil
|
||||
}
|
||||
|
||||
// ModulePort returns the module port from the environment variable or a free port.
|
||||
func ModulePort() (int, error) {
|
||||
// 从环境变量获取端口号
|
||||
value, exists := os.LookupEnv("PPANEL_PORT")
|
||||
if exists {
|
||||
var port int
|
||||
_, err := fmt.Sscanf(value, "%d", &port)
|
||||
if err != nil {
|
||||
return FreePort()
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
return FreePort()
|
||||
}
|
||||
|
||||
// GatewayPort returns the gateway port from the environment variable or a free port.
|
||||
func GatewayPort() (int, error) {
|
||||
// 从环境变量获取端口号
|
||||
value, exists := os.LookupEnv("GATEWAY_PORT")
|
||||
if exists {
|
||||
var port int
|
||||
_, err := fmt.Sscanf(value, "%d", &port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
return 0, errors.New("could not determine gateway port")
|
||||
}
|
||||
21
internal/report/tool_test.go
Normal file
21
internal/report/tool_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFreePort(t *testing.T) {
|
||||
port, err := FreePort()
|
||||
if err != nil {
|
||||
t.Fatalf("FreePort() error: %v", err)
|
||||
}
|
||||
t.Logf("FreePort: %v", port)
|
||||
}
|
||||
|
||||
func TestModulePort(t *testing.T) {
|
||||
port, err := ModulePort()
|
||||
if err != nil {
|
||||
t.Fatalf("ModulePort() error: %v", err)
|
||||
}
|
||||
t.Logf("ModulePort: %v", port)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user