403 lines
5.2 KiB
Go
403 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var diagramSourceCache = map[string]diagramCacheEntry{}
|
|
|
|
type diagramCacheEntry struct {
|
|
Type string
|
|
Source string
|
|
At time.Time
|
|
}
|
|
|
|
func diagramHash(
|
|
diagramType string,
|
|
source string,
|
|
format string,
|
|
) string {
|
|
|
|
sum :=
|
|
sha256.Sum256(
|
|
[]byte(
|
|
diagramType + "\x00" + format + "\x00" + source,
|
|
),
|
|
)
|
|
|
|
return hex.EncodeToString(
|
|
sum[:],
|
|
)
|
|
}
|
|
|
|
func allowedDiagramType(
|
|
t string,
|
|
) bool {
|
|
|
|
switch t {
|
|
case "mermaid", "plantuml", "graphviz", "dot", "d2":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func normalizeDiagramType(
|
|
t string,
|
|
) string {
|
|
|
|
switch t {
|
|
case "dot":
|
|
return "graphviz"
|
|
default:
|
|
return t
|
|
}
|
|
}
|
|
|
|
func krokiURL() string {
|
|
|
|
v :=
|
|
strings.TrimSpace(
|
|
os.Getenv("KROKI_URL"),
|
|
)
|
|
|
|
if v == "" {
|
|
return "http://kroki:8000"
|
|
}
|
|
|
|
return strings.TrimRight(
|
|
v,
|
|
"/",
|
|
)
|
|
}
|
|
|
|
func (s *Server) renderDiagramsInHTML(
|
|
html string,
|
|
) string {
|
|
|
|
re :=
|
|
regexp.MustCompile(`(?s)<pre><code class="language-(mermaid|plantuml|graphviz|dot|d2)">(.+?)</code></pre>`)
|
|
|
|
return re.ReplaceAllStringFunc(
|
|
html,
|
|
func(block string) string {
|
|
|
|
m :=
|
|
re.FindStringSubmatch(
|
|
block,
|
|
)
|
|
|
|
if len(m) != 3 {
|
|
return block
|
|
}
|
|
|
|
diagramType :=
|
|
normalizeDiagramType(
|
|
m[1],
|
|
)
|
|
|
|
source :=
|
|
htmlUnescapeString(
|
|
m[2],
|
|
)
|
|
|
|
hash :=
|
|
diagramHash(
|
|
diagramType,
|
|
source,
|
|
"svg",
|
|
)
|
|
|
|
diagramSourceCache[hash] =
|
|
diagramCacheEntry{
|
|
Type: diagramType,
|
|
Source: source,
|
|
At: time.Now(),
|
|
}
|
|
|
|
return `<figure class="diagram diagram-` + diagramType + `">` +
|
|
`<img src="/:/compile/diagram/` + diagramType + `/` + hash + `.svg" loading="lazy" alt="` + diagramType + ` diagram">` +
|
|
`</figure>`
|
|
},
|
|
)
|
|
}
|
|
|
|
func htmlUnescapeString(
|
|
s string,
|
|
) string {
|
|
|
|
r :=
|
|
strings.NewReplacer(
|
|
"<", "<",
|
|
">", ">",
|
|
"&", "&",
|
|
""", `"`,
|
|
"'", "'",
|
|
)
|
|
|
|
return r.Replace(
|
|
s,
|
|
)
|
|
}
|
|
|
|
func (s *Server) handleCompileDiagram(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
if r.Method == http.MethodPost {
|
|
s.handleCompileDiagramPost(
|
|
w,
|
|
r,
|
|
)
|
|
return
|
|
}
|
|
|
|
rest :=
|
|
strings.TrimPrefix(
|
|
r.URL.Path,
|
|
"/:/compile/diagram/",
|
|
)
|
|
|
|
parts :=
|
|
strings.Split(
|
|
rest,
|
|
"/",
|
|
)
|
|
|
|
if len(parts) != 2 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
diagramType :=
|
|
normalizeDiagramType(
|
|
parts[0],
|
|
)
|
|
|
|
if !allowedDiagramType(diagramType) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
hashFile :=
|
|
parts[1]
|
|
|
|
if !strings.HasSuffix(hashFile, ".svg") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
hash :=
|
|
strings.TrimSuffix(
|
|
hashFile,
|
|
".svg",
|
|
)
|
|
|
|
if !regexp.MustCompile(`^[a-f0-9]{64}$`).MatchString(hash) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
cachePath :=
|
|
filepath.Join(
|
|
"/data/cache/diagram",
|
|
diagramType,
|
|
hash+".svg",
|
|
)
|
|
|
|
if b, err := os.ReadFile(cachePath); err == nil {
|
|
|
|
w.Header().Set(
|
|
"Content-Type",
|
|
"image/svg+xml; charset=utf-8",
|
|
)
|
|
|
|
_, _ = w.Write(b)
|
|
return
|
|
}
|
|
|
|
entry, ok :=
|
|
diagramSourceCache[hash]
|
|
|
|
if !ok || entry.Type != diagramType {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
s.renderDiagramSVG(
|
|
w,
|
|
diagramType,
|
|
entry.Source,
|
|
cachePath,
|
|
)
|
|
}
|
|
|
|
func (s *Server) handleCompileDiagramPost(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
rest :=
|
|
strings.TrimPrefix(
|
|
r.URL.Path,
|
|
"/:/compile/diagram/",
|
|
)
|
|
|
|
diagramType :=
|
|
normalizeDiagramType(
|
|
strings.Trim(rest, "/"),
|
|
)
|
|
|
|
if !allowedDiagramType(diagramType) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
sourceBytes, err :=
|
|
io.ReadAll(
|
|
io.LimitReader(
|
|
r.Body,
|
|
1024*1024,
|
|
),
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
source :=
|
|
string(sourceBytes)
|
|
|
|
hash :=
|
|
diagramHash(
|
|
diagramType,
|
|
source,
|
|
"svg",
|
|
)
|
|
|
|
cachePath :=
|
|
filepath.Join(
|
|
"/data/cache/diagram",
|
|
diagramType,
|
|
hash+".svg",
|
|
)
|
|
|
|
if b, err := os.ReadFile(cachePath); err == nil {
|
|
|
|
w.Header().Set(
|
|
"Content-Type",
|
|
"image/svg+xml; charset=utf-8",
|
|
)
|
|
|
|
_, _ = w.Write(b)
|
|
return
|
|
}
|
|
|
|
s.renderDiagramSVG(
|
|
w,
|
|
diagramType,
|
|
source,
|
|
cachePath,
|
|
)
|
|
}
|
|
|
|
func (s *Server) renderDiagramSVG(
|
|
w http.ResponseWriter,
|
|
diagramType string,
|
|
source string,
|
|
cachePath string,
|
|
) {
|
|
|
|
req, err :=
|
|
http.NewRequest(
|
|
http.MethodPost,
|
|
krokiURL()+"/"+diagramType+"/svg",
|
|
bytes.NewBufferString(source),
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
req.Header.Set(
|
|
"Content-Type",
|
|
"text/plain; charset=utf-8",
|
|
)
|
|
|
|
client :=
|
|
http.Client{
|
|
Timeout: 15 * time.Second,
|
|
}
|
|
|
|
resp, err :=
|
|
client.Do(
|
|
req,
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 502)
|
|
return
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 ||
|
|
resp.StatusCode >= 300 {
|
|
|
|
http.Error(w, "diagram renderer failed", 502)
|
|
return
|
|
}
|
|
|
|
b, err :=
|
|
io.ReadAll(
|
|
resp.Body,
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
_ =
|
|
os.MkdirAll(
|
|
filepath.Dir(cachePath),
|
|
0755,
|
|
)
|
|
|
|
_ =
|
|
os.WriteFile(
|
|
cachePath,
|
|
b,
|
|
0644,
|
|
)
|
|
|
|
w.Header().Set(
|
|
"Content-Type",
|
|
"image/svg+xml; charset=utf-8",
|
|
)
|
|
|
|
w.Header().Set(
|
|
"Content-Security-Policy",
|
|
"default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
|
)
|
|
|
|
_, _ =
|
|
w.Write(
|
|
b,
|
|
)
|
|
}
|