|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json" |
5 | 6 | "errors" |
6 | 7 | "net/http" |
@@ -52,3 +53,49 @@ func (s Server) GetAPIHealthcheck(w http.ResponseWriter, r *http.Request) { |
52 | 53 | return |
53 | 54 | } |
54 | 55 | } |
| 56 | + |
| 57 | +// (GET /functions/base64Encode). |
| 58 | +func (s Server) GetFunctionsBase64Encode(w http.ResponseWriter, _ *http.Request) { |
| 59 | + d := Documentation{ |
| 60 | + Name: "base64Encode", |
| 61 | + Description: "Encode anything to base64", |
| 62 | + Input: struct { |
| 63 | + Description string `json:"description"` |
| 64 | + Example string `json:"example"` |
| 65 | + Type string `json:"type"` |
| 66 | + }{ |
| 67 | + Description: "Input the data you'd like to encode to base64", |
| 68 | + Example: "Hello, world", |
| 69 | + Type: "string", |
| 70 | + }, |
| 71 | + Output: struct { |
| 72 | + Description string `json:"description"` |
| 73 | + Example string `json:"example"` |
| 74 | + Type string `json:"type"` |
| 75 | + }{ |
| 76 | + Description: "Base64 encoded string", |
| 77 | + Example: "SGVsbG8sIHdvcmxk", |
| 78 | + Type: "string", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + SetHeaderAndWriteResponse(w, http.StatusOK, d) |
| 83 | +} |
| 84 | + |
| 85 | +// (POST /functions/base64Encode). |
| 86 | +func (s Server) PostFunctionsBase64Encode(w http.ResponseWriter, r *http.Request) { |
| 87 | + var err error |
| 88 | + var body PostFunctionsBase64EncodeJSONBody |
| 89 | + if err = json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 90 | + s.Logger.Error(ErrUnmarshalBody, zap.Error(err)) |
| 91 | + sendError(w, http.StatusBadRequest, ErrUnmarshalBody.Error()) |
| 92 | + return |
| 93 | + } |
| 94 | + encoded := base64.StdEncoding.EncodeToString([]byte(body.Input)) |
| 95 | + resp := struct { |
| 96 | + Output string `json:"output"` |
| 97 | + }{ |
| 98 | + Output: encoded, |
| 99 | + } |
| 100 | + SetHeaderAndWriteResponse(w, http.StatusOK, resp) |
| 101 | +} |
0 commit comments