This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (47 loc) · 1.76 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/robyzzz/csl-backend/config"
"github.com/robyzzz/csl-backend/controller"
"github.com/robyzzz/csl-backend/middleware"
"github.com/robyzzz/csl-backend/model"
"github.com/robyzzz/csl-backend/websocket"
)
var router *mux.Router
// start server
func main() {
config.GetEnvVariables()
model.Connect()
setupRouter()
log.Printf("Server initialized and listening on %s\n", config.PORT)
log.Fatal(http.ListenAndServe(":"+config.PORT, router))
}
func setupRouter() {
router = mux.NewRouter()
router.HandleFunc("/", controller.Home)
// auth
router.HandleFunc("/login", controller.Login).Methods("GET")
router.HandleFunc("/logout", controller.Logout).Methods("GET")
router.Handle("/auth", middleware.BeforeAuth(controller.Auth)).Methods("PUT", "OPTIONS")
// steam_user (steam data)
router.Handle("/profile", middleware.IsAuthenticated(controller.GetProfile))
router.HandleFunc("/api/steam", controller.GetAllSteamUsers).Methods("GET")
router.HandleFunc("/api/steam/{steamid}", controller.GetSteamUser).Methods("GET")
// player_stats (game statistics)
router.HandleFunc("/api/players", controller.GetAllPlayers).Methods("GET")
router.HandleFunc("/api/player/{steamid}", controller.GetPlayer).Methods("GET")
// hub/lobby
websocket.Hub = websocket.NewHub()
router.HandleFunc("/api/rooms", websocket.HandleHub) //TODO: apply isauth middleware after everything is done
go websocket.RunHub()
// rooms (TODO: no hardcode range)
for _, uid := range []int{1, 2, 3, 4, 5} {
newRoom := websocket.NewRoom(uid, 3)
websocket.AddRoomToHub(newRoom)
router.HandleFunc("/api/room/"+fmt.Sprint(uid), newRoom.HandleRoom) //TODO: apply isauth middleware after everything is done
go newRoom.Run()
}
}