Skip to content

Commit

Permalink
feat: support for using Flag-Filter request headers to filter the f…
Browse files Browse the repository at this point in the history
…lags that the client expects to receive (#64)
  • Loading branch information
whwlsfb authored and Li4n0 committed Mar 24, 2023
1 parent 1572171 commit b33de11
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package server

import (
"regexp"
"sync"

"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin"
"github.com/li4n0/revsuit/internal/database"
"github.com/li4n0/revsuit/internal/file"
Expand Down Expand Up @@ -44,6 +46,8 @@ func (revsuit *Revsuit) addClient(c *gin.Context) int {

revsuit.clientID++
revsuit.clients[revsuit.clientID] = c
sse.Event{}.WriteContentType(c.Writer)
c.Writer.Flush()
revsuit.clientsNum <- struct{}{}
return revsuit.clientID
}
Expand Down Expand Up @@ -231,8 +235,23 @@ func (revsuit *Revsuit) Run() {
<-revsuit.clientsNum
revsuit.clientsLock.RLock()
for _, client := range revsuit.clients {
client.SSEvent("message", r.GetFlag())
client.Writer.Flush()
pushIt := false
flag := client.Request.Header.Get("Flag-Filter")
if len(flag) == 0 || flag == "*" {
pushIt = true
} else {
if catcher, err := regexp.Compile(flag); err != nil {
log.Warn("%s[sse flag:%s]", err, flag)
continue
} else {
matched := catcher.FindStringSubmatch(r.GetFlag())
pushIt = len(matched) > 0
}
}
if pushIt {
client.SSEvent("message", r.GetFlag())
client.Writer.Flush()
}
}
revsuit.clientsNum <- struct{}{}
revsuit.clientsLock.RUnlock()
Expand Down

0 comments on commit b33de11

Please sign in to comment.