diff --git a/markdown/go.mod b/markdown/go.mod new file mode 100644 index 0000000..52d950e --- /dev/null +++ b/markdown/go.mod @@ -0,0 +1,5 @@ +module handler/function + +go 1.18 + +require github.com/russross/blackfriday/v2 v2.1.0 diff --git a/markdown/go.sum b/markdown/go.sum new file mode 100644 index 0000000..502a072 --- /dev/null +++ b/markdown/go.sum @@ -0,0 +1,2 @@ +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/markdown/handler.go b/markdown/handler.go new file mode 100644 index 0000000..8fecd6c --- /dev/null +++ b/markdown/handler.go @@ -0,0 +1,32 @@ +package function + +import ( + "io" + "net/http" + + "github.com/russross/blackfriday/v2" +) + +func Handle(w http.ResponseWriter, r *http.Request) { + var input []byte + + if r.Body != nil { + defer r.Body.Close() + + body, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Failed to read body", http.StatusInternalServerError) + return + } + + input = body + } + + // Convert Markdown to HTML + htmlContent := blackfriday.Run(input) + + // Set response header for HTML + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + w.Write(htmlContent) +} diff --git a/stack.yml b/stack.yml index ce1bb98..9e78c55 100644 --- a/stack.yml +++ b/stack.yml @@ -96,8 +96,15 @@ functions: handler: ./external-ip image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/external-ip-fn:${TAG:-latest} + markdown: + lang: golang-middleware + handler: ./markdown + image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/markdown-fn:${TAG:-latest} + + configuration: templates: - name: golang-middleware source: https://github.com/openfaas/golang-http-template +