From 9495e0e178af54a5df079d3ee8299023ab24f865 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Thu, 14 Nov 2024 18:15:17 +0100 Subject: [PATCH] Control URI in config; close #68 --- config/config.yaml | 5 +++-- go.mod | 2 +- go.sum | 2 ++ internal/app/http.go | 12 ++++++++---- internal/app/upf.go | 6 +----- internal/config/config.go | 6 +++--- internal/config/control.go | 15 +++++++++++++++ internal/config/logger.go | 3 ++- internal/config/rule.go | 3 ++- main.go | 13 +------------ 10 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 internal/config/control.go diff --git a/config/config.yaml b/config/config.yaml index c703069..26f47e7 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,6 +1,7 @@ pfcp-address: "10.0.60.3" -http-address: "192.0.2.2" -http-port: "8080" +control: + uri: "http://192.0.2.2:8080" + bind-addr: "192.0.2.2:8080" logger: level: info uplink: diff --git a/go.mod b/go.mod index 2ec19ce..4da2329 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/gofrs/uuid v4.4.0+incompatible github.com/nextmn/go-pfcp-networking v0.0.38 - github.com/nextmn/json-api v0.0.12 + github.com/nextmn/json-api v0.0.13 github.com/nextmn/logrus-formatter v0.0.1 github.com/nextmn/rfc9433 v0.0.2 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 4af8649..fc67cb0 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/nextmn/go-pfcp-networking v0.0.38 h1:C7HgLh6UUbqfoTHl/uvUcYNZUBPSur68 github.com/nextmn/go-pfcp-networking v0.0.38/go.mod h1:KYoKLiltDmHL2YMU5mz2k/E1xMoz4TpmzTz6Nr5u5gA= github.com/nextmn/json-api v0.0.12 h1:QIg+wmCBhti5hzvh2mtQ6sJ3XayFrOusvsnuHOd9fdU= github.com/nextmn/json-api v0.0.12/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak= +github.com/nextmn/json-api v0.0.13 h1:k8Z0Oo9et5PvdCa4wUmJE9TAHJp1zTkoAmvy1LQcoyQ= +github.com/nextmn/json-api v0.0.13/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak= github.com/nextmn/logrus-formatter v0.0.1 h1:Bsf78jjiEESc+rV8xE6IyKj4frDPGMwXFNrLQzm6A1E= github.com/nextmn/logrus-formatter v0.0.1/go.mod h1:vdSZ+sIcSna8vjbXkSFxsnsKHqRwaUEed4JCPcXoGyM= github.com/nextmn/rfc9433 v0.0.2 h1:6FjMY+Qy8MNXQ0PPxezUsyXDxJiCbTp5j3OcXQgIQh8= diff --git a/internal/app/http.go b/internal/app/http.go index 51ae6c0..ff95e06 100644 --- a/internal/app/http.go +++ b/internal/app/http.go @@ -11,9 +11,11 @@ import ( "sync" "time" + "github.com/nextmn/json-api/healthcheck" + "github.com/nextmn/json-api/jsonapi" + "github.com/gin-gonic/gin" "github.com/gofrs/uuid" - "github.com/nextmn/json-api/jsonapi" "github.com/sirupsen/logrus" ) @@ -27,7 +29,7 @@ type RouterRegistry struct { routers jsonapi.RouterMap } -func NewHttpServerEntity(addr string, port string) *HttpServerEntity { +func NewHttpServerEntity(httpAddr string) *HttpServerEntity { rr := RouterRegistry{ routers: make(jsonapi.RouterMap), } @@ -38,7 +40,6 @@ func NewHttpServerEntity(addr string, port string) *HttpServerEntity { r.GET("/routers/:uuid", rr.GetRouter) r.DELETE("/routers/:uuid", rr.DeleteRouter) r.POST("/routers", rr.PostRouter) - httpAddr := fmt.Sprintf("[%s]:%s", addr, port) logrus.WithFields(logrus.Fields{"http-addr": httpAddr}).Info("HTTP Server created") e := HttpServerEntity{ routers: &rr, @@ -69,8 +70,11 @@ func (e *HttpServerEntity) Stop() { // get status of the controller func (l *RouterRegistry) Status(c *gin.Context) { + status := healthcheck.Status{ + Ready: true, + } c.Header("Cache-Control", "no-cache") - c.JSON(http.StatusOK, gin.H{"ready": true}) + c.JSON(http.StatusOK, status) } // get a router infos diff --git a/internal/app/upf.go b/internal/app/upf.go index 7c4094d..039ceb4 100644 --- a/internal/app/upf.go +++ b/internal/app/upf.go @@ -43,11 +43,7 @@ func PFCPServerAddHooks(s *pfcp_networking.PFCPEntityUP, pusher *RulesPusher) er } func NewHttpServer(conf *config.CtrlConfig) *HttpServerEntity { - port := "80" // default http port - if conf.HTTPPort != nil { - port = *conf.HTTPPort - } - HTTPServer := NewHttpServerEntity(conf.HTTPAddress.String(), port) + HTTPServer := NewHttpServerEntity(conf.Control.BindAddr) return HTTPServer } diff --git a/internal/config/config.go b/internal/config/config.go index 51f5ba3..01c58e5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,8 @@ -// Copyright 2023 Louis Royer and the NextMN-SRv6-ctrl contributors. All rights reserved. +// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved. // Use of this source code is governed by a MIT-style license that can be // found in the LICENSE file. // SPDX-License-Identifier: MIT + package config import ( @@ -30,8 +31,7 @@ func ParseConf(file string) (*CtrlConfig, error) { type CtrlConfig struct { PFCPAddress netip.Addr `yaml:"pfcp-address"` - HTTPAddress netip.Addr `yaml:"http-address"` - HTTPPort *string `yaml:"http-port,omitempty"` // default: 80 + Control Control `yaml:"control"` Logger *Logger `yaml:"logger,omitempty"` Uplink []Rule `yaml:"uplink"` Downlink []Rule `yaml:"downlink"` diff --git a/internal/config/control.go b/internal/config/control.go new file mode 100644 index 0000000..673cecc --- /dev/null +++ b/internal/config/control.go @@ -0,0 +1,15 @@ +// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved. +// Use of this source code is governed by a MIT-style license that can be +// found in the LICENSE file. +// SPDX-License-Identifier: MIT + +package config + +import ( + "github.com/nextmn/json-api/jsonapi" +) + +type Control struct { + Uri jsonapi.ControlURI `yaml:"uri"` // may contain domain name instead of ip address + BindAddr string `yaml:"bind-addr"` // in the form `ip` or `ip:port` (with default port being 80) +} diff --git a/internal/config/logger.go b/internal/config/logger.go index eb171c3..188b0bc 100644 --- a/internal/config/logger.go +++ b/internal/config/logger.go @@ -1,7 +1,8 @@ -// Copyright 2024 Louis Royer and the NextMN-SRv6-ctrl contributors. All rights reserved. +// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved. // Use of this source code is governed by a MIT-style license that can be // found in the LICENSE file. // SPDX-License-Identifier: MIT + package config import "github.com/sirupsen/logrus" diff --git a/internal/config/rule.go b/internal/config/rule.go index 515eee4..3e3086a 100644 --- a/internal/config/rule.go +++ b/internal/config/rule.go @@ -1,7 +1,8 @@ -// Copyright 2023 Louis Royer and the NextMN-SRv6-ctrl contributors. All rights reserved. +// Copyright 2023 Louis Royer and the NextMN contributors. All rights reserved. // Use of this source code is governed by a MIT-style license that can be // found in the LICENSE file. // SPDX-License-Identifier: MIT + package config type Rule struct { diff --git a/main.go b/main.go index 29ab9bf..b85216d 100644 --- a/main.go +++ b/main.go @@ -79,18 +79,7 @@ func main() { if conf.Logger != nil { logrus.SetLevel(conf.Logger.Level) } - // TODO: use directly URI in config - httpPort := "80" // default http port - if conf.HTTPPort != nil { - httpPort = *conf.HTTPPort - } - httpURI := "http://" - if conf.HTTPAddress.Is6() { - httpURI = httpURI + "[" + conf.HTTPAddress.String() + "]:" + httpPort - } else { - httpURI = httpURI + conf.HTTPAddress.String() + ":" + httpPort - } - if err := healthcheck.NewHealthcheck(httpURI, "go-github-nextmn-srv6-ctrl").Run(ctx.Context); err != nil { + if err := healthcheck.NewHealthcheck(*conf.Control.Uri.JoinPath("status"), "go-github-nextmn-srv6-ctrl").Run(ctx.Context); err != nil { os.Exit(1) } return nil