-
cloudflare-client
Lightweight universal Cloudflare API client library for Node.js, Browser, and CF Workers
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
I had been putting off some gnarly dependency upgrades for over a year and finally got around to it last week, after a few false starts of the npm variety. I eventually gave up and rebuilt from scratch using the gatsby new tool, then ported my customizations. After I finished my polishes to some assets, I noticed that the changes didn't seem to have applied on forcepush.tech. This wasn't very mysterious, as I've since put a Cloudflare cache in front of my site.
package purger import ( "fmt " "io" "log" "net/http" "strings" "github.com/GoogleCloudPlatform/functions-framework-go/functions" ) func init() { functions.HTTP("PurgeCache", purgeCache) } // httpError logs the error and returns an HTTP error message and code. func httpError(w http.ResponseWriter, err error, msg string, errorCode int) { errorMsg := fmt.Sprintf("%s: %v", msg, err) log.Printf("%s", errorMsg) http.Error(w, errorMsg, errorCode) } func purgeCache(w http.ResponseWriter, r *http.Request) { log.Printf("Received %s from %v", r.Method, r.RemoteAddr) if r.Method == "POST" { body, err := io.ReadAll(r.Body) if err != nil { httpError(w, err, "error reading POST body", http.StatusInternalServerError) return } log.Printf("Request body: %s", body) } // Send POST request to Cloudflare client := &http.Client{} data := `{"purge_everything":true}` req, err := http.NewRequest("POST", "https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache", strings.NewReader(data)) if err != nil { httpError(w, err, "error creating new Request", http.StatusInternalServerError) return } req.Header.Add("Authorization", "Bearer CLOUDFLARE-API-TOKEN") req.Header.Add("Content-Type", "application/json") cloudflareResp, err := client.Do(req) if err != nil { httpError(w, err, "error sending POST request", http.StatusInternalServerError) return } defer cloudflareResp.Body.Close() // Pass cloudflare response to caller cloudflareRespBody, err := io.ReadAll(cloudflareResp.Body) if err != nil { httpError(w, err, "error reading Cloudflare response", http.StatusInternalServerError) return } if cloudflareResp.StatusCode != http.StatusOK { msg := fmt.Sprintf("error non-200 status: %s", cloudflareRespBody) httpError(w, nil, msg, http.StatusInternalServerError) return } log.Printf("Cloudflare response: %s", cloudflareRespBody) _, err = w.Write(cloudflareRespBody) if err != nil { httpError(w, err, "error sending response to client", http.StatusInternalServerError) return } }