Skip to content

Commit

Permalink
add lookup by id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej committed Sep 6, 2024
1 parent 4ff5f50 commit 1f5f8c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
11 changes: 10 additions & 1 deletion flix/v1_1/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ func (i *TemplateIndexer) List() []Template {
return i.store
}

func (i *TemplateIndexer) LookupBySource(cadenceSource []byte) (*Template, error) {
func (i *TemplateIndexer) GetByID(id string) *Template {
for _, template := range i.store {
if template.ID() == id {
return &template
}
}
return nil
}

func (i *TemplateIndexer) GetBySource(cadenceSource []byte) (*Template, error) {
for _, template := range i.store {
isMatch, err := template.MatchesSource(cadenceSource)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions flix/v1_1/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func NewFromJson(rawJson []byte) (Template, error) {
}, nil
}

func (t Template) ID() string {
return t.parsed.Id
}

func (t Template) MarshalJSON() ([]byte, error) {
return t.raw, nil
}
Expand Down
22 changes: 21 additions & 1 deletion server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *HttpServer) Setup() {
w.WriteHeader(http.StatusBadRequest)
}

match, err := s.Indexer.LookupBySource(cadenceSource)
match, err := s.Indexer.GetBySource(cadenceSource)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
Expand All @@ -57,6 +57,26 @@ func (s *HttpServer) Setup() {
s.Logger.Printf("error writing response: %e", err)
}
})

http.HandleFunc("/v1.1/templates/{id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")

response := s.Indexer.GetByID(r.PathValue("id"))
if response == nil {
w.WriteHeader(http.StatusNotFound)
return
}

jsonResponse, err := json.Marshal(response)
if err != nil {
s.Logger.Printf("error serializing response: %e", err)
}

_, err = w.Write(jsonResponse)
if err != nil {
s.Logger.Printf("error writing response: %e", err)
}
})
}

func (s *HttpServer) Start() error {
Expand Down

0 comments on commit 1f5f8c3

Please sign in to comment.