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
var Version = "v0.1.0"
var Version = "v0.1.1"
var Build string

View File

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