From 9939e10d4d970aae12b57734845117c9fcbd4c24 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Sun, 7 Jan 2024 14:53:41 +0530 Subject: [PATCH] Move template functions to separate file --- assets/_head.html | 2 +- assets/mixer.html | 2 +- assets/styles.css | 8 +++++ routes/mix/handler.go | 41 ----------------------- routes/mix/template_context.go | 59 ++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 43 deletions(-) create mode 100644 routes/mix/template_context.go diff --git a/assets/_head.html b/assets/_head.html index d5d0833..3ad0d6c 100644 --- a/assets/_head.html +++ b/assets/_head.html @@ -3,7 +3,7 @@ Httpbun - + diff --git a/assets/mixer.html b/assets/mixer.html index 5c2548d..5b4e33d 100644 --- a/assets/mixer.html +++ b/assets/mixer.html @@ -8,7 +8,7 @@

Logo Mixer — Note: Below URL is different from the one in the browser URL bar.

-

https://httpbun.com/mix/...
Click to copy

+

https://httpbun.com/mix/...Click to copy

diff --git a/assets/styles.css b/assets/styles.css index af8f3d9..18729c7 100644 --- a/assets/styles.css +++ b/assets/styles.css @@ -146,6 +146,14 @@ button { display: inline-block; } +#url { + white-space: nowrap; + overflow-x: hidden; + max-width: 100%; + display: block; + text-overflow: ellipsis; +} + .ghost { position: fixed; display: flex; diff --git a/routes/mix/handler.go b/routes/mix/handler.go index 7544790..db810df 100644 --- a/routes/mix/handler.go +++ b/routes/mix/handler.go @@ -3,7 +3,6 @@ package mix import ( "bytes" "encoding/base64" - "encoding/json" "github.com/sharat87/httpbun/assets" "github.com/sharat87/httpbun/exchange" "github.com/sharat87/httpbun/response" @@ -42,46 +41,6 @@ var pairValueDirectives = map[string]any{ "c": nil, } -var templateFuncMap = template.FuncMap{ - "seq": func(args ...int) []int { - var start, end, delta int - switch len(args) { - case 1: - start = 0 - end = args[0] - delta = 1 - case 2: - start = args[0] - end = args[1] - delta = 1 - case 3: - start = args[0] - end = args[1] - delta = args[2] - } - if (start > end && delta > 0) || (start < end && delta < 0) { - delta = -delta - } - var seq []int - for i := start; i != end; i += delta { - seq = append(seq, i) - } - return seq - }, - "toJSON": func(v any) string { - buffer := &bytes.Buffer{} - encoder := json.NewEncoder(buffer) - encoder.SetEscapeHTML(false) - encoder.SetIndent("", " ") - err := encoder.Encode(v) - if err != nil { - log.Printf("Error encoding JSON: %v", err) - return err.Error() - } - return string(bytes.TrimSpace(buffer.Bytes())) - }, -} - func computeMixEntries(ex *exchange.Exchange) ([]entry, error) { // We need raw path here, with percent encoding intact. path := strings.TrimPrefix(ex.RoutedRawPath, "/mix") diff --git a/routes/mix/template_context.go b/routes/mix/template_context.go new file mode 100644 index 0000000..bccf925 --- /dev/null +++ b/routes/mix/template_context.go @@ -0,0 +1,59 @@ +package mix + +import ( + "bytes" + "encoding/json" + "log" + "text/template" +) + +var templateFuncMap = template.FuncMap{ + "seq": tplFuncSeq, + "repeat": tplFuncSeq, + "toJSON": func(v any) string { + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + err := encoder.Encode(v) + if err != nil { + log.Printf("Error encoding JSON: %v", err) + return err.Error() + } + return string(bytes.TrimSpace(buffer.Bytes())) + }, +} + +func tplFuncSeq(args ...int) []map[string]any { + var start, end, delta int + switch len(args) { + case 1: + start = 0 + end = args[0] + delta = 1 + case 2: + start = args[0] + end = args[1] + delta = 1 + case 3: + start = args[0] + end = args[1] + delta = args[2] + } + if (start > end && delta > 0) || (start < end && delta < 0) { + delta = -delta + } + var seq []map[string]any + for i := start; i != end; i += delta { + seq = append(seq, map[string]any{ + "N": i, + "IsFirst": false, + "IsLast": false, + }) + } + if len(seq) > 0 { + seq[0]["IsFirst"] = true + seq[len(seq)-1]["IsLast"] = true + } + return seq +}