59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"gralias/config"
|
|
"gralias/handlers"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var cfg *config.Config
|
|
|
|
func init() {
|
|
cfg = config.LoadConfigOrDefault("")
|
|
}
|
|
|
|
func ListenToRequests(port string) error {
|
|
mux := http.NewServeMux()
|
|
server := &http.Server{
|
|
Handler: handlers.LogRequests(handlers.GetSession(mux)),
|
|
Addr: ":" + port,
|
|
ReadTimeout: time.Second * 5, // TODO: to cfg
|
|
WriteTimeout: 0, // sse streaming
|
|
}
|
|
fs := http.FileServer(http.Dir("assets/"))
|
|
mux.Handle("GET /assets/", http.StripPrefix("/assets/", fs))
|
|
//
|
|
mux.HandleFunc("GET /ping", handlers.HandlePing)
|
|
mux.HandleFunc("GET /", handlers.HandleHome)
|
|
mux.HandleFunc("POST /login", handlers.HandleFrontLogin)
|
|
mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam)
|
|
mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn)
|
|
mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom)
|
|
mux.HandleFunc("GET /start-game", handlers.HandleStartGame)
|
|
mux.HandleFunc("GET /room-join", handlers.HandleJoinRoom)
|
|
mux.HandleFunc("POST /give-clue", handlers.HandleGiveClue)
|
|
mux.HandleFunc("GET /room/exit", handlers.HandleExit)
|
|
//elements
|
|
mux.HandleFunc("GET /actionhistory", handlers.HandleActionHistory)
|
|
mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm)
|
|
mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm)
|
|
mux.HandleFunc("GET /word/show-color", handlers.HandleShowColor)
|
|
mux.HandleFunc("POST /check/name", handlers.HandleNameCheck)
|
|
mux.HandleFunc("GET /add-bot", handlers.HandleAddBot)
|
|
// special
|
|
mux.HandleFunc("GET /renotify-bot", handlers.HandleRenotifyBot)
|
|
// sse
|
|
mux.Handle("GET /sub/sse", handlers.Notifier)
|
|
slog.Info("Listening", "addr", port)
|
|
return server.ListenAndServe()
|
|
}
|
|
|
|
func main() {
|
|
err := ListenToRequests(cfg.ServerConfig.Port)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|