This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
166 lines (140 loc) · 4.15 KB
/
api.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package api
import (
"context"
"log"
"os"
"sync"
"time"
"github.com/gomodule/redigo/redis"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// RoomAPIServer is room api server.
type RoomAPIServer struct {
// Address is room api server address.
Address string
// BaseUri is base uri of room api.
BaseUri string
// RedisHost is redis address.
RedisHost string
// MaxUser is max value of room capacity.
MaxUser int
ServerDeadLine time.Duration
RoomDeadLine time.Duration
Logger *log.Logger
serverManager *ServerManager
roomManager *RoomManager
}
const (
defaultAddress = ":80"
defaultBaseUri = "/api/v1"
defaultRedisHost = ":6379"
defaultMaxUser = 70
defaultServerDeadline = time.Minute * 5
defaultRoomDeadline = time.Minute * 5
)
// NewRoomAPIServer is an instance of RoomAPIServer.
func NewRoomAPIServer() *RoomAPIServer {
return &RoomAPIServer{
Address: defaultAddress,
BaseUri: defaultBaseUri,
RedisHost: defaultRedisHost,
MaxUser: defaultMaxUser,
ServerDeadLine: defaultServerDeadline,
RoomDeadLine: defaultRoomDeadline,
Logger: log.New(os.Stdout, "iguagile-room-api ", log.Lshortfile),
serverManager: &ServerManager{servers: &sync.Map{}},
roomManager: &RoomManager{rooms: &sync.Map{}},
}
}
// Server is room server information.
type Server struct {
Host string `json:"server"`
Port int `json:"port"`
ServerID int `json:"-"`
Load int `json:"-"`
APIPort int `json:"-"`
Token []byte `json:"-"`
updated time.Time `json:"-"`
}
// Room is room information.
type Room struct {
RoomID int `json:"room_id"`
RequirePassword bool `json:"require_password"`
MaxUser int `json:"max_user"`
ConnectedUser int `json:"connected_user"`
Server Server `json:"server"`
Token string `json:"token"`
Information map[string]string `json:"information"`
ApplicationName string `json:"-"`
Version string `json:"-"`
updated time.Time `json:"-"`
}
// RoomAPIResponse is api response.
type RoomAPIResponse struct {
Success bool `json:"success"`
Result interface{} `json:"result"`
Error string `json:"error"`
}
// CreateRoomRequest is api request.
type CreateRoomRequest struct {
ApplicationName string `json:"application_name"`
Version string `json:"version"`
Password string `json:"password"`
MaxUser int `json:"max_user"`
Information map[string]string `json:"information"`
}
const iguagileAPIVersion = "v1"
// Start starts an room api server.
func (s *RoomAPIServer) Start() error {
redisConn, err := redis.Dial("tcp", s.RedisHost)
if err != nil {
return err
}
psc := redis.PubSubConn{Conn: redisConn}
if err := psc.Subscribe(channelServer, channelRoom); err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context, psc redis.PubSubConn) {
if err := s.subscribe(ctx, psc); err != nil {
s.Logger.Println(err)
}
for {
select {
case <-ctx.Done():
return
default:
}
redisConn, err := redis.Dial("tcp", s.RedisHost)
if err != nil {
s.Logger.Println(err)
break
}
psc = redis.PubSubConn{Conn: redisConn}
if err := psc.Subscribe(channelServer, channelRoom); err != nil {
s.Logger.Println(err)
break
}
if err := s.subscribe(ctx, psc); err != nil {
s.Logger.Println(err)
}
}
}(ctx, psc)
go s.serverManager.DeleteUnhealthServerAtPeriodic(ctx, s.ServerDeadLine)
go s.roomManager.DeleteDeadRoomAtPeriodic(ctx, s.RoomDeadLine)
e := echo.New()
e.Use(middleware.Recover())
e.Use(middleware.Logger())
g := e.Group(s.BaseUri)
g.Add(echo.POST, "/rooms", s.roomCreateHandler)
g.Add(echo.GET, "/rooms", s.roomListHandler)
g.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Add("X-IGUAGILE-API", iguagileAPIVersion)
return next(c)
}
})
return e.Start(s.Address)
}