1
0
Fork 0
forked from boyska/circolog
circolog/cmd/circologd/http.go

127 lines
2.8 KiB
Go
Raw Normal View History

package main
import (
"errors"
2018-11-05 11:53:26 +01:00
"log"
"net/http"
"strconv"
"time"
2018-08-23 01:21:53 +02:00
"git.lattuga.net/boyska/circolog"
"github.com/gorilla/websocket"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
2018-08-23 12:25:07 +02:00
func setupHTTP(hub circolog.Hub) {
http.HandleFunc("/", getHTTPHandler(hub))
http.HandleFunc("/ws", getWSHandler(hub))
}
func parseParameterL(r *http.Request) (int, error) {
var requestMessageLen int
var err error
if reqL, ok := r.Form["l"]; ok {
if len(reqL) == 1 {
requestMessageLen, err = strconv.Atoi(reqL[0])
if err != nil {
return 0, err
}
if requestMessageLen <= 0 {
return 0, errors.New("malformed request")
}
} else {
return 0, errors.New("malformed request")
}
}
return requestMessageLen, nil
}
2018-11-08 19:25:40 +01:00
func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) {
var opts circolog.ClientOptions
2018-11-08 19:37:03 +01:00
opts.BacklogLength = 10 // default
err := r.ParseForm()
if err != nil {
log.Println("error parsing http request", err)
2018-11-08 19:25:40 +01:00
return opts, err
}
l, err := parseParameterL(r)
if err != nil {
return opts, err
}
2018-11-08 19:25:40 +01:00
opts.BacklogLength = l
return opts, err
}
2018-08-23 01:21:53 +02:00
func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Looking for known parameter in the request
2018-11-08 19:25:40 +01:00
opts, err := parseParameters(w, r)
if err != nil {
log.Println("Error on request parameter \"l\":", err)
w.WriteHeader(400)
return
}
2018-11-08 19:25:40 +01:00
opts.Nofollow = true
2018-08-23 01:21:53 +02:00
client := circolog.Client{
Messages: make(chan format.LogParts, 20),
2018-11-08 19:25:40 +01:00
Options: opts,
}
hub.Register <- client
for x := range client.Messages {
w.Write([]byte(circolog.FormatSyslog(x)))
w.Write([]byte("\n"))
}
}
}
2018-08-23 01:21:53 +02:00
func getWSHandler(hub circolog.Hub) http.HandlerFunc {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
2018-11-08 19:37:03 +01:00
client := circolog.Client{Messages: make(chan format.LogParts, 20),
Options: circolog.ClientOptions{BacklogLength: -1},
}
hub.Register <- client
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
2018-08-23 01:21:53 +02:00
go func(conn *websocket.Conn, c circolog.Client) {
defer func() {
hub.Unregister <- c
conn.Close()
}()
for {
select {
case message, ok := <-c.Messages:
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if !ok {
// The hub closed the channel.
conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
2018-08-23 15:35:55 +02:00
w.Write([]byte(circolog.FormatSyslog(message)))
if err := w.Close(); err != nil {
return
}
// TODO: ticker/ping
}
}
}(conn, client)
}
}