-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add entryList for build response for client
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |