Kaynağa Gözat

Move hub from system to router

Florian Hoss 2 yıl önce
ebeveyn
işleme
19d32f1e5c
5 değiştirilmiş dosya ile 19 ekleme ve 22 silme
  1. 9 7
      hub/hub.go
  2. 5 5
      server/routes.go
  3. 3 0
      server/server.go
  4. 2 3
      system/system.go
  5. 0 7
      system/types.go

+ 9 - 7
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() {

+ 5 - 5
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)

+ 3 - 0
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()

+ 2 - 3
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()
 	}
 }
 

+ 0 - 7
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"`