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 var LiveInformationCh chan Message
func (h *Hub) Initialize() { func NewHub() *Hub {
hub := Hub{}
LiveInformationCh = make(chan Message) LiveInformationCh = make(chan Message)
h.Notifier = make(NotifierChan) hub.Notifier = make(NotifierChan)
h.NewClients = make(chan NotifierChan) hub.NewClients = make(chan NotifierChan)
h.ClosingClients = make(chan NotifierChan) hub.ClosingClients = make(chan NotifierChan)
h.clients = make(map[NotifierChan]struct{}) hub.clients = make(map[NotifierChan]struct{})
go h.listen() go hub.listen()
go func() { go func() {
for { for {
if msg, ok := <-LiveInformationCh; ok { if msg, ok := <-LiveInformationCh; ok {
h.Notifier <- msg hub.Notifier <- msg
} }
} }
}() }()
return &hub
} }
func (h *Hub) listen() { func (h *Hub) listen() {

View file

@ -26,7 +26,7 @@ func launchpad(w http.ResponseWriter, r *http.Request) {
Title: "Godash", Title: "Godash",
Bookmarks: bookmark.Bookmarks, Bookmarks: bookmark.Bookmarks,
Weather: weather.CurrentOpenWeather, 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] // @Router /system/live [get]
func routeLiveSystem(w http.ResponseWriter, r *http.Request) { func routeLiveSystem(w http.ResponseWriter, r *http.Request) {
if system.Config.LiveSystem { if system.Config.LiveSystem {
jsonResponse(w, system.Live.System.Live, http.StatusOK) jsonResponse(w, system.Sys.Live, http.StatusOK)
} else { } else {
jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent) 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] // @Router /system/static [get]
func routeStaticSystem(w http.ResponseWriter, r *http.Request) { func routeStaticSystem(w http.ResponseWriter, r *http.Request) {
if system.Config.LiveSystem { if system.Config.LiveSystem {
jsonResponse(w, system.Live.System.Static, http.StatusOK) jsonResponse(w, system.Sys.Static, http.StatusOK)
} else { } else {
jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent) jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
} }
@ -85,9 +85,9 @@ func webSocket(w http.ResponseWriter, r *http.Request) {
return return
} }
messageChan := make(hub.NotifierChan) messageChan := make(hub.NotifierChan)
system.Live.Hub.NewClients <- messageChan server.Hub.NewClients <- messageChan
defer func() { defer func() {
system.Live.Hub.ClosingClients <- messageChan server.Hub.ClosingClients <- messageChan
conn.Close() conn.Close()
}() }()
go readPump(conn) go readPump(conn)

View file

@ -5,12 +5,14 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"godash/config" "godash/config"
"godash/hub"
"godash/message" "godash/message"
"net/http" "net/http"
) )
type Server struct { type Server struct {
Router *chi.Mux Router *chi.Mux
Hub *hub.Hub
Port int Port int
AllowedHosts []string `mapstructure:"ALLOWED_HOSTS"` AllowedHosts []string `mapstructure:"ALLOWED_HOSTS"`
Swagger bool Swagger bool
@ -21,6 +23,7 @@ var server = Server{}
func NewServer() { func NewServer() {
config.ParseViperConfig(&server, config.AddViperConfig("server")) config.ParseViperConfig(&server, config.AddViperConfig("server"))
server.Router = chi.NewRouter() server.Router = chi.NewRouter()
server.Hub = hub.NewHub()
server.setupMiddlewares() server.setupMiddlewares()
server.setupRouter() server.setupRouter()
server.setupSwagger() server.setupSwagger()

View file

@ -8,13 +8,12 @@ import (
) )
var Config = SystemConfig{} var Config = SystemConfig{}
var Live = Service{} var Sys = System{}
func init() { func init() {
config.ParseViperConfig(&Config, config.AddViperConfig("system")) config.ParseViperConfig(&Config, config.AddViperConfig("system"))
if Config.LiveSystem { if Config.LiveSystem {
Live.System.Initialize() Sys.Initialize()
Live.Hub.Initialize()
} }
} }

View file

@ -1,7 +1,5 @@
package system package system
import "godash/hub"
type SystemConfig struct { type SystemConfig struct {
LiveSystem bool `mapstructure:"LIVE_SYSTEM"` LiveSystem bool `mapstructure:"LIVE_SYSTEM"`
} }
@ -29,11 +27,6 @@ type System struct {
Static StaticInformation `json:"static" validate:"required"` Static StaticInformation `json:"static" validate:"required"`
} }
type Service struct {
System System
Hub hub.Hub
}
type Storage struct { type Storage struct {
Readable string `json:"readable" validate:"required"` Readable string `json:"readable" validate:"required"`
Value float64 `json:"value" validate:"required"` Value float64 `json:"value" validate:"required"`