/
server.go
85 lines (64 loc) · 1.6 KB
/
server.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
package server
import (
"net/http"
"sync"
socketio "github.com/googollee/go-socket.io"
"github.com/gorilla/mux"
"github.com/sabhiram/fast-for-odin/room"
)
type ErrRoomNotFound struct{}
func (e *ErrRoomNotFound) Error() string {
return "room not found"
}
type ErrRoomExists struct{}
func (e *ErrRoomExists) Error() string {
return "room already exists"
}
type Server struct {
*http.Server // composed of a http.Server instance
io *socketio.Server // ... which contains an instance of a socket.io thingy
address string // server host address
roomLock sync.RWMutex
rooms map[string]*room.Room // map of rooms
}
func New(address string) (*Server, error) {
router := mux.NewRouter()
s := &Server{
Server: &http.Server{
Addr: address,
},
address: address,
rooms: make(map[string]*room.Room),
}
sio, err := s.SocketIOHandler()
if err != nil {
return nil, err
}
s.io = sio
// Application interface
router.HandleFunc("/api/create/room", s.HTTPPostNewRoomHandler())
// Handle socket.io stuff
router.Handle("/socket.io/", sio)
// Handle static routes
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static"))) // TODO:
s.Handler = router
return s, nil
}
func (s *Server) RegisterRoom(r *room.Room) error {
s.roomLock.Lock()
defer s.roomLock.Unlock()
if _, ok := s.rooms[r.GetID()]; ok {
return &ErrRoomExists{}
} else {
s.rooms[r.GetID()] = r
}
return nil
}
func (s *Server) GetRoom(id string) (*room.Room, error) {
s.roomLock.RLock()
defer s.roomLock.RUnlock()
if r, ok := s.rooms[id]; ok {
return r, nil
}
return nil, &ErrRoomNotFound{}
}