mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-20 18:43:41 -05:00
cmd/gomuks: move most things into new package
This commit is contained in:
parent
540e8fa43e
commit
5701bbf708
6 changed files with 46 additions and 31 deletions
|
@ -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) {
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package main
|
||||
package gomuks
|
||||
|
||||
import (
|
||||
"errors"
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package main
|
||||
package gomuks
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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 {
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package main
|
||||
package gomuks
|
||||
|
||||
import (
|
||||
"context"
|
Loading…
Add table
Reference in a new issue