forked from AaronStGeorge/tanks
/
main.go
112 lines (91 loc) · 2.71 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"database/sql"
"fmt"
"github.com/apcera/nats"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"html/template"
"log"
"net/http"
)
type Global struct {
store *sessions.CookieStore
db *sql.DB
ec *nats.EncodedConn
}
var templates = map[string]*template.Template{
"index": template.Must(template.ParseFiles("templates/main.tmpl",
"templates/header.tmpl", "templates/footer.tmpl")),
}
type User struct {
UserName string
Id int
// Only this is listening to fmt.Sprintf("x%d", user.Id) this is this
// users "phoneNumber".
PhoneNumber string
// Every user intersted in information about user is listeneing on
// string(user.Id) this is their twitter.
Twitter string
}
func main() {
// create session store
store := sessions.NewCookieStore([]byte("nRrHLlHcHH0u7fUz25Hje9m7uJ5SnJzP"))
// store options
store.Options = &sessions.Options{
Path: "/",
Domain: "aaronstgeorge.co",
MaxAge: 0,
}
// open database connection
db, err := sql.Open("mysql", "root:@/Tanks")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// test connection
err = db.Ping()
if err != nil {
log.Fatal(err)
}
// create connection to nats server
nc, _ := nats.Connect(nats.DefaultURL)
ec, _ := nats.NewEncodedConn(nc, "json")
defer ec.Close()
global := &Global{db: db, store: store, ec: ec}
stdChain := alice.New(global.loadSession)
loadUser := stdChain.Append(global.loadUser)
loadUserData := loadUser.Append(global.loadFriends)
router := httprouter.New()
// mainPage
router.Handler("GET", "/", loadUserData.Then(http.HandlerFunc(mainPage)))
// login
router.HandlerFunc("GET", "/login", loginGET)
router.Handler("POST", "/login", stdChain.Then(
http.HandlerFunc(global.loginPOST)))
// logout
router.Handler("GET", "/logout", stdChain.Then(http.HandlerFunc(logout)))
// play
router.HandlerFunc("GET", "/play", http.HandlerFunc(play))
// game websocket
router.Handler("GET", "/gpws", loadUser.Then(http.HandlerFunc(
global.gamePageWs)))
// mainPage websocket
router.Handler("GET", "/mpws", loadUserData.Then(http.HandlerFunc(
global.mainPageWs)))
// register
router.HandlerFunc("GET", "/register", registerGET)
router.Handler("POST", "/register", stdChain.Then(
http.HandlerFunc(global.registerPOST)))
// add friend
router.HandlerFunc("GET", "/friend", friendGET)
router.Handler("POST", "/friend", loadUser.Then(
http.HandlerFunc(global.friendPOST)))
// Serve static files from the ./public directory
router.ServeFiles("/static/*filepath", http.Dir("./public/"))
fmt.Println("serving...")
log.Fatal(http.ListenAndServe(":80", context.ClearHandler(router)))
}