Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Config fields from main.go should not be exported; minor style changes.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
  • Loading branch information
dborovcanin committed Oct 31, 2018
1 parent e8d573a commit 416482c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
26 changes: 13 additions & 13 deletions cmd/coap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ const (
)

type config struct {
Port string
NatsURL string
ThingsURL string
LogLevel string
port string
natsURL string
thingsURL string
logLevel string
}

func main() {
cfg := loadConfig()

logger, err := logger.New(os.Stdout, cfg.LogLevel)
logger, err := logger.New(os.Stdout, cfg.logLevel)
if err != nil {
log.Fatalf(err.Error())
}

nc, err := broker.Connect(cfg.NatsURL)
nc, err := broker.Connect(cfg.natsURL)
if err != nil {
logger.Error(fmt.Sprintf("Failed to connect to NATS: %s", err))
os.Exit(1)
}
defer nc.Close()

conn, err := grpc.Dial(cfg.ThingsURL, grpc.WithInsecure())
conn, err := grpc.Dial(cfg.thingsURL, grpc.WithInsecure())
if err != nil {
logger.Error(fmt.Sprintf("Failed to connect to users service: %s", err))
os.Exit(1)
Expand Down Expand Up @@ -94,8 +94,8 @@ func main() {

errs := make(chan error, 2)

go startHTTPServer(cfg.Port, logger, errs)
go startCOAPServer(cfg.Port, svc, cc, respChan, logger, errs)
go startHTTPServer(cfg.port, logger, errs)
go startCOAPServer(cfg.port, svc, cc, respChan, logger, errs)

go func() {
c := make(chan os.Signal)
Expand All @@ -109,10 +109,10 @@ func main() {

func loadConfig() config {
return config{
ThingsURL: mainflux.Env(envThingsURL, defThingsURL),
NatsURL: mainflux.Env(envNatsURL, defNatsURL),
Port: mainflux.Env(envPort, defPort),
LogLevel: mainflux.Env(envLogLevel, defLogLevel),
thingsURL: mainflux.Env(envThingsURL, defThingsURL),
natsURL: mainflux.Env(envNatsURL, defNatsURL),
port: mainflux.Env(envPort, defPort),
logLevel: mainflux.Env(envLogLevel, defLogLevel),
}
}

Expand Down
6 changes: 3 additions & 3 deletions coap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func receive(svc coap.Service) handler {
return func(conn *net.UDPConn, addr *net.UDPAddr, msg *gocoap.Message) *gocoap.Message {
// By default message is NonConfirmable, so
// NonConfirmable response is sent back.
var res = &gocoap.Message{
res := &gocoap.Message{
Type: gocoap.NonConfirmable,
// According to https://tools.ietf.org/html/rfc7252#page-47: If the POST
// succeeds but does not result in a new resource being created on the
Expand Down Expand Up @@ -164,8 +164,7 @@ func receive(svc coap.Service) handler {

func observe(svc coap.Service, responses chan<- string) handler {
return func(conn *net.UDPConn, addr *net.UDPAddr, msg *gocoap.Message) *gocoap.Message {
var res *gocoap.Message
res = &gocoap.Message{
res := &gocoap.Message{
Type: gocoap.Acknowledgement,
Code: gocoap.Content,
MessageID: msg.MessageID,
Expand Down Expand Up @@ -212,6 +211,7 @@ func observe(svc coap.Service, responses chan<- string) handler {
go ping(svc, obsID, conn, addr, o, msg)
go cancel(o)
}

return res
}
}
Expand Down
8 changes: 2 additions & 6 deletions coap/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

package api

import (
"strings"
)

const keyHeader = "key"
import "strings"

func authKey(opt interface{}) (string, error) {
val, ok := opt.(string)
if !ok {
return "", errBadRequest
}
arr := strings.Split(val, "=")
if len(arr) != 2 || strings.ToLower(arr[0]) != keyHeader {
if len(arr) != 2 || strings.ToLower(arr[0]) != "key" {
return "", errBadOption
}
return arr[1], nil
Expand Down

0 comments on commit 416482c

Please sign in to comment.