Skip to content

Commit

Permalink
feat: handler for HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed Jun 20, 2024
1 parent 1df97ff commit 25e5f8f
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package manifesto

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

const (
headerContentType = "Content-Type"
headerUserAgent = "User-Agent"

mimeJson = "application/json"
mimeText = "plain/text"
)

// HandleManifest adds a POST endpoint for the given path and spec/status types.
func HandleManifest(path string, pool *Pool, spec any, status any) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("this endpoint only allows POST requests"))
return
}

manifest := ParseReader(r.Body, spec, status)
pool.Apply(*manifest)

b, err := json.Marshal(manifest)
if err != nil {
w.Header().Add(headerContentType, mimeText)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
} else {
w.Header().Add(headerContentType, mimeJson)
w.WriteHeader(http.StatusOK)
w.Write(b)
return
}
})
}

// SendManifest sends a manifest to a given endpoint (blocking) and returns the new manifest.
func SendManifestSilent(endpoint string, manifest *Manifest, userAgent string) (*Manifest, error) {
b, err := json.Marshal(manifest)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, endpoint+"/"+manifest.ApiVersion+"/"+manifest.Kind, bytes.NewBuffer(b))
if err != nil {
return nil, err
}

req.Header.Set(headerContentType, mimeJson)
req.Header.Set(headerUserAgent, userAgent)

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()
return ParseReader(res.Body, manifest.Spec, manifest.Status), nil
}

0 comments on commit 25e5f8f

Please sign in to comment.