blob: 798bab69c2a61053eadff9af6a3cb0a7f2518bd4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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)
}
}
|