refactor ClientOptions

This commit is contained in:
boyska 2018-11-08 19:25:40 +01:00
parent f2b02e5ec2
commit fbc0f96647
2 changed files with 24 additions and 19 deletions

View file

@ -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 {

12
hub.go
View file

@ -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)
}
}