LastMUD/cmd/lastmudserver/main.go

71 lines
1,020 B
Go
Raw Normal View History

2025-06-19 16:22:55 +03:00
package main
import (
2025-06-20 16:26:39 +03:00
"context"
2025-06-19 16:22:55 +03:00
"log"
"os"
2025-06-20 16:26:39 +03:00
"os/signal"
"sync"
"syscall"
2025-06-24 16:37:26 +03:00
"time"
2025-06-19 16:22:55 +03:00
2025-06-28 01:04:18 +03:00
"net/http"
_ "net/http/pprof"
2025-06-20 16:26:39 +03:00
2025-06-28 01:04:18 +03:00
"code.haedhutner.dev/mvv/LastMUD/internal/server"
2025-06-20 16:26:39 +03:00
"golang.org/x/term"
2025-06-19 16:22:55 +03:00
)
func main() {
2025-06-20 16:26:39 +03:00
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
defer wg.Wait()
defer cancel()
2025-06-19 16:22:55 +03:00
2025-06-20 16:26:39 +03:00
_, err := server.CreateServer(ctx, &wg, ":8000")
2025-06-19 16:22:55 +03:00
if err != nil {
log.Fatal(err)
}
2025-06-28 01:04:18 +03:00
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
2025-06-20 16:26:39 +03:00
processInput()
}
func processInput() {
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
2025-06-19 16:22:55 +03:00
2025-06-20 16:26:39 +03:00
buf := make([]byte, 1)
2025-06-19 16:22:55 +03:00
for {
2025-06-20 16:26:39 +03:00
// If interrupt received, stop
select {
case <-sigChan:
return
default:
}
// TODO: Proper TUI for the server
os.Stdin.Read(buf)
2025-06-19 16:22:55 +03:00
2025-06-20 16:26:39 +03:00
if buf[0] == 'q' {
2025-06-19 16:22:55 +03:00
return
}
2025-06-24 16:37:26 +03:00
time.Sleep(50 * time.Millisecond)
2025-06-19 16:22:55 +03:00
}
}