-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (35 loc) · 887 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"context"
"flag"
"log"
mermaid_go "github.com/dreampuf/mermaid.go"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
var addr = flag.String("addr", ":8088", "TCP address to listen to")
func generateGraph(content string) []byte {
re, err := mermaid_go.NewRenderEngine(context.Background())
if err != nil {
log.Panicln(err.Error())
}
defer re.Cancel()
// content := `pie title pie_graph
// "dimension 1": 60
// "dimension 2": 40
// "dimension 3": 100`
pngInBytes, _, err := re.RenderAsPng(content)
if err != nil {
log.Panicln(err.Error())
}
return pngInBytes
}
func requestHandler(ctx *fasthttp.RequestCtx) {
input := ctx.Request.Body()
ctx.Response.AppendBody(generateGraph(string(input)))
}
func main() {
r := router.New()
r.POST("/make-charts", requestHandler)
log.Fatal(fasthttp.ListenAndServe(*addr, r.Handler))
}