Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/httpjson: add minify_json template helper #74

Merged
merged 2 commits into from
Jan 23, 2024
Merged

Conversation

efd6
Copy link
Contributor

@efd6 efd6 commented Jan 17, 2024

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"}.

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"}.
@efd6 efd6 requested a review from a team January 17, 2024 02:03
@@ -98,3 +100,8 @@ func file(path string) (string, error) {
}
return string(b), nil
}

func minify(body string) (string, error) {
b, err := json.Marshal(json.RawMessage(body))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should turn off EscapeHTML as to not have this modifying content outside of ordering and spacing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pity jsonv2 is not here yet.

@efd6 efd6 requested a review from andrewkroh January 22, 2024 21:15
@elasticmachine
Copy link

💚 Build Succeeded

History

@efd6 efd6 merged commit 6344366 into elastic:main Jan 23, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants