package server import ( "embed" "fmt" "github.com/spf13/pflag" "html/template" "net" "net/http" "sakura/cfg" "sakura/server/api" "strconv" ) //go:embed web var web embed.FS var tmpl *template.Template func Start() { pflag.Parse() fmt.Println("Server started") addr := net.JoinHostPort(*cfg.Host, strconv.Itoa(*cfg.Port)) fmt.Printf("Listening on %s\n", addr) var err error tmpl, err = template.ParseFS(web, "web/**.gohtml") http.HandleFunc("/{$}", serveHome) http.HandleFunc(cfg.CreateMethod+" "+cfg.CreatePath, api.GameCreate) http.HandleFunc(cfg.ConnectMethod+" "+cfg.ConnectPath, api.GameConnect) err = http.ListenAndServe(addr, nil) if err != nil { panic(err) } } func serveHome(w http.ResponseWriter, r *http.Request) { err := tmpl.ExecuteTemplate(w, "index.gohtml", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }