Move hub from system to router

This commit is contained in:
Florian Hoss 2022-10-21 12:55:29 +02:00
parent ac2deea60c
commit 19d32f1e5c
5 changed files with 19 additions and 22 deletions

View file

@ -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() {

View file

@ -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)

View file

@ -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()

View file

@ -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()
}
}

View file

@ -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"`