Tidy webserver

This commit is contained in:
Torjus Håkestad 2021-04-12 08:51:50 +02:00
parent a264ce76ed
commit 7c7117d8d6
2 changed files with 9 additions and 8 deletions

View File

@ -1,4 +1,4 @@
package apiary package apiary
var Version = "v0.1.0" var Version = "v0.1.1"
var Build string var Build string

View File

@ -22,23 +22,23 @@ import (
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
) )
const streamKeepAliveDuration = 30 * time.Second
type Server struct { type Server struct {
http.Server http.Server
httpRedirectServer http.Server
cfg config.FrontendConfig cfg config.FrontendConfig
store store.LoginAttemptStore honeypotServer *honeypot.HoneypotServer
store store.LoginAttemptStore
ServerLogger *zap.SugaredLogger ServerLogger *zap.SugaredLogger
AccessLogger *zap.SugaredLogger AccessLogger *zap.SugaredLogger
honeypotServer *honeypot.HoneypotServer
attemptListenersLock sync.RWMutex attemptListenersLock sync.RWMutex
attemptListeners map[string]chan models.LoginAttempt attemptListeners map[string]chan models.LoginAttempt
streamContext context.Context
streamContext context.Context
httpRedirectServer http.Server
} }
func NewServer(cfg config.FrontendConfig, hs *honeypot.HoneypotServer, store store.LoginAttemptStore) *Server { func NewServer(cfg config.FrontendConfig, hs *honeypot.HoneypotServer, store store.LoginAttemptStore) *Server {
@ -155,7 +155,7 @@ func (s *Server) HandlerAttemptStream(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
flusher := w.(http.Flusher) flusher := w.(http.Flusher)
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(streamKeepAliveDuration)
defer ticker.Stop() defer ticker.Stop()
for { for {
@ -170,6 +170,7 @@ func (s *Server) HandlerAttemptStream(w http.ResponseWriter, r *http.Request) {
s.ServerLogger.Warnw("Error writing event", "error", err) s.ServerLogger.Warnw("Error writing event", "error", err)
} }
flusher.Flush() flusher.Flush()
ticker.Reset(streamKeepAliveDuration)
case <-s.streamContext.Done(): case <-s.streamContext.Done():
return return
case <-r.Context().Done(): case <-r.Context().Done():