forked from render-examples/go-gin-web-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
166 lines (138 loc) · 3.1 KB
/
routes.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 main
import (
"encoding/json"
"fmt"
"html"
"strings"
"sync"
"time"
"github.com/kataras/iris/v12"
"github.com/r3labs/sse/v2"
)
var sseServer *sse.Server
func init() {
// There are many sse implementations out there,
// this is just an example of one of them.
sseServer = sse.New()
sseServer.AutoStream = true
sseServer.AutoReplay = true
streams := make(map[string]int64)
mu := new(sync.RWMutex)
sseServer.OnSubscribe = func(streamID string, sub *sse.Subscriber) {
users.Add("connected", 1)
mu.Lock()
streams[streamID] = streams[streamID] + 1
mu.Unlock()
}
sseServer.OnUnsubscribe = func(streamID string, sub *sse.Subscriber) {
users.Add("disconnected", 1)
mu.Lock()
value := streams[streamID] - 1
if value == 0 {
delete(streams, streamID)
} else {
streams[streamID] = value
}
mu.Unlock()
}
go func() {
t := time.NewTicker(1 * time.Second)
defer t.Stop()
for range t.C {
stats := Stats()
if len(stats) == 0 {
continue
}
data, err := json.Marshal(stats)
if err != nil {
return
}
streamsCopy := make(map[string]int64)
mu.RLock()
for k, v := range streams {
streamsCopy[k] = v
}
mu.RUnlock()
for streamID := range streamsCopy {
sseServer.Publish(streamID, &sse.Event{
Event: []byte("stats"),
Data: data,
})
}
}
}()
}
func rateLimit(ctx iris.Context) {
ip := ctx.RemoteAddr()
value := int(ips.Add(ip, 1))
if value%50 == 0 {
fmt.Printf("ip: %s, count: %d\n", ip, value)
}
if value >= 200 {
if value%200 == 0 {
fmt.Println("ip blocked")
}
ctx.StopWithText(iris.StatusServiceUnavailable, "you were automatically banned :)")
return
}
ctx.Next()
}
func index(ctx iris.Context) {
ctx.Redirect("/room/hn", iris.StatusMovedPermanently)
}
func roomGET(ctx iris.Context) {
roomid := ctx.Params().Get("roomid")
nick := ctx.URLParam("nick")
if len(nick) < 2 {
nick = ""
}
if len(nick) > 13 {
nick = nick[0:12] + "..."
}
err := ctx.View("room_login.templ.html", iris.Map{
"roomid": roomid,
"nick": nick,
"timestamp": time.Now().Unix(),
})
if err != nil {
ctx.HTML("<strong>%s</strong>", err.Error())
return
}
}
func roomPOST(ctx iris.Context) {
roomid := ctx.Params().Get("roomid")
nick := ctx.URLParam("nick")
message := ctx.PostValue("message")
message = strings.TrimSpace(message)
validMessage := len(message) > 1 && len(message) < 200
validNick := len(nick) > 1 && len(nick) < 14
if !validMessage || !validNick {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{
"status": "failed",
"error": "the message or nickname is too long",
})
return
}
post := iris.Map{
"nick": html.EscapeString(nick),
"message": html.EscapeString(message),
}
messages.Add("inbound", 1)
data, err := json.Marshal(post)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{
"status": "failed",
"error": err.Error(),
})
return
}
sseServer.Publish(roomid, &sse.Event{
Event: []byte("message"),
Data: data,
})
// room(roomid).Submit(post)
ctx.JSON(post)
}
func listenEvents() iris.Handler {
return iris.FromStd(sseServer.ServeHTTP)
}