Skip to content

Commit

Permalink
Move template functions to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Jan 7, 2024
1 parent d997658 commit 9939e10
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion assets/_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta charset=utf-8>
<title>Httpbun</title>
<link rel=canonical href="https://httpbun.com">

<meta name=color-scheme content="light dark">
<meta name=viewport content="width=device-width">

<link rel=apple-touch-icon sizes=180x180 href="{{.serverSpec.PathPrefix}}/icon-180.png">
Expand Down
2 changes: 1 addition & 1 deletion assets/mixer.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1><img alt=Logo src='{{.pathPrefix}}/icon-180.png'> Mixer &mdash; <a style="co

<p id=urlMismatchMessage style="display:none">Note: Below URL is different from the one in the browser URL bar.</p>

<p style="cursor:pointer;text-align:center"><a id=url>https://httpbun.com/mix/...</a><br>Click to copy</p>
<p style="cursor:pointer;text-align:center"><a id=url>https://httpbun.com/mix/...</a>Click to copy</p>

<form id=form></form>

Expand Down
8 changes: 8 additions & 0 deletions assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 0 additions & 41 deletions routes/mix/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
59 changes: 59 additions & 0 deletions routes/mix/template_context.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9939e10

Please sign in to comment.