From 9b0d8fb7c6135794079c7b7dfebf25071e5f0c79 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 24 Oct 2023 10:04:34 +0100 Subject: [PATCH] Add external-ip function by Alex Ellis Original source from: https://github.com/openfaas/cloud-functions Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- external-ip/handler.go | 31 +++++++++++++++++++++++++++++++ stack.yml | 5 +++++ 2 files changed, 36 insertions(+) create mode 100644 external-ip/handler.go diff --git a/external-ip/handler.go b/external-ip/handler.go new file mode 100644 index 0000000..b56cd08 --- /dev/null +++ b/external-ip/handler.go @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Alex Ellis, OpenFaaS Ltd +// Licensed under the MIT license, see LICENSE.md file. + +package function + +import ( + "io" + "net/http" +) + +func Handle(w http.ResponseWriter, r *http.Request) { + + if r.Body != nil { + defer r.Body.Close() + } + + res, err := http.Get("https://api.ipify.org") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + for k, v := range res.Header { + w.Header().Set(k, v[0]) + } + + if res.Body != nil { + defer res.Body.Close() + io.Copy(w, res.Body) + } +} diff --git a/stack.yml b/stack.yml index 8dbe991..ce1bb98 100644 --- a/stack.yml +++ b/stack.yml @@ -91,6 +91,11 @@ functions: handler: ./certinfo image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/certinfo-fn:${TAG:-latest} + external-ip: + lang: golang-middleware + handler: ./external-ip + image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/external-ip-fn:${TAG:-latest} + configuration: templates: - name: golang-middleware