refactor: web server returns *http.Server for shutdown control

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-25 17:36:15 +09:00
parent a38cf804ef
commit 6e78d8a073
2 changed files with 21 additions and 9 deletions

View File

@@ -8,9 +8,12 @@ import (
"log/slog"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"golang.org/x/crypto/ssh"
"github.com/tolelom/catacombs/game"
"github.com/tolelom/catacombs/store"
)
//go:embed static
@@ -26,8 +29,8 @@ type resizeMsg struct {
Rows int `json:"rows"`
}
// Start launches the HTTP server for the web terminal.
func Start(addr string, sshPort int) error {
// Start launches the HTTP server for the web terminal and returns the server handle.
func Start(addr string, sshPort int, lobby *game.Lobby, db *store.DB, startTime time.Time) *http.Server {
mux := http.NewServeMux()
// Serve static files from embedded FS
@@ -38,8 +41,19 @@ func Start(addr string, sshPort int) error {
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)
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) {