From 91175e7509d98fa04a25fec48f38784124af940f Mon Sep 17 00:00:00 2001 From: imbytecat Date: Thu, 22 Aug 2024 11:51:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9A=84=20URL=20=E6=9D=A5=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E5=9B=9E=E8=B0=83=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 3 +++ config/config.go | 15 ++++++++++----- main.go | 2 +- router/handler/onvif/subscription.go | 5 ++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/config.yaml b/config.yaml index 82dc3ad..7c1779a 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,6 @@ server: port: 8080 host: "localhost" + +app: + url: "http://localhost:8080" diff --git a/config/config.go b/config/config.go index 357cea1..6d835d9 100644 --- a/config/config.go +++ b/config/config.go @@ -7,16 +7,21 @@ import ( "os" ) -type Config struct { - Server ServerConfig `mapstructure:"server"` +type config struct { + Server serverConfig `mapstructure:"server"` + App appConfig `mapstructure:"app"` } -type ServerConfig struct { +type serverConfig struct { Port int `mapstructure:"port"` Host string `mapstructure:"host"` } -var AppConfig Config +type appConfig struct { + URL string `mapstructure:"url"` +} + +var Conf config func LoadConfig() error { env := os.Getenv("PROFILE") @@ -37,7 +42,7 @@ func LoadConfig() error { return err } - if err := viper.Unmarshal(&AppConfig); err != nil { + if err := viper.Unmarshal(&Conf); err != nil { return err } diff --git a/main.go b/main.go index c236ae0..4788949 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ func main() { router.SetupRoutes(r) - addr := fmt.Sprintf(":%d", config.AppConfig.Server.Port) + addr := fmt.Sprintf("%s:%d", config.Conf.Server.Host, config.Conf.Server.Port) if err := r.Run(addr); err != nil { fmt.Println("Failed to start server:", err) } diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index e944404..b2760d5 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -1,17 +1,20 @@ package onvif import ( + "fmt" "github.com/IOTechSystems/onvif/event" "github.com/IOTechSystems/onvif/gosoap" "github.com/gin-gonic/gin" "log" "net/http" + "onvif-agent/config" ) func CreateEventSubscription(c *gin.Context) { xaddr := c.Param("xaddr") // FIXME: 把参数变成传入的参数 - result, err := conns[xaddr].SubscribeEvents("http://172.16.19.230:8080/events/callback", "PT60S") + callbackURL := event.AttributedURIType(fmt.Sprintf("%s/subscriptions/callback", config.Conf.App.URL)) + result, err := conns[xaddr].SubscribeEvents(callbackURL, "PT60S") if err != nil { c.JSON(http.StatusServiceUnavailable, gin.H{ "message": err.Error(),