Skip to content

Commit

Permalink
feat: make webhook httpapi reusable
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Rüger <[email protected]>
  • Loading branch information
mrueg committed Nov 24, 2023
1 parent 70eb2b7 commit 43e9e51
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions provider/webhook/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (
)

type WebhookServer struct {
provider provider.Provider
Provider provider.Provider
}

func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
records, err := p.provider.Records(context.Background())
records, err := p.Provider.Records(context.Background())
if err != nil {
log.Errorf("Failed to get Records: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -56,7 +56,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
w.WriteHeader(http.StatusBadRequest)
return
}
err := p.provider.ApplyChanges(context.Background(), &changes)
err := p.Provider.ApplyChanges(context.Background(), &changes)
if err != nil {
log.Errorf("Failed to Apply Changes: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -70,7 +70,7 @@ func (p *WebhookServer) recordsHandler(w http.ResponseWriter, req *http.Request)
}
}

func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
log.Errorf("Unsupported method %s", req.Method)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -84,7 +84,7 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
return
}
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
pve, err := p.provider.AdjustEndpoints(pve)
pve, err := p.Provider.AdjustEndpoints(pve)
if err != nil {
log.Errorf("Failed to call adjust endpoints: %v", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -96,9 +96,9 @@ func (p *WebhookServer) adjustEndpointsHandler(w http.ResponseWriter, req *http.
}
}

func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set(contentTypeHeader, mediaTypeFormatAndVersion)
json.NewEncoder(w).Encode(p.provider.GetDomainFilter())
json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
}

// StartHTTPApi starts a HTTP server given any provider.
Expand All @@ -111,13 +111,13 @@ func (p *WebhookServer) negotiateHandler(w http.ResponseWriter, req *http.Reques
// - /adjustendpoints (POST): executes the AdjustEndpoints method
func StartHTTPApi(provider provider.Provider, startedChan chan struct{}, readTimeout, writeTimeout time.Duration, providerPort string) {
p := WebhookServer{
provider: provider,
Provider: provider,
}

m := http.NewServeMux()
m.HandleFunc("/", p.negotiateHandler)
m.HandleFunc("/records", p.recordsHandler)
m.HandleFunc("/adjustendpoints", p.adjustEndpointsHandler)
m.HandleFunc("/", p.NegotiateHandler)
m.HandleFunc("/records", p.RecordsHandler)
m.HandleFunc("/adjustendpoints", p.AdjustEndpointsHandler)

s := &http.Server{
Addr: providerPort,
Expand Down

0 comments on commit 43e9e51

Please sign in to comment.