fmt=json (http only)

This commit is contained in:
boyska 2018-11-09 13:59:58 +01:00
parent a0b57e1a78
commit 36f531a58a
2 changed files with 74 additions and 13 deletions

View file

@ -1,7 +1,9 @@
package circolog package main
import ( import (
"bytes" "encoding/json"
"fmt"
"io"
"text/template" "text/template"
"time" "time"
@ -31,9 +33,32 @@ func init() {
)) ))
} }
// FormatSyslog format a message in the typical format used in /var/log/messages type renderFormat int
func FormatSyslog(msg format.LogParts) string {
var buf bytes.Buffer func parseRenderFormat(s string) (renderFormat, error) {
syslogTmpl.Execute(&buf, msg) switch s {
return buf.String() case "json":
return formatJSON, nil
case "syslog":
return formatSyslog, nil
default:
return 0, fmt.Errorf("Undefined format `%s`", s)
}
}
const (
formatSyslog = iota // 0
formatJSON = iota
)
func writeFormatted(w io.Writer, f renderFormat, msg format.LogParts) error {
switch f {
case formatSyslog:
return syslogTmpl.Execute(w, msg)
case formatJSON:
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(msg)
}
return nil
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -36,7 +37,7 @@ func parseParameterL(r *http.Request) (int, error) {
return requestMessageLen, nil return requestMessageLen, nil
} }
func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOptions, error) { func parseParameters(r *http.Request) (circolog.ClientOptions, error) {
var opts circolog.ClientOptions var opts circolog.ClientOptions
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
@ -52,13 +53,46 @@ func parseParameters(w http.ResponseWriter, r *http.Request) (circolog.ClientOpt
return opts, err return opts, err
} }
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
}
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
opts, err := parseParameters(w, r) 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)
if err != nil { if err != nil {
log.Println("Error on request parameter \"l\":", err) log.Println("Error on request parameter \"l\":", err)
w.WriteHeader(400) w.WriteHeader(400)
fmt.Fprintln(w, err)
return return
} }
opts.Nofollow = true opts.Nofollow = true
@ -70,10 +104,12 @@ func getHTTPHandler(hub circolog.Hub) http.HandlerFunc {
hub.Register <- client hub.Register <- client
for x := range client.Messages { for x := range client.Messages {
w.Write([]byte(circolog.FormatSyslog(x))) writeFormatted(w, render_opts.Format, x)
if render_opts.Format != formatJSON { // bleah
w.Write([]byte("\n")) w.Write([]byte("\n"))
} }
} }
}
} }
func getWSHandler(hub circolog.Hub) http.HandlerFunc { func getWSHandler(hub circolog.Hub) http.HandlerFunc {
@ -82,7 +118,7 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc {
WriteBufferSize: 1024, WriteBufferSize: 1024,
} }
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
opts, err := parseParameters(w, r) opts, err := parseParameters(r)
if err != nil { if err != nil {
log.Println("Error on request parameter \"l\":", err) log.Println("Error on request parameter \"l\":", err)
w.WriteHeader(400) w.WriteHeader(400)
@ -120,7 +156,7 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc {
if err != nil { if err != nil {
return return
} }
w.Write([]byte(circolog.FormatSyslog(message))) writeFormatted(w, formatSyslog, message)
if err := w.Close(); err != nil { if err := w.Close(); err != nil {
return return