forked from boyska/circolog
refactor ClientOptions
This commit is contained in:
parent
f2b02e5ec2
commit
fbc0f96647
2 changed files with 24 additions and 19 deletions
|
@ -12,12 +12,6 @@ import (
|
||||||
"gopkg.in/mcuadros/go-syslog.v2/format"
|
"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) {
|
func setupHTTP(hub circolog.Hub) {
|
||||||
http.HandleFunc("/", getHTTPHandler(hub))
|
http.HandleFunc("/", getHTTPHandler(hub))
|
||||||
http.HandleFunc("/ws", getWSHandler(hub))
|
http.HandleFunc("/ws", getWSHandler(hub))
|
||||||
|
@ -42,30 +36,37 @@ func parseParameterL(r *http.Request) (int, error) {
|
||||||
return requestMessageLen, nil
|
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()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error parsing http request", err)
|
log.Println("error parsing http request", err)
|
||||||
|
return opts, err
|
||||||
}
|
}
|
||||||
p.errorList = make(map[string]error, 1)
|
l, err := parseParameterL(r)
|
||||||
p.requestMessageLen, p.errorList["l"] = parseParameterL(r)
|
if err != nil {
|
||||||
|
return opts, err
|
||||||
|
}
|
||||||
|
opts.BacklogLength = l
|
||||||
|
|
||||||
|
return opts, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
|
func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Looking for known parameter in the request
|
// Looking for known parameter in the request
|
||||||
var params requestParams
|
opts, err := parseParameters(w, r)
|
||||||
params.parseParameters(w, r)
|
if err != nil {
|
||||||
if params.errorList["l"] != nil {
|
log.Println("Error on request parameter \"l\":", err)
|
||||||
log.Println("Request parameter \"l\":", params.errorList["l"])
|
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
opts.Nofollow = true
|
||||||
|
|
||||||
client := circolog.Client{
|
client := circolog.Client{
|
||||||
Messages: make(chan format.LogParts, 20),
|
Messages: make(chan format.LogParts, 20),
|
||||||
LastMsg: params.requestMessageLen,
|
Options: opts,
|
||||||
Nofollow: true}
|
}
|
||||||
hub.Register <- client
|
hub.Register <- client
|
||||||
|
|
||||||
for x := range client.Messages {
|
for x := range client.Messages {
|
||||||
|
|
12
hub.go
12
hub.go
|
@ -11,8 +11,12 @@ import (
|
||||||
// new messages are sent.
|
// new messages are sent.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Messages chan format.LogParts // only hub should write/close this
|
Messages chan format.LogParts // only hub should write/close this
|
||||||
LastMsg int // The number of last messages to display
|
Options ClientOptions
|
||||||
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
|
}
|
||||||
|
|
||||||
|
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
|
// 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) {
|
func (h *Hub) register(cl Client) {
|
||||||
if _, ok := h.clients[cl]; !ok {
|
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
|
h.clients[cl] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +64,7 @@ func (h *Hub) register(cl Client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if cl.Nofollow {
|
if cl.Options.Nofollow {
|
||||||
close(cl.Messages)
|
close(cl.Messages)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue