@@ -305,51 +305,6 @@ func Listen(wg *sync.WaitGroup, webSocketHandler func(*websocket.Conn)) {
305305 doBasicAuth (roomData ),
306306 ))
307307
308- //
309- // Http server start up
310- //
311-
312- if networkConfig .HttpPort > 0 {
313-
314- httpServer = & http.Server {
315- Addr : fmt .Sprintf (`:%d` , networkConfig .HttpPort ),
316- }
317-
318- if networkConfig .HttpsRedirect && networkConfig .HttpsPort != 0 {
319-
320- var redirectHandler http.HandlerFunc = func (w http.ResponseWriter , r * http.Request ) {
321-
322- host := r .Host
323-
324- // If the host header includes a port (e.g. "example.com:80"), strip it out.
325- if strings .Contains (host , ":" ) {
326- host , _ , _ = net .SplitHostPort (host )
327- }
328-
329- // Build the target URL with your known HTTPS port (443 in this case).
330- target := fmt .Sprintf ("https://%s:%d%s" , host , networkConfig .HttpsPort , r .RequestURI )
331-
332- http .Redirect (w , r , target , http .StatusMovedPermanently )
333-
334- }
335-
336- httpServer .Handler = redirectHandler
337-
338- }
339-
340- // HTTP Server
341- wg .Add (1 )
342-
343- mudlog .Info ("HTTP" , "stage" , "Starting http server" , "port" , networkConfig .HttpPort )
344- go func () {
345- defer wg .Done ()
346-
347- if err := httpServer .ListenAndServe (); err != nil && err != http .ErrServerClosed {
348- mudlog .Error ("HTTP" , "error" , fmt .Errorf ("Error starting web server: %w" , err ))
349- }
350- }()
351- }
352-
353308 //
354309 // Https server start up
355310 //
@@ -360,7 +315,7 @@ func Listen(wg *sync.WaitGroup, webSocketHandler func(*websocket.Conn)) {
360315
361316 if len (filePaths .HttpsCertFile ) == 0 || len (filePaths .HttpsKeyFile ) == 0 {
362317
363- mudlog .Info ("HTTPS" , "stage" , "skipping" , "error" , "Undefined key file " , "Public Cert" , filePaths .HttpsCertFile , "Private Key" , filePaths .HttpsKeyFile )
318+ mudlog .Info ("HTTPS" , "stage" , "skipping" , "error" , "Undefined public/private key files " , "Public Cert" , filePaths .HttpsCertFile , "Private Key" , filePaths .HttpsKeyFile )
364319
365320 } else {
366321
@@ -399,6 +354,57 @@ func Listen(wg *sync.WaitGroup, webSocketHandler func(*websocket.Conn)) {
399354 }
400355 }
401356
357+ //
358+ // Http server start up
359+ //
360+
361+ if networkConfig .HttpPort > 0 {
362+
363+ httpServer = & http.Server {
364+ Addr : fmt .Sprintf (`:%d` , networkConfig .HttpPort ),
365+ }
366+
367+ if networkConfig .HttpsRedirect {
368+
369+ if httpsServer == nil {
370+
371+ mudlog .Error ("HTTP" , "error" , "Cannot enable https redirect. There is no https server configured/running." )
372+
373+ } else {
374+
375+ var redirectHandler http.HandlerFunc = func (w http.ResponseWriter , r * http.Request ) {
376+
377+ host := r .Host
378+ // If the host header includes a port (e.g. "example.com:80"), strip it out.
379+ if strings .Contains (host , ":" ) {
380+ host , _ , _ = net .SplitHostPort (host )
381+ }
382+
383+ // Build the target URL with your known HTTPS port (443 in this case).
384+ target := fmt .Sprintf ("https://%s:%d%s" , host , networkConfig .HttpsPort , r .RequestURI )
385+
386+ http .Redirect (w , r , target , http .StatusMovedPermanently )
387+ }
388+
389+ httpServer .Handler = redirectHandler
390+
391+ }
392+
393+ }
394+
395+ // HTTP Server
396+ wg .Add (1 )
397+
398+ mudlog .Info ("HTTP" , "stage" , "Starting http server" , "port" , networkConfig .HttpPort )
399+ go func () {
400+ defer wg .Done ()
401+
402+ if err := httpServer .ListenAndServe (); err != nil && err != http .ErrServerClosed {
403+ mudlog .Error ("HTTP" , "error" , fmt .Errorf ("Error starting web server: %w" , err ))
404+ }
405+ }()
406+ }
407+
402408}
403409
404410// This wraps the handler functiojn with a game lock (mutex) to keep the mud from
0 commit comments