forked from jhcook/game_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
41 lines (37 loc) · 1.26 KB
/
engine.go
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 main
import (
"github.com/jhcook/game_engine/hangman"
"log"
"net/http"
"golang.org/x/net/websocket"
"math/rand"
"time"
)
func apiHandler(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("%v: %v from %v", r.Method, r.URL.Path, r.RemoteAddr)
next.ServeHTTP(w, r)
}
}
func apiHandlerFunc(next http.HandlerFunc) http.HandlerFunc {
return apiHandler(next)
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
// Create the singleton "men" in hangman to keep track of all hangman episodes
hangman.NewMen()
// http.Handle("/list", apiHandlerFunc())
// http.Handle("/new", apiHandlerFunc())
// http.Handle("/status", apiHandlerFunc())
// For websockets we need to use HandleFunc to override the http Origin header.
// http://stackoverflow.com/questions/19708330/serving-a-websocket-in-go
// Otherwise, we will get a 403 and waste a night trying to debug :)
http.HandleFunc("/wshangman", func(w http.ResponseWriter, req *http.Request) {
s := websocket.Server{Handler: websocket.Handler(hangman.Playws)}
s.ServeHTTP(w, req)
})
//}websocket.Handler(hangman.Playws))
// Provide an HTTP interface
http.Handle("/hangman", apiHandlerFunc(hangman.Playhttp))
http.ListenAndServe(":3000", nil)
}