From fbc0f96647c0482df14246c1a22fa1aa9cac25e3 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 8 Nov 2018 19:25:40 +0100 Subject: [PATCH] refactor ClientOptions --- cmd/circologd/http.go | 31 ++++++++++++++++--------------- hub.go | 12 ++++++++---- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/cmd/circologd/http.go b/cmd/circologd/http.go index 718d4a2..f9d33f1 100644 --- a/cmd/circologd/http.go +++ b/cmd/circologd/http.go @@ -12,12 +12,6 @@ import ( "gopkg.in/mcuadros/go-syslog.v2/format" ) -type requestParams struct { - // Add here the expected request parameters - errorList map[string]error - requestMessageLen int -} - func setupHTTP(hub circolog.Hub) { http.HandleFunc("/", getHTTPHandler(hub)) http.HandleFunc("/ws", getWSHandler(hub)) @@ -42,30 +36,37 @@ func parseParameterL(r *http.Request) (int, error) { return requestMessageLen, nil } -func (p *requestParams) parseParameters(w http.ResponseWriter, r *http.Request) { +func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) { + var opts circolog.ClientOptions err := r.ParseForm() if err != nil { log.Println("error parsing http request", err) + return opts, err } - p.errorList = make(map[string]error, 1) - p.requestMessageLen, p.errorList["l"] = parseParameterL(r) + l, err := parseParameterL(r) + if err != nil { + return opts, err + } + opts.BacklogLength = l + + return opts, err } func getHTTPHandler(hub circolog.Hub) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Looking for known parameter in the request - var params requestParams - params.parseParameters(w, r) - if params.errorList["l"] != nil { - log.Println("Request parameter \"l\":", params.errorList["l"]) + opts, err := parseParameters(w, r) + if err != nil { + log.Println("Error on request parameter \"l\":", err) w.WriteHeader(400) return } + opts.Nofollow = true client := circolog.Client{ Messages: make(chan format.LogParts, 20), - LastMsg: params.requestMessageLen, - Nofollow: true} + Options: opts, + } hub.Register <- client for x := range client.Messages { diff --git a/hub.go b/hub.go index dfa0199..8af32c1 100644 --- a/hub.go +++ b/hub.go @@ -11,8 +11,12 @@ import ( // new messages are sent. type Client struct { Messages chan format.LogParts // only hub should write/close this - LastMsg int // The number of last messages to display - Nofollow bool // if Nofollow is true, the hub will not keep this client permanently. Rather, it will send every message to "Messages" and close the channel. Use this if you want to get the messages one-shot + Options ClientOptions +} + +type ClientOptions struct { + BacklogLength int // how many past messages the client wants to receive upon connection + Nofollow bool // if Nofollow is true, the hub will not keep this client permanently. Rather, it will send every message to "Messages" and close the channel. Use this if you want to get the messages one-shot } // The Hub is the central "registry"; it keeps both the data storage and clients notifications @@ -41,7 +45,7 @@ func NewHub(ringBufSize int) Hub { func (h *Hub) register(cl Client) { if _, ok := h.clients[cl]; !ok { - if !cl.Nofollow { // we won't need it in future + if !cl.Options.Nofollow { // we won't need it in future h.clients[cl] = true } @@ -60,7 +64,7 @@ func (h *Hub) register(cl Client) { } } }) - if cl.Nofollow { + if cl.Options.Nofollow { close(cl.Messages) } }