From 19d32f1e5cf3d1320f18ac65e8fdfd2f5da912d1 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Fri, 21 Oct 2022 12:55:29 +0200 Subject: [PATCH] Move hub from system to router --- hub/hub.go | 16 +++++++++------- server/routes.go | 10 +++++----- server/server.go | 3 +++ system/system.go | 5 ++--- system/types.go | 7 ------- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/hub/hub.go b/hub/hub.go index 5753c95..2747cf6 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -28,20 +28,22 @@ type ( var LiveInformationCh chan Message -func (h *Hub) Initialize() { +func NewHub() *Hub { + hub := Hub{} LiveInformationCh = make(chan Message) - h.Notifier = make(NotifierChan) - h.NewClients = make(chan NotifierChan) - h.ClosingClients = make(chan NotifierChan) - h.clients = make(map[NotifierChan]struct{}) - go h.listen() + hub.Notifier = make(NotifierChan) + hub.NewClients = make(chan NotifierChan) + hub.ClosingClients = make(chan NotifierChan) + hub.clients = make(map[NotifierChan]struct{}) + go hub.listen() go func() { for { if msg, ok := <-LiveInformationCh; ok { - h.Notifier <- msg + hub.Notifier <- msg } } }() + return &hub } func (h *Hub) listen() { diff --git a/server/routes.go b/server/routes.go index 8c60a9c..2cba29f 100644 --- a/server/routes.go +++ b/server/routes.go @@ -26,7 +26,7 @@ func launchpad(w http.ResponseWriter, r *http.Request) { Title: "Godash", Bookmarks: bookmark.Bookmarks, Weather: weather.CurrentOpenWeather, - System: system.Live.System.Live, + System: system.Sys.Live, }) } @@ -56,7 +56,7 @@ func getWeather(w http.ResponseWriter, r *http.Request) { // @Router /system/live [get] func routeLiveSystem(w http.ResponseWriter, r *http.Request) { if system.Config.LiveSystem { - jsonResponse(w, system.Live.System.Live, http.StatusOK) + jsonResponse(w, system.Sys.Live, http.StatusOK) } else { jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent) } @@ -72,7 +72,7 @@ func routeLiveSystem(w http.ResponseWriter, r *http.Request) { // @Router /system/static [get] func routeStaticSystem(w http.ResponseWriter, r *http.Request) { if system.Config.LiveSystem { - jsonResponse(w, system.Live.System.Static, http.StatusOK) + jsonResponse(w, system.Sys.Static, http.StatusOK) } else { jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent) } @@ -85,9 +85,9 @@ func webSocket(w http.ResponseWriter, r *http.Request) { return } messageChan := make(hub.NotifierChan) - system.Live.Hub.NewClients <- messageChan + server.Hub.NewClients <- messageChan defer func() { - system.Live.Hub.ClosingClients <- messageChan + server.Hub.ClosingClients <- messageChan conn.Close() }() go readPump(conn) diff --git a/server/server.go b/server/server.go index 67fbb09..604be61 100644 --- a/server/server.go +++ b/server/server.go @@ -5,12 +5,14 @@ import ( "github.com/go-chi/chi/v5" "github.com/sirupsen/logrus" "godash/config" + "godash/hub" "godash/message" "net/http" ) type Server struct { Router *chi.Mux + Hub *hub.Hub Port int AllowedHosts []string `mapstructure:"ALLOWED_HOSTS"` Swagger bool @@ -21,6 +23,7 @@ var server = Server{} func NewServer() { config.ParseViperConfig(&server, config.AddViperConfig("server")) server.Router = chi.NewRouter() + server.Hub = hub.NewHub() server.setupMiddlewares() server.setupRouter() server.setupSwagger() diff --git a/system/system.go b/system/system.go index 51f1ccb..9fcf155 100644 --- a/system/system.go +++ b/system/system.go @@ -8,13 +8,12 @@ import ( ) var Config = SystemConfig{} -var Live = Service{} +var Sys = System{} func init() { config.ParseViperConfig(&Config, config.AddViperConfig("system")) if Config.LiveSystem { - Live.System.Initialize() - Live.Hub.Initialize() + Sys.Initialize() } } diff --git a/system/types.go b/system/types.go index 4287b3d..16f4d0a 100644 --- a/system/types.go +++ b/system/types.go @@ -1,7 +1,5 @@ package system -import "godash/hub" - type SystemConfig struct { LiveSystem bool `mapstructure:"LIVE_SYSTEM"` } @@ -29,11 +27,6 @@ type System struct { Static StaticInformation `json:"static" validate:"required"` } -type Service struct { - System System - Hub hub.Hub -} - type Storage struct { Readable string `json:"readable" validate:"required"` Value float64 `json:"value" validate:"required"`