Skip to content

Commit

Permalink
pkg/httpjson: add minify_json template helper (#74)
Browse files Browse the repository at this point in the history
This helper allows nicely formatted JSON to be sent the the client as
single-line JSON text.

rules:
- path: "/static/minify"
  methods: ["GET"]

  responses:
  - status_code: 200
    body: |-
      {{ minify_json `
      {
      	"key1": "value1",
      	"key2": "<value2>"
      }
      `}}

will respond to requests to /static/minify with {"key1":"value1","key2":"<value2>"}.
  • Loading branch information
efd6 authored Jan 23, 2024
1 parent 6aa80ba commit 6344366
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Added `minify_json` template helper function for minifying static JSON: [#x](https://github.com/elastic/stream/pull/x)

### Changed

### Fixed
Expand Down
19 changes: 15 additions & 4 deletions pkg/httpserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package httpserver

import (
"encoding/json"
"errors"
"os"
"strings"
"text/template"

ucfg "github.com/elastic/go-ucfg"
Expand Down Expand Up @@ -45,10 +47,11 @@ func (t *tpl) Unpack(in string) error {
parsed, err := template.New("").
Option("missingkey=zero").
Funcs(template.FuncMap{
"env": env,
"hostname": hostname,
"sum": sum,
"file": file,
"env": env,
"hostname": hostname,
"sum": sum,
"file": file,
"minify_json": minify,
}).
Parse(in)
if err != nil {
Expand Down Expand Up @@ -98,3 +101,11 @@ func file(path string) (string, error) {
}
return string(b), nil
}

func minify(body string) (string, error) {
var buf strings.Builder
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(json.RawMessage(body))
return strings.TrimSpace(buf.String()), err
}
28 changes: 28 additions & 0 deletions pkg/httpserver/httpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestHTTPServer(t *testing.T) {
status_code: 200
body: |-
{"next": "http://{{ hostname }}/page/{{ sum (.req_num) 1 }}"}
- path: "/page/{pagenum:[0-9]}"
methods: ["POST"]
Expand All @@ -52,6 +53,19 @@ func TestHTTPServer(t *testing.T) {
body: "{{ .request.vars.pagenum }}"
headers:
content-type: ["text/plain"]
- path: "/static/minify"
methods: ["GET"]
responses:
- status_code: 200
body: |-
{{ minify_json ` + "`" + `
{
"key1": "value1",
"key2": "<value2>"
}
` + "`" + `}}
`

f, err := ioutil.TempFile("", "test")
Expand Down Expand Up @@ -136,6 +150,20 @@ func TestHTTPServer(t *testing.T) {

assert.Equal(t, []byte{}, body)
})

t.Run("minify static JSON", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://"+addr+"/static/minify", nil)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()

assert.Equal(t, `{"key1":"value1","key2":"<value2>"}`, string(body))
})
}

func TestRunAsSequence(t *testing.T) {
Expand Down

0 comments on commit 6344366

Please sign in to comment.