From 5701bbf7087696248f60db5ef96fce0095cca997 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 8 Nov 2024 10:09:17 +0100 Subject: [PATCH] cmd/gomuks: move most things into new package --- cmd/gomuks/main.go | 28 +++++++-------------------- {cmd => pkg}/gomuks/config.go | 2 +- {cmd => pkg}/gomuks/gomuks.go | 33 ++++++++++++++++++++++++++++++-- {cmd => pkg}/gomuks/media.go | 2 +- {cmd => pkg}/gomuks/server.go | 10 +++++----- {cmd => pkg}/gomuks/websocket.go | 2 +- 6 files changed, 46 insertions(+), 31 deletions(-) rename {cmd => pkg}/gomuks/config.go (99%) rename {cmd => pkg}/gomuks/gomuks.go (90%) rename {cmd => pkg}/gomuks/media.go (99%) rename {cmd => pkg}/gomuks/server.go (96%) rename {cmd => pkg}/gomuks/websocket.go (99%) diff --git a/cmd/gomuks/main.go b/cmd/gomuks/main.go index 1d85fff..f77554e 100644 --- a/cmd/gomuks/main.go +++ b/cmd/gomuks/main.go @@ -28,6 +28,7 @@ import ( flag "maunium.net/go/mauflag" "maunium.net/go/mautrix" + "go.mau.fi/gomuks/pkg/gomuks" "go.mau.fi/gomuks/pkg/hicli" ) @@ -71,27 +72,12 @@ func main() { os.Exit(0) } - gmx := NewGomuks() - gmx.InitDirectories() - err = gmx.LoadConfig() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to load config:", err) - os.Exit(9) - } - gmx.SetupLog() - gmx.Log.Info(). - Str("version", Version). - Str("go_version", runtime.Version()). - Time("built_at", ParsedBuildTime). - Msg("Initializing gomuks") - gmx.StartServer() - gmx.StartClient() - gmx.Log.Info().Msg("Initialization complete") - gmx.WaitForInterrupt() - gmx.Log.Info().Msg("Shutting down...") - gmx.directStop() - gmx.Log.Info().Msg("Shutdown complete") - os.Exit(0) + gmx := gomuks.NewGomuks() + gmx.Version = Version + gmx.Commit = Commit + gmx.LinkifiedVersion = LinkifiedVersion + gmx.BuildTime = ParsedBuildTime + gmx.Run() } func initVersion(tag, commit, rawBuildTime string) { diff --git a/cmd/gomuks/config.go b/pkg/gomuks/config.go similarity index 99% rename from cmd/gomuks/config.go rename to pkg/gomuks/config.go index 6da120a..ff64725 100644 --- a/cmd/gomuks/config.go +++ b/pkg/gomuks/config.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package main +package gomuks import ( "errors" diff --git a/cmd/gomuks/gomuks.go b/pkg/gomuks/gomuks.go similarity index 90% rename from cmd/gomuks/gomuks.go rename to pkg/gomuks/gomuks.go index 7e23e25..b2919aa 100644 --- a/cmd/gomuks/gomuks.go +++ b/pkg/gomuks/gomuks.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package main +package gomuks import ( "context" @@ -28,6 +28,7 @@ import ( "slices" "sync" "syscall" + "time" "github.com/coder/websocket" "github.com/rs/zerolog" @@ -43,6 +44,11 @@ type Gomuks struct { Server *http.Server Client *hicli.HiClient + Version string + Commit string + LinkifiedVersion string + BuildTime time.Time + ConfigDir string DataDir string CacheDir string @@ -194,7 +200,7 @@ func (gmx *Gomuks) WaitForInterrupt() { } } -func (gmx *Gomuks) directStop() { +func (gmx *Gomuks) DirectStop() { gmx.eventListenersLock.Lock() closers := slices.Collect(maps.Values(gmx.websocketClosers)) gmx.eventListenersLock.Unlock() @@ -232,3 +238,26 @@ func (gmx *Gomuks) SubscribeEvents(closeForRestart WebsocketCloseFunc, cb func(c delete(gmx.websocketClosers, id) } } + +func (gmx *Gomuks) Run() { + gmx.InitDirectories() + err := gmx.LoadConfig() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, "Failed to load config:", err) + os.Exit(9) + } + gmx.SetupLog() + gmx.Log.Info(). + Str("version", gmx.Version). + Str("go_version", runtime.Version()). + Time("built_at", gmx.BuildTime). + Msg("Initializing gomuks") + gmx.StartServer() + gmx.StartClient() + gmx.Log.Info().Msg("Initialization complete") + gmx.WaitForInterrupt() + gmx.Log.Info().Msg("Shutting down...") + gmx.DirectStop() + gmx.Log.Info().Msg("Shutdown complete") + os.Exit(0) +} diff --git a/cmd/gomuks/media.go b/pkg/gomuks/media.go similarity index 99% rename from cmd/gomuks/media.go rename to pkg/gomuks/media.go index e5bb635..c948a76 100644 --- a/cmd/gomuks/media.go +++ b/pkg/gomuks/media.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package main +package gomuks import ( "context" diff --git a/cmd/gomuks/server.go b/pkg/gomuks/server.go similarity index 96% rename from cmd/gomuks/server.go rename to pkg/gomuks/server.go index 67dfea7..ea3631e 100644 --- a/cmd/gomuks/server.go +++ b/pkg/gomuks/server.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package main +package gomuks import ( "crypto/hmac" @@ -65,7 +65,7 @@ func (gmx *Gomuks) StartServer() { if frontend, err := fs.Sub(web.Frontend, "dist"); err != nil { gmx.Log.Warn().Msg("Frontend not found") } else { - router.Handle("/", FrontendCacheMiddleware(http.FileServerFS(frontend))) + router.Handle("/", gmx.FrontendCacheMiddleware(http.FileServerFS(frontend))) } gmx.Server = &http.Server{ Addr: gmx.Config.Web.ListenAddress, @@ -80,10 +80,10 @@ func (gmx *Gomuks) StartServer() { gmx.Log.Info().Str("address", gmx.Config.Web.ListenAddress).Msg("Server started") } -func FrontendCacheMiddleware(next http.Handler) http.Handler { +func (gmx *Gomuks) FrontendCacheMiddleware(next http.Handler) http.Handler { var frontendCacheETag string - if Commit != "unknown" && !ParsedBuildTime.IsZero() { - frontendCacheETag = fmt.Sprintf(`"%s-%s"`, Commit, ParsedBuildTime.Format(time.RFC3339)) + if gmx.Commit != "unknown" && !gmx.BuildTime.IsZero() { + frontendCacheETag = fmt.Sprintf(`"%s-%s"`, gmx.Commit, gmx.BuildTime.Format(time.RFC3339)) } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("If-None-Match") == frontendCacheETag { diff --git a/cmd/gomuks/websocket.go b/pkg/gomuks/websocket.go similarity index 99% rename from cmd/gomuks/websocket.go rename to pkg/gomuks/websocket.go index cad15e7..f06f7b6 100644 --- a/cmd/gomuks/websocket.go +++ b/pkg/gomuks/websocket.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package main +package gomuks import ( "context"