-
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.
- Loading branch information
1 parent
1df97ff
commit 25e5f8f
Showing
1 changed file
with
66 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,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 | ||
} |