Skip to content

Commit 6d20000

Browse files
jnilesaths008
authored andcommitted
Update shared
1 parent ed462ba commit 6d20000

File tree

4 files changed

+228
-112
lines changed

4 files changed

+228
-112
lines changed

api/impl.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"errors"
67
"net/http"
@@ -52,3 +53,49 @@ func (s Server) GetAPIHealthcheck(w http.ResponseWriter, r *http.Request) {
5253
return
5354
}
5455
}
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+
}

api/middleware.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type RequestIDCtxKey struct{}
3535
func AuthMiddleware(next http.Handler) http.Handler {
3636
// Paths to ignore this
3737
excludedPaths := map[string]bool{
38-
"/api/auth/callback": true, // http cookie is not set before logging in ie. during OAuth flow
39-
"/api/healthcheck": true, // http cookie doesn't need to present for a healthcheck
38+
"/api/auth/callback": true, // http cookie is not set before logging in ie. during OAuth flow
39+
"/api/healthcheck": true, // http cookie doesn't need to present for a healthcheck
40+
"/functions/base64Encode": true, // http cookie doesn't need to present for a healthcheck
4041
}
4142

4243
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -99,10 +100,11 @@ func RequestIDMiddleware(next http.Handler) http.Handler {
99100
// JWTMiddleware parses and validates the access token, and stores the userID in the request context.
100101
func JWTMiddleware(next http.Handler) http.Handler {
101102
excludedPaths := map[string]bool{
102-
"/api/auth/callback": true,
103-
"/api/healthcheck": true,
104-
"/api/users/logout": true,
105-
"/api/refresh": true,
103+
"/api/auth/callback": true,
104+
"/api/healthcheck": true,
105+
"/api/users/logout": true,
106+
"/api/refresh": true,
107+
"/functions/base64Encode": true,
106108
}
107109

108110
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)