-
Notifications
You must be signed in to change notification settings - Fork 0
/
merknera.go
92 lines (76 loc) · 1.94 KB
/
merknera.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/graphql-go/handler"
"github.com/mleonard87/merknera/gameworker"
"github.com/mleonard87/merknera/graphql"
"github.com/mleonard87/merknera/repository"
"github.com/mleonard87/merknera/schema"
"github.com/mleonard87/merknera/security"
"github.com/mleonard87/merknera/services"
"github.com/mleonard87/rpc"
"github.com/mleonard87/rpc/json"
)
func registerRPCHandler() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(services.RegistrationService), "")
http.Handle("/rpc", s)
}
func registerGraphQLHandler() {
schema := schema.MerkneraSchema()
h := graphql.NewCORSHandler(&handler.Config{
Schema: schema,
Pretty: true,
})
http.Handle("/graphql", h)
}
func registerStaticFileServerHandler() {
fs := http.FileServer(http.Dir("static"))
http.Handle("/", fs)
}
func registerAboutHandler() {
http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>Welcome to Merknera</h1>")
})
}
func registerLoginHandler() {
lih := security.LoginHandler{}
http.Handle("/login", lih)
loh := security.LogoutHandler{}
http.Handle("/logout", loh)
}
func verifyBotsAndQueueMoves() {
botList, err := repository.ListBots()
if err != nil {
log.Fatal(err)
}
for _, b := range botList {
b.Ping()
}
// Find all moves that are currently awaiting play and queue them.
awaitingMoves, err := repository.ListAwaitingMoves()
if err != nil {
log.Fatal(err)
}
for _, gm := range awaitingMoves {
gameworker.QueueGameMove(gm)
}
}
func main() {
registerRPCHandler()
registerGraphQLHandler()
graphiql := os.Getenv("MERKNERA_GRAPHIQL")
if graphiql == "TRUE" {
registerStaticFileServerHandler()
}
registerAboutHandler()
registerLoginHandler()
gameworker.StartGameMoveDispatcher(4)
go verifyBotsAndQueueMoves()
fmt.Println("Merknera is now listening on localhost:8080")
http.ListenAndServe(":8080", nil)
}