2018-08-23 00:34:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-08 12:37:31 +01:00
|
|
|
"errors"
|
2018-11-09 13:59:58 +01:00
|
|
|
"fmt"
|
2018-11-05 11:53:26 +01:00
|
|
|
"log"
|
2018-08-23 00:34:02 +02:00
|
|
|
"net/http"
|
2018-11-04 23:13:27 +01:00
|
|
|
"strconv"
|
2018-08-23 00:34:02 +02:00
|
|
|
"time"
|
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
"git.lattuga.net/boyska/circolog"
|
2018-08-23 00:34:02 +02:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
|
|
|
)
|
|
|
|
|
2018-08-23 12:25:07 +02:00
|
|
|
func setupHTTP(hub circolog.Hub) {
|
2018-08-23 00:56:27 +02:00
|
|
|
http.HandleFunc("/", getHTTPHandler(hub))
|
2018-08-23 00:34:02 +02:00
|
|
|
http.HandleFunc("/ws", getWSHandler(hub))
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:37:31 +01:00
|
|
|
func parseParameterL(r *http.Request) (int, error) {
|
2018-11-09 11:21:50 +01:00
|
|
|
var requestMessageLen int = -1
|
2018-11-08 12:37:31 +01:00
|
|
|
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-09 13:59:58 +01:00
|
|
|
func parseParameters(r *http.Request) (circolog.ClientOptions, error) {
|
2018-11-08 19:25:40 +01:00
|
|
|
var opts circolog.ClientOptions
|
2018-11-08 12:37:31 +01:00
|
|
|
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 12:37:31 +01:00
|
|
|
}
|
2018-11-08 19:25:40 +01:00
|
|
|
opts.BacklogLength = l
|
|
|
|
|
|
|
|
return opts, err
|
2018-11-08 12:37:31 +01:00
|
|
|
}
|
|
|
|
|
2018-11-09 13:59:58 +01:00
|
|
|
type renderOptions struct { // those are options relevant to the rendered (that is, the HTTP side of circologd)
|
|
|
|
Format renderFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRenderParameters(r *http.Request) (renderOptions, error) {
|
|
|
|
opts := renderOptions{}
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error parsing http request", err)
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
if val, ok := r.Form["fmt"]; ok {
|
|
|
|
if len(val) != 1 {
|
|
|
|
return opts, errors.New("Format repeated multiple times")
|
|
|
|
}
|
|
|
|
format, err := parseRenderFormat(val[0])
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
opts.Format = format
|
|
|
|
}
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
|
2018-08-23 00:34:02 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-11-08 12:37:31 +01:00
|
|
|
// Looking for known parameter in the request
|
2018-11-09 13:59:58 +01:00
|
|
|
render_opts, err := parseRenderParameters(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error parsing:", err)
|
|
|
|
w.WriteHeader(400)
|
|
|
|
fmt.Fprintln(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := parseParameters(r)
|
2018-11-08 19:25:40 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error on request parameter \"l\":", err)
|
2018-11-08 12:37:31 +01:00
|
|
|
w.WriteHeader(400)
|
2018-11-09 13:59:58 +01:00
|
|
|
fmt.Fprintln(w, err)
|
2018-11-08 12:37:31 +01:00
|
|
|
return
|
|
|
|
}
|
2018-11-08 19:25:40 +01:00
|
|
|
opts.Nofollow = true
|
2018-11-08 12:37:31 +01:00
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
client := circolog.Client{
|
2018-08-23 02:05:50 +02:00
|
|
|
Messages: make(chan format.LogParts, 20),
|
2018-11-08 19:25:40 +01:00
|
|
|
Options: opts,
|
|
|
|
}
|
2018-08-23 00:56:27 +02:00
|
|
|
hub.Register <- client
|
|
|
|
|
2018-11-08 12:37:31 +01:00
|
|
|
for x := range client.Messages {
|
2018-11-09 13:59:58 +01:00
|
|
|
writeFormatted(w, render_opts.Format, x)
|
|
|
|
if render_opts.Format != formatJSON { // bleah
|
|
|
|
w.Write([]byte("\n"))
|
|
|
|
}
|
2018-08-23 00:56:27 +02:00
|
|
|
}
|
2018-08-23 00:34:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
func getWSHandler(hub circolog.Hub) http.HandlerFunc {
|
2018-08-23 00:34:02 +02:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-11-10 17:20:12 +01:00
|
|
|
render_opts, err := parseRenderParameters(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error parsing:", err)
|
|
|
|
w.WriteHeader(400)
|
|
|
|
fmt.Fprintln(w, err)
|
|
|
|
return
|
|
|
|
}
|
2018-11-09 13:59:58 +01:00
|
|
|
opts, err := parseParameters(r)
|
2018-11-09 11:21:50 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error on request parameter \"l\":", err)
|
|
|
|
w.WriteHeader(400)
|
2018-11-10 17:20:12 +01:00
|
|
|
fmt.Fprintln(w, err)
|
2018-11-09 11:21:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opts.Nofollow = false
|
2018-08-23 00:34:02 +02:00
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-11-09 11:21:50 +01:00
|
|
|
client := circolog.Client{
|
|
|
|
Messages: make(chan format.LogParts, 20),
|
|
|
|
Options: opts,
|
2018-11-08 19:37:03 +01:00
|
|
|
}
|
2018-08-23 00:34:02 +02:00
|
|
|
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) {
|
2018-08-23 00:34:02 +02:00
|
|
|
defer func() {
|
|
|
|
hub.Unregister <- c
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
2018-08-23 00:56:27 +02:00
|
|
|
case message, ok := <-c.Messages:
|
2018-08-23 00:34:02 +02:00
|
|
|
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-11-10 17:20:12 +01:00
|
|
|
writeFormatted(w, render_opts.Format, message)
|
2018-08-23 00:34:02 +02:00
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: ticker/ping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(conn, client)
|
|
|
|
}
|
|
|
|
}
|