Skip to content

Commit

Permalink
Add function for markdown generation
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Oct 16, 2024
1 parent ecffb9f commit 977e64b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions markdown/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module handler/function

go 1.18

require github.com/russross/blackfriday/v2 v2.1.0
2 changes: 2 additions & 0 deletions markdown/go.sum
Original file line number Diff line number Diff line change
@@ -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=
32 changes: 32 additions & 0 deletions markdown/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 977e64b

Please sign in to comment.