Commit a6ce41b9 authored by Sietse Ringers's avatar Sietse Ringers

refactor: unexport Conf and ServerSentEvents fields in servercore.Server

parent 1ed30466
......@@ -7,7 +7,6 @@ package servercore
import (
"encoding/hex"
"encoding/json"
"log"
"net/http"
"regexp"
"strconv"
......@@ -23,15 +22,15 @@ import (
)
type Server struct {
ServerSentEvents *sse.Server
conf *server.Configuration
sessions sessionStore
scheduler *gocron.Scheduler
stopScheduler chan bool
handlers map[string]server.SessionHandler
serverSentEvents *sse.Server
}
func New(conf *server.Configuration) (*Server, error) {
func New(conf *server.Configuration, eventServer *sse.Server) (*Server, error) {
if err := conf.Check(); err != nil {
return nil, err
}
......@@ -44,33 +43,8 @@ func New(conf *server.Configuration) (*Server, error) {
client: make(map[string]*session),
conf: conf,
},
handlers: make(map[string]server.SessionHandler),
}
if conf.EnableSSE {
s.ServerSentEvents = sse.NewServer(&sse.Options{
ChannelNameFunc: func(r *http.Request) string {
component, token, noun, args, err := Route(r.URL.Path, r.Method)
if err != nil {
_ = server.LogWarning(err)
return ""
}
if component == server.ComponentSession && noun == "statusevents" {
return "session/" + token
}
if component == server.ComponentRevocation && noun == "updateevents" {
return "revocation/" + args[0]
}
return ""
},
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
},
Logger: log.New(conf.Logger.WithField("type", "sse").WriterLevel(logrus.DebugLevel), "", 0),
})
s.Conf.IrmaConfiguration.Revocation.ServerSentEvents = s.ServerSentEvents
handlers: make(map[string]server.SessionHandler),
serverSentEvents: eventServer,
}
s.scheduler.Every(10).Seconds().Do(func() {
......@@ -228,9 +202,9 @@ func (s *Server) SubscribeServerSentEvents(w http.ResponseWriter, r *http.Reques
// event to just the webclient currently listening. (Thus the handler of this "open" event must be idempotent.)
go func() {
time.Sleep(200 * time.Millisecond)
s.ServerSentEvents.SendMessage("session/"+token, sse.NewMessage("", "", "open"))
s.serverSentEvents.SendMessage("session/"+token, sse.NewMessage("", "", "open"))
}()
s.ServerSentEvents.ServeHTTP(w, r)
s.serverSentEvents.ServeHTTP(w, r)
return nil
}
......
......@@ -162,7 +162,7 @@ func (s *Server) newSession(action irma.Action, request irma.RequestorRequest) *
prevStatus: server.StatusInitialized,
conf: s.conf,
sessions: s.sessions,
sse: s.ServerSentEvents,
sse: s.serverSentEvents,
result: &server.SessionResult{
LegacySession: request.SessionRequest().Base().Legacy(),
Token: token,
......
......@@ -6,18 +6,23 @@ package irmaserver
import (
"io/ioutil"
"log"
"net/http"
"time"
"github.com/alexandrevicenzi/go-sse"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/servercore"
"github.com/privacybydesign/irmago/server"
"github.com/sirupsen/logrus"
)
// Server is an irmaserver instance.
type Server struct {
*servercore.Server
conf *server.Configuration
serverSentEvents *sse.Server
}
// Default server instance
......@@ -31,12 +36,21 @@ func Initialize(conf *server.Configuration) (err error) {
// New creates a new Server instance with the specified configuration.
func New(conf *server.Configuration) (*Server, error) {
s, err := servercore.New(conf)
var e *sse.Server
if conf.EnableSSE {
e = eventServer(conf)
}
s, err := servercore.New(conf, e)
if err != nil {
return nil, err
}
conf.IrmaConfiguration.Revocation.ServerSentEvents = e
return &Server{
Server: s,
Server: s,
conf: conf,
serverSentEvents: e,
}, nil
}
......@@ -116,10 +130,10 @@ func (s *Server) HandlerFunc() http.HandlerFunc {
}
if err == nil && component == server.ComponentRevocation && noun == "updateevents" {
id := irma.NewCredentialTypeIdentifier(args[0])
if settings := s.Conf.RevocationSettings[id]; settings != nil &&
if settings := s.conf.RevocationSettings[id]; settings != nil &&
settings.ServerMode &&
s.ServerSentEvents != nil {
s.ServerSentEvents.ServeHTTP(w, r)
s.serverSentEvents != nil {
s.serverSentEvents.ServeHTTP(w, r)
} else {
server.WriteError(w, server.ErrorUnsupported, "")
}
......@@ -139,3 +153,28 @@ func (s *Server) HandlerFunc() http.HandlerFunc {
}
}
}
func eventServer(conf *server.Configuration) *sse.Server {
return sse.NewServer(&sse.Options{
ChannelNameFunc: func(r *http.Request) string {
component, token, noun, args, err := servercore.Route(r.URL.Path, r.Method)
if err != nil {
conf.Logger.Warn(err)
return ""
}
if component == server.ComponentSession && noun == "statusevents" {
return "session/" + token
}
if component == server.ComponentRevocation && noun == "updateevents" {
return "revocation/" + args[0]
}
return ""
},
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
},
Logger: log.New(conf.Logger.WithField("type", "sse").WriterLevel(logrus.DebugLevel), "", 0),
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment