refactor: 使用配置文件的 URL 来拼接回调 URL
This commit is contained in:
parent
6b1cc58445
commit
91175e7509
@ -1,3 +1,6 @@
|
||||
server:
|
||||
port: 8080
|
||||
host: "localhost"
|
||||
|
||||
app:
|
||||
url: "http://localhost:8080"
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
2
main.go
2
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)
|
||||
}
|
||||
|
@ -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(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user