Skip to content

Commit 977e64b

Browse files
committed
Add function for markdown generation
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent ecffb9f commit 977e64b

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

markdown/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module handler/function
2+
3+
go 1.18
4+
5+
require github.com/russross/blackfriday/v2 v2.1.0

markdown/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
2+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

markdown/handler.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package function
2+
3+
import (
4+
"io"
5+
"net/http"
6+
7+
"github.com/russross/blackfriday/v2"
8+
)
9+
10+
func Handle(w http.ResponseWriter, r *http.Request) {
11+
var input []byte
12+
13+
if r.Body != nil {
14+
defer r.Body.Close()
15+
16+
body, err := io.ReadAll(r.Body)
17+
if err != nil {
18+
http.Error(w, "Failed to read body", http.StatusInternalServerError)
19+
return
20+
}
21+
22+
input = body
23+
}
24+
25+
// Convert Markdown to HTML
26+
htmlContent := blackfriday.Run(input)
27+
28+
// Set response header for HTML
29+
w.Header().Set("Content-Type", "text/html")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write(htmlContent)
32+
}

stack.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,15 @@ functions:
9696
handler: ./external-ip
9797
image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/external-ip-fn:${TAG:-latest}
9898

99+
markdown:
100+
lang: golang-middleware
101+
handler: ./markdown
102+
image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/markdown-fn:${TAG:-latest}
103+
104+
99105
configuration:
100106
templates:
101107
- name: golang-middleware
102108
source: https://github.com/openfaas/golang-http-template
103109

110+

0 commit comments

Comments
 (0)