Skip to content

Commit

Permalink
Refactor CoapConfig struct in main.go
Browse files Browse the repository at this point in the history
The CoapConfig struct in main.go has been refactored to improve code organization and readability. The struct now includes separate fields for host, port, DTLS, targetHost, and targetPort. This change allows for better configuration of CoAP settings. The code has been updated to reflect these changes.

Signed-off-by: SammyOina <[email protected]>
  • Loading branch information
SammyOina committed Oct 22, 2023
1 parent c4554c3 commit e071208
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
57 changes: 32 additions & 25 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,22 @@ type config struct {
serverCert string
serverKey string

httpConfig HTTPConfig
mqttConfig MQTTConfig
wsMQTTConfig WSMQTTConfig
wsConfig WSConfig

coapHost string
coapPort string
coapDTLS bool
coapTargetHost string
coapTargetPort string
httpConfig HTTPConfig
mqttConfig MQTTConfig
wsConfig WSConfig
coapConfig CoapConfig

logLevel string
}

type CoapConfig struct {
host string
port string
DTLS bool
targetHost string
targetPort string
}

type WSConfig struct {
host string
port string
Expand Down Expand Up @@ -212,7 +214,7 @@ func main() {
}

switch {
case cfg.coapDTLS:
case cfg.coapConfig.DTLS:
tlsCfg, err := mptls.LoadTLSCfg(cfg.caCerts, cfg.serverCert, cfg.serverKey)
if err != nil {
errs <- err
Expand All @@ -221,15 +223,15 @@ func main() {
Certificates: tlsCfg.Certificates,
ClientCAs: tlsCfg.ClientCAs,
}
go proxyCoapDTLS(cfg, dtlsCfg, logger, h, errs)
go proxyCoapDTLS(cfg.coapConfig, dtlsCfg, logger, h, errs)
case cfg.clientTLS:
tlsCfg, err := mptls.LoadTLSCfg(cfg.caCerts, cfg.serverCert, cfg.serverKey)
if err != nil {
errs <- err
}
go proxyCoapTLS(cfg, tlsCfg, logger, h, errs)
go proxyCoapTLS(cfg.coapConfig, tlsCfg, logger, h, errs)
default:
go proxyCoap(cfg, logger, h, errs)
go proxyCoap(cfg.coapConfig, logger, h, errs)
}

go func() {
Expand Down Expand Up @@ -305,11 +307,13 @@ func loadConfig() config {
targetPort: env(envWSTargetPort, defWSTargetPort),
},
// CoAP
coapHost: env(envCoAPHost, defCoAPHost),
coapPort: env(envCoAPPort, defCoAPPort),
coapDTLS: dtls,
coapTargetHost: env(envCoAPTargetHost, defCoAPTargetHost),
coapTargetPort: env(envCoAPTargetPort, defCoAPTargetPort),
coapConfig: CoapConfig{
host: env(envCoAPHost, defCoAPHost),
port: env(envCoAPPort, defCoAPPort),
DTLS: dtls,
targetHost: env(envCoAPTargetHost, defCoAPTargetHost),
targetPort: env(envCoAPTargetPort, defCoAPTargetPort),
},

// Log
logLevel: env(envLogLevel, defLogLevel),
Expand Down Expand Up @@ -392,6 +396,9 @@ func proxyWSS(ctx context.Context, cfg config, logger mflog.Logger, handler sess
func proxyCoapTLS(cfg config, tlsCfg *tls.Config, logger mflog.Logger, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.coapHost, cfg.coapPort)
target := fmt.Sprintf("%s:%s", cfg.coapTargetHost, cfg.coapTargetPort)
func proxyCoapTLS(cfg CoapConfig, tlsCfg *tls.Config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
cp, err := coap.NewProxy(address, target, logger, handler)
if err != nil {
errs <- err
Expand All @@ -400,9 +407,9 @@ func proxyCoapTLS(cfg config, tlsCfg *tls.Config, logger mflog.Logger, errs chan
errs <- cp.ListenTLS(tlsCfg)
}

func proxyCoapDTLS(cfg config, dtlsCfg *dtls.Config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.coapHost, cfg.coapPort)
target := fmt.Sprintf("%s:%s", cfg.coapTargetHost, cfg.coapTargetPort)
func proxyCoapDTLS(cfg CoapConfig, dtlsCfg *dtls.Config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
cp, err := coap.NewProxy(address, target, logger, handler)
if err != nil {
errs <- err
Expand All @@ -411,9 +418,9 @@ func proxyCoapDTLS(cfg config, dtlsCfg *dtls.Config, logger mflog.Logger, handle
errs <- cp.ListenDLS(dtlsCfg)
}

func proxyCoap(cfg config, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.coapHost, cfg.coapPort)
target := fmt.Sprintf("%s:%s", cfg.coapTargetHost, cfg.coapTargetPort)
func proxyCoap(cfg CoapConfig, logger mflog.Logger, handler session.Handler, errs chan error) {
address := fmt.Sprintf("%s:%s", cfg.host, cfg.port)
target := fmt.Sprintf("%s:%s", cfg.targetHost, cfg.targetPort)
cp, err := coap.NewProxy(address, target, logger, handler)
if err != nil {
errs <- err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/mainflux/mainflux v0.12.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
github.com/pion/dtls/v2 v2.2.8-0.20230905141523-2b584af66577
)

require (
github.com/dsnet/golib/memfile v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/pion/dtls/v2 v2.2.8-0.20230905141523-2b584af66577 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/transport/v3 v3.0.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
Expand Down

0 comments on commit e071208

Please sign in to comment.