Skip to content

Commit

Permalink
Add entryList for build response for client
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed Jul 14, 2024
1 parent 47bb745 commit c7a836a
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
84 changes: 84 additions & 0 deletions entrylist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package baker

import (
"encoding/json"
"net/http"
)

type entryList struct {
cahced []byte
collection []struct {
Domain string `json:"domain"`
Path string `json:"path"`
Rules []struct {
Type string `json:"type"`
Args any `json:"args"`
} `json:"rules"`
}
}

func (e *entryList) New(domain, path string, ready bool) *entryList {
if !ready {
return e
}

e.collection = append(e.collection, struct {
Domain string `json:"domain"`
Path string `json:"path"`
Rules []struct {
Type string `json:"type"`
Args any `json:"args"`
} `json:"rules"`
}{
Domain: domain,
Path: path,
Rules: []struct {
Type string `json:"type"`
Args any `json:"args"`
}{},
})

return e
}

func (e *entryList) WithRules(rules ...struct {
Type string `json:"type"`
Args any `json:"args"`
}) *entryList {
if len(e.collection) == 0 {
return e
}

e.collection[len(e.collection)-1].Rules = rules

return e
}

func (e *entryList) getPayload() any {
return struct {
Endpoints any `json:"endpoints"`
}{
Endpoints: e.collection,
}
}

// CacheResponse caches the response and this can be used to optimize the response
// If you call this method, the next call should be WriteResponse
func (e *entryList) CacheResponse() *entryList {
e.cahced, _ = json.Marshal(e.getPayload())
return e
}

func (e *entryList) WriteResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if len(e.cahced) > 0 {
w.Write(e.cahced)
return
}
json.NewEncoder(w).Encode(e.getPayload())
}

func NewEntryList() *entryList {
return &entryList{}
}
58 changes: 58 additions & 0 deletions entrylist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package baker_test

import (
"fmt"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"

"ella.to/baker"
"ella.to/baker/rule"
)

func TestBakerEndpoints(t *testing.T) {
{

rr := httptest.NewRecorder()
baker.NewEntryList().
New("example.com", "/", true).
WriteResponse(rr)
assert.JSONEq(t, `{"endpoints":[{"domain":"example.com","path":"/","rules":[]}]}`, strings.TrimSpace(rr.Body.String()))
}

{
rr := httptest.NewRecorder()
baker.NewEntryList().
New("example.com", "/", true).
WithRules(rule.NewAppendPath("a", "b")).
WriteResponse(rr)
assert.JSONEq(t, `{"endpoints":[{"domain":"example.com","path":"/","rules":[{"args":{"begin":"a","end":"b"},"type":"AppendPath"}]}]}`, strings.TrimSpace(rr.Body.String()))
}

{
rr := httptest.NewRecorder()
baker.NewEntryList().
New("example.com", "/", true).
WithRules(
rule.NewAppendPath("a", "b"),
rule.NewReplacePath("/a", "/b", 1),
).
WriteResponse(rr)
assert.JSONEq(t, `{"endpoints":[{"domain":"example.com","path":"/","rules":[{"args":{"begin":"a","end":"b"},"type":"AppendPath"},{"args":{"search":"/a","replace":"/b","times":1},"type":"ReplacePath"}]}]}`, strings.TrimSpace(rr.Body.String()))
}

{
rr := httptest.NewRecorder()
baker.NewEntryList().
New("example.com", "/", true).
WithRules(
rule.NewRateLimiter(1, 1*time.Second),
).
WriteResponse(rr)
fmt.Println(strings.TrimSpace(rr.Body.String()))
assert.JSONEq(t, `{"endpoints":[{"domain":"example.com","path":"/","rules":[{"type":"RateLimiter","args":{"request_limit":1,"window_duration":"1s"}}]}]}`, strings.TrimSpace(rr.Body.String()))
}
}

0 comments on commit c7a836a

Please sign in to comment.