forked from Mirrors/gomuks
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"
|
flag "maunium.net/go/mauflag"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
|
|
||||||
|
"go.mau.fi/gomuks/pkg/gomuks"
|
||||||
"go.mau.fi/gomuks/pkg/hicli"
|
"go.mau.fi/gomuks/pkg/hicli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,27 +72,12 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
gmx := NewGomuks()
|
gmx := gomuks.NewGomuks()
|
||||||
gmx.InitDirectories()
|
gmx.Version = Version
|
||||||
err = gmx.LoadConfig()
|
gmx.Commit = Commit
|
||||||
if err != nil {
|
gmx.LinkifiedVersion = LinkifiedVersion
|
||||||
_, _ = fmt.Fprintln(os.Stderr, "Failed to load config:", err)
|
gmx.BuildTime = ParsedBuildTime
|
||||||
os.Exit(9)
|
gmx.Run()
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initVersion(tag, commit, rawBuildTime string) {
|
func initVersion(tag, commit, rawBuildTime string) {
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package gomuks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package gomuks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -28,6 +28,7 @@ import (
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
@ -43,6 +44,11 @@ type Gomuks struct {
|
||||||
Server *http.Server
|
Server *http.Server
|
||||||
Client *hicli.HiClient
|
Client *hicli.HiClient
|
||||||
|
|
||||||
|
Version string
|
||||||
|
Commit string
|
||||||
|
LinkifiedVersion string
|
||||||
|
BuildTime time.Time
|
||||||
|
|
||||||
ConfigDir string
|
ConfigDir string
|
||||||
DataDir string
|
DataDir string
|
||||||
CacheDir string
|
CacheDir string
|
||||||
|
@ -194,7 +200,7 @@ func (gmx *Gomuks) WaitForInterrupt() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gmx *Gomuks) directStop() {
|
func (gmx *Gomuks) DirectStop() {
|
||||||
gmx.eventListenersLock.Lock()
|
gmx.eventListenersLock.Lock()
|
||||||
closers := slices.Collect(maps.Values(gmx.websocketClosers))
|
closers := slices.Collect(maps.Values(gmx.websocketClosers))
|
||||||
gmx.eventListenersLock.Unlock()
|
gmx.eventListenersLock.Unlock()
|
||||||
|
@ -232,3 +238,26 @@ func (gmx *Gomuks) SubscribeEvents(closeForRestart WebsocketCloseFunc, cb func(c
|
||||||
delete(gmx.websocketClosers, id)
|
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
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package gomuks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package gomuks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
|
@ -65,7 +65,7 @@ func (gmx *Gomuks) StartServer() {
|
||||||
if frontend, err := fs.Sub(web.Frontend, "dist"); err != nil {
|
if frontend, err := fs.Sub(web.Frontend, "dist"); err != nil {
|
||||||
gmx.Log.Warn().Msg("Frontend not found")
|
gmx.Log.Warn().Msg("Frontend not found")
|
||||||
} else {
|
} else {
|
||||||
router.Handle("/", FrontendCacheMiddleware(http.FileServerFS(frontend)))
|
router.Handle("/", gmx.FrontendCacheMiddleware(http.FileServerFS(frontend)))
|
||||||
}
|
}
|
||||||
gmx.Server = &http.Server{
|
gmx.Server = &http.Server{
|
||||||
Addr: gmx.Config.Web.ListenAddress,
|
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")
|
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
|
var frontendCacheETag string
|
||||||
if Commit != "unknown" && !ParsedBuildTime.IsZero() {
|
if gmx.Commit != "unknown" && !gmx.BuildTime.IsZero() {
|
||||||
frontendCacheETag = fmt.Sprintf(`"%s-%s"`, Commit, ParsedBuildTime.Format(time.RFC3339))
|
frontendCacheETag = fmt.Sprintf(`"%s-%s"`, gmx.Commit, gmx.BuildTime.Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Header.Get("If-None-Match") == frontendCacheETag {
|
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
|
// 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/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package gomuks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
Loading…
Add table
Reference in a new issue