refactor: web server returns *http.Server for shutdown control
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
10
main.go
10
main.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/tolelom/catacombs/config"
|
"github.com/tolelom/catacombs/config"
|
||||||
"github.com/tolelom/catacombs/game"
|
"github.com/tolelom/catacombs/game"
|
||||||
@@ -41,12 +42,9 @@ func main() {
|
|||||||
sshAddr := fmt.Sprintf("0.0.0.0:%d", cfg.Server.SSHPort)
|
sshAddr := fmt.Sprintf("0.0.0.0:%d", cfg.Server.SSHPort)
|
||||||
webAddr := fmt.Sprintf(":%d", cfg.Server.HTTPPort)
|
webAddr := fmt.Sprintf(":%d", cfg.Server.HTTPPort)
|
||||||
|
|
||||||
// Start web terminal server in background
|
startTime := time.Now()
|
||||||
go func() {
|
webServer := web.Start(webAddr, cfg.Server.SSHPort, lobby, db, startTime)
|
||||||
if err := web.Start(webAddr, cfg.Server.SSHPort); err != nil {
|
_ = webServer // used later for graceful shutdown
|
||||||
slog.Error("web server error", "error", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
slog.Info("server starting", "ssh_port", cfg.Server.SSHPort, "http_port", cfg.Server.HTTPPort)
|
slog.Info("server starting", "ssh_port", cfg.Server.SSHPort, "http_port", cfg.Server.HTTPPort)
|
||||||
if err := server.Start(sshAddr, lobby, db); err != nil {
|
if err := server.Start(sshAddr, lobby, db); err != nil {
|
||||||
|
|||||||
@@ -8,9 +8,12 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
|
"github.com/tolelom/catacombs/game"
|
||||||
|
"github.com/tolelom/catacombs/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed static
|
//go:embed static
|
||||||
@@ -26,8 +29,8 @@ type resizeMsg struct {
|
|||||||
Rows int `json:"rows"`
|
Rows int `json:"rows"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start launches the HTTP server for the web terminal.
|
// Start launches the HTTP server for the web terminal and returns the server handle.
|
||||||
func Start(addr string, sshPort int) error {
|
func Start(addr string, sshPort int, lobby *game.Lobby, db *store.DB, startTime time.Time) *http.Server {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
// Serve static files from embedded FS
|
// Serve static files from embedded FS
|
||||||
@@ -38,8 +41,19 @@ func Start(addr string, sshPort int) error {
|
|||||||
handleWS(w, r, sshPort)
|
handleWS(w, r, sshPort)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Admin endpoint
|
||||||
|
mux.Handle("/admin", AdminHandler(lobby, db, startTime))
|
||||||
|
|
||||||
|
srv := &http.Server{Addr: addr, Handler: mux}
|
||||||
|
|
||||||
slog.Info("starting web terminal", "addr", addr)
|
slog.Info("starting web terminal", "addr", addr)
|
||||||
return http.ListenAndServe(addr, mux)
|
go func() {
|
||||||
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
slog.Error("web server error", "error", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleWS(w http.ResponseWriter, r *http.Request, sshPort int) {
|
func handleWS(w http.ResponseWriter, r *http.Request, sshPort int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user