From 02c7464ce207c0b35acceb02fa2a369e19857be5 Mon Sep 17 00:00:00 2001 From: Holger Cremer Date: Sat, 25 Aug 2018 15:18:40 +0200 Subject: [PATCH] changes project layout --- cmd/{ => spaceDevices}/spaceDevices.go | 8 ++--- do.sh | 2 +- {conf => internal/conf}/config.go | 0 {conf => internal/conf}/config_test.go | 4 +-- {db => internal/db}/macDb.go | 2 +- {db => internal/db}/userDb.go | 2 +- {db => internal/db}/utils.go | 0 {db => internal/db}/utils_test.go | 0 {db => internal/db}/visibility.go | 0 {mqtt => internal/mqtt}/deviceData.go | 33 ++++++++++--------- {mqtt => internal/mqtt}/deviceData_test.go | 4 +-- {mqtt => internal/mqtt}/mqtt.go | 7 ++-- {mqtt => internal/mqtt}/mqtt_test.go | 0 {mqtt => internal/mqtt}/utils.go | 0 {mqtt => internal/mqtt}/watchDog.go | 0 .../webService}/simpleXsrfCheck.go | 0 {webService => internal/webService}/web.go | 6 ++-- {mqtt => pkg/structs}/structs.go | 2 +- 18 files changed, 36 insertions(+), 34 deletions(-) rename cmd/{ => spaceDevices}/spaceDevices.go (89%) rename {conf => internal/conf}/config.go (100%) rename {conf => internal/conf}/config_test.go (85%) rename {db => internal/db}/macDb.go (95%) rename {db => internal/db}/userDb.go (97%) rename {db => internal/db}/utils.go (100%) rename {db => internal/db}/utils_test.go (100%) rename {db => internal/db}/visibility.go (100%) rename {mqtt => internal/mqtt}/deviceData.go (82%) rename {mqtt => internal/mqtt}/deviceData_test.go (98%) rename {mqtt => internal/mqtt}/mqtt.go (95%) rename {mqtt => internal/mqtt}/mqtt_test.go (100%) rename {mqtt => internal/mqtt}/utils.go (100%) rename {mqtt => internal/mqtt}/watchDog.go (100%) rename {webService => internal/webService}/simpleXsrfCheck.go (100%) rename {webService => internal/webService}/web.go (96%) rename {mqtt => pkg/structs}/structs.go (98%) diff --git a/cmd/spaceDevices.go b/cmd/spaceDevices/spaceDevices.go similarity index 89% rename from cmd/spaceDevices.go rename to cmd/spaceDevices/spaceDevices.go index 2b48d2c..510c218 100644 --- a/cmd/spaceDevices.go +++ b/cmd/spaceDevices/spaceDevices.go @@ -4,10 +4,10 @@ import ( "fmt" "os" - "github.com/ktt-ol/spaceDevices/conf" - "github.com/ktt-ol/spaceDevices/db" - "github.com/ktt-ol/spaceDevices/mqtt" - "github.com/ktt-ol/spaceDevices/webService" + "github.com/ktt-ol/spaceDevices/internal/conf" + "github.com/ktt-ol/spaceDevices/internal/db" + "github.com/ktt-ol/spaceDevices/internal/mqtt" + "github.com/ktt-ol/spaceDevices/internal/webService" "github.com/sirupsen/logrus" ) diff --git a/do.sh b/do.sh index 46cc4bb..af29403 100755 --- a/do.sh +++ b/do.sh @@ -13,7 +13,7 @@ fi while (( "$#" )); do case "$1" in build-linux) - env GOOS=linux GOARCH=amd64 go build cmd/spaceDevices.go + env GOOS=linux GOARCH=amd64 go build cmd/spaceDevices/spaceDevices.go ;; test-sync) rsync -n -avzi --delete spaceDevices webUI root@spacegate:/home/status/spaceDevices2/ diff --git a/conf/config.go b/internal/conf/config.go similarity index 100% rename from conf/config.go rename to internal/conf/config.go diff --git a/conf/config_test.go b/internal/conf/config_test.go similarity index 85% rename from conf/config_test.go rename to internal/conf/config_test.go index 70685d1..bbdb276 100644 --- a/conf/config_test.go +++ b/internal/conf/config_test.go @@ -1,9 +1,9 @@ -package conf_test +package conf import ( "testing" - "github.com/ktt-ol/spaceDevices/conf" + "github.com/ktt-ol/spaceDevices/internal/confrnal/conf" "github.com/stretchr/testify/assert" ) diff --git a/db/macDb.go b/internal/db/macDb.go similarity index 95% rename from db/macDb.go rename to internal/db/macDb.go index 86f9450..0c6be06 100644 --- a/db/macDb.go +++ b/internal/db/macDb.go @@ -4,7 +4,7 @@ import ( "encoding/json" "io/ioutil" - "github.com/ktt-ol/spaceDevices/conf" + "github.com/ktt-ol/spaceDevices/internal/conf" log "github.com/sirupsen/logrus" ) diff --git a/db/userDb.go b/internal/db/userDb.go similarity index 97% rename from db/userDb.go rename to internal/db/userDb.go index 2781d69..3fd205d 100644 --- a/db/userDb.go +++ b/internal/db/userDb.go @@ -5,7 +5,7 @@ import ( "io/ioutil" "sync" - "github.com/ktt-ol/spaceDevices/conf" + "github.com/ktt-ol/spaceDevices/internal/conf" log "github.com/sirupsen/logrus" ) diff --git a/db/utils.go b/internal/db/utils.go similarity index 100% rename from db/utils.go rename to internal/db/utils.go diff --git a/db/utils_test.go b/internal/db/utils_test.go similarity index 100% rename from db/utils_test.go rename to internal/db/utils_test.go diff --git a/db/visibility.go b/internal/db/visibility.go similarity index 100% rename from db/visibility.go rename to internal/db/visibility.go diff --git a/mqtt/deviceData.go b/internal/mqtt/deviceData.go similarity index 82% rename from mqtt/deviceData.go rename to internal/mqtt/deviceData.go index 3588ebc..cad8e9f 100644 --- a/mqtt/deviceData.go +++ b/internal/mqtt/deviceData.go @@ -3,8 +3,8 @@ package mqtt import ( "encoding/json" - "github.com/ktt-ol/spaceDevices/conf" - "github.com/ktt-ol/spaceDevices/db" + "github.com/ktt-ol/spaceDevices/internal/conf" + "github.com/ktt-ol/spaceDevices/internal/db" "github.com/sirupsen/logrus" "crypto/md5" @@ -12,6 +12,7 @@ import ( "bytes" "sort" "strings" + "github.com/ktt-ol/spaceDevices/pkg/structs" ) var ignoredVisibility = [...]db.Visibility{db.VisibilityCriticalInfrastructure, db.VisibilityImportantInfrastructure, @@ -22,7 +23,7 @@ var ddLogger = logrus.WithField("where", "deviceData") type devicesEntry struct { hideName bool showDevices bool - devices []Devices + devices []structs.Devices } type DeviceData struct { @@ -30,7 +31,7 @@ type DeviceData struct { mqttHandler *MqttHandler masterDb db.MasterDb userDb db.UserDb - wifiSessionList []WifiSession + wifiSessionList []structs.WifiSession lastSentHash []byte @@ -74,7 +75,7 @@ func (d *DeviceData) newData(data []byte) { h := md5.New() s := fmt.Sprintf("%v", peopleAndDevices) hash := h.Sum([]byte(s)) - if bytes.Equal(hash, d.lastSentHash) { + if bytes.Equal(hash, d.lastSentHash) { ddLogger.Debug("Nothing changed in people count, skipping mqtt") } else { d.mqttHandler.SendPeopleAndDevices(peopleAndDevices) @@ -84,18 +85,18 @@ func (d *DeviceData) newData(data []byte) { } } -func (d *DeviceData) GetByIp(ip string) (WifiSession, bool) { +func (d *DeviceData) GetByIp(ip string) (structs.WifiSession, bool) { for _, v := range d.wifiSessionList { if v.Ip == ip { return v, true } } - return WifiSession{}, false + return structs.WifiSession{}, false } -func (d *DeviceData) parseWifiSessions(rawData []byte) (sessionsList []WifiSession, peopleAndDevices PeopleAndDevices, success bool) { - var sessionData map[string]WifiSession +func (d *DeviceData) parseWifiSessions(rawData []byte) (sessionsList []structs.WifiSession, peopleAndDevices structs.PeopleAndDevices, success bool) { + var sessionData map[string]structs.WifiSession if err := json.Unmarshal(rawData, &sessionData); err != nil { ddLogger.WithFields(logrus.Fields{ "rawData": string(rawData), @@ -134,7 +135,7 @@ SESSION_LOOP: username2DevicesMap[userInfo.Name] = entry } - device := Devices{Name: userInfo.DeviceName, Location: d.findLocation(wifiSession.AP)} + device := structs.Devices{Name: userInfo.DeviceName, Location: d.findLocation(wifiSession.AP)} entry.devices = append(entry.devices, device) if userInfo.Visibility == db.VisibilityIgnore { @@ -162,22 +163,22 @@ SESSION_LOOP: } } - peopleAndDevices.People = make([]Person, 0, 10) + peopleAndDevices.People = make([]structs.Person, 0, 10) for username, devicesEntry := range username2DevicesMap { if devicesEntry.hideName { continue } - var person Person + var person structs.Person if devicesEntry.showDevices { - person = Person{Name: username, Devices: devicesEntry.devices} - sort.Sort(DevicesSorter(person.Devices)) + person = structs.Person{Name: username, Devices: devicesEntry.devices} + sort.Sort(structs.DevicesSorter(person.Devices)) } else { - person = Person{Name: username} + person = structs.Person{Name: username} } peopleAndDevices.People = append(peopleAndDevices.People, person) } - sort.Sort(PersonSorter(peopleAndDevices.People)) + sort.Sort(structs.PersonSorter(peopleAndDevices.People)) success = true return diff --git a/mqtt/deviceData_test.go b/internal/mqtt/deviceData_test.go similarity index 98% rename from mqtt/deviceData_test.go rename to internal/mqtt/deviceData_test.go index c91934c..ef8e429 100644 --- a/mqtt/deviceData_test.go +++ b/internal/mqtt/deviceData_test.go @@ -6,9 +6,9 @@ import ( "strconv" "testing" - "github.com/ktt-ol/spaceDevices/conf" + "github.com/ktt-ol/spaceDevices/internal/conf" - "github.com/ktt-ol/spaceDevices/db" + "github.com/ktt-ol/spaceDevices/internal/db" "github.com/stretchr/testify/assert" ) diff --git a/mqtt/mqtt.go b/internal/mqtt/mqtt.go similarity index 95% rename from mqtt/mqtt.go rename to internal/mqtt/mqtt.go index 171332e..a267517 100644 --- a/mqtt/mqtt.go +++ b/internal/mqtt/mqtt.go @@ -8,8 +8,9 @@ import ( "time" "github.com/eclipse/paho.mqtt.golang" - "github.com/ktt-ol/spaceDevices/conf" + "github.com/ktt-ol/spaceDevices/internal/conf" log "github.com/sirupsen/logrus" + "github.com/ktt-ol/spaceDevices/pkg/structs" ) const CLIENT_ID = "spaceDevicesGo" @@ -91,7 +92,7 @@ func (h *MqttHandler) GetNewDataChannel() chan []byte { return h.newDataChan } -func (h *MqttHandler) SendPeopleAndDevices(data PeopleAndDevices) { +func (h *MqttHandler) SendPeopleAndDevices(data structs.PeopleAndDevices) { bytes, err := json.Marshal(data) if err != nil { mqttLogger.Errorln("Invalid people json", err) @@ -198,7 +199,7 @@ func defaultCertPool(certFile string) *x509.CertPool { } func emptyPeopleAndDevices() string { - pad := PeopleAndDevices{People: []Person{}} + pad := structs.PeopleAndDevices{People: []structs.Person{}} bytes, err := json.Marshal(pad) if err != nil { mqttLogger.WithError(err).Panic() diff --git a/mqtt/mqtt_test.go b/internal/mqtt/mqtt_test.go similarity index 100% rename from mqtt/mqtt_test.go rename to internal/mqtt/mqtt_test.go diff --git a/mqtt/utils.go b/internal/mqtt/utils.go similarity index 100% rename from mqtt/utils.go rename to internal/mqtt/utils.go diff --git a/mqtt/watchDog.go b/internal/mqtt/watchDog.go similarity index 100% rename from mqtt/watchDog.go rename to internal/mqtt/watchDog.go diff --git a/webService/simpleXsrfCheck.go b/internal/webService/simpleXsrfCheck.go similarity index 100% rename from webService/simpleXsrfCheck.go rename to internal/webService/simpleXsrfCheck.go diff --git a/webService/web.go b/internal/webService/web.go similarity index 96% rename from webService/web.go rename to internal/webService/web.go index c3d7f55..01e60e5 100644 --- a/webService/web.go +++ b/internal/webService/web.go @@ -8,9 +8,9 @@ import ( "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" - "github.com/ktt-ol/spaceDevices/conf" - "github.com/ktt-ol/spaceDevices/db" - "github.com/ktt-ol/spaceDevices/mqtt" + "github.com/ktt-ol/spaceDevices/internal/conf" + "github.com/ktt-ol/spaceDevices/internal/db" + "github.com/ktt-ol/spaceDevices/internal/mqtt" "github.com/sirupsen/logrus" ) diff --git a/mqtt/structs.go b/pkg/structs/structs.go similarity index 98% rename from mqtt/structs.go rename to pkg/structs/structs.go index 7aada9a..8f1ae89 100644 --- a/mqtt/structs.go +++ b/pkg/structs/structs.go @@ -1,4 +1,4 @@ -package mqtt +package structs import ( "strings"