Get docker running properly and add more logging

This commit is contained in:
Tom Wright
2020-04-15 19:26:21 +01:00
parent 06b5694125
commit 1bdfc9e612
7 changed files with 126 additions and 54 deletions

View File

@@ -1,5 +1,7 @@
package internal
import "fmt"
// DiagramCache provides the ability to cache diagram results.
type DiagramCache interface {
// Store stores a diagram in the cache.
@@ -26,7 +28,7 @@ type inMemoryDiagramCache struct {
func (c *inMemoryDiagramCache) Store(diagram *Diagram) error {
id, err := diagram.ID()
if err != nil {
return err
return fmt.Errorf("cannot get diagram ID: %w", err)
}
c.idToDiagram[id] = diagram
return nil
@@ -36,7 +38,7 @@ func (c *inMemoryDiagramCache) Store(diagram *Diagram) error {
func (c *inMemoryDiagramCache) Has(diagram *Diagram) (bool, error) {
id, err := diagram.ID()
if err != nil {
return false, err
return false, fmt.Errorf("cannot get diagram ID: %w", err)
}
if d, ok := c.idToDiagram[id]; ok && d != nil {
return true, nil

View File

@@ -5,6 +5,8 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
)
@@ -14,42 +16,44 @@ type Generator interface {
Generate(diagram *Diagram) error
}
func NewGenerator(cache DiagramCache, mermaidCLIPath string, inPath string, outPath string) Generator {
func NewGenerator(cache DiagramCache, mermaidCLIPath string, inPath string, outPath string, puppeteerConfigPath string) Generator {
return &cachingGenerator{
cache: cache,
mermaidCLIPath: mermaidCLIPath,
inPath: inPath,
outPath: outPath,
cache: cache,
mermaidCLIPath: mermaidCLIPath,
inPath: inPath,
outPath: outPath,
puppeteerConfigPath: puppeteerConfigPath,
}
}
// cachingGenerator is an implementation of Generator.
type cachingGenerator struct {
cache DiagramCache
mermaidCLIPath string
inPath string
outPath string
cache DiagramCache
mermaidCLIPath string
inPath string
outPath string
puppeteerConfigPath string
}
// Generate generates the given diagram.
func (c cachingGenerator) Generate(diagram *Diagram) error {
has, err := c.cache.Has(diagram)
if err != nil {
return err
return fmt.Errorf("cache.Has failed: %w", err)
}
if has {
cached, err := c.cache.Get(diagram)
if err != nil {
return err
return fmt.Errorf("cache.Get failed: %w", err)
}
*diagram = *cached
return nil
}
if err := c.generate(diagram); err != nil {
return err
return fmt.Errorf("cachingGenerater.generate failed: %w", err)
}
if err := c.cache.Store(diagram); err != nil {
return err
return fmt.Errorf("cache.Store failed: %w", err)
}
return nil
}
@@ -58,35 +62,41 @@ func (c cachingGenerator) Generate(diagram *Diagram) error {
func (c cachingGenerator) generate(diagram *Diagram) error {
id, err := diagram.ID()
if err != nil {
return err
}
has, err := c.cache.Has(diagram)
if err != nil {
return err
}
if has {
cached, err := c.cache.Get(diagram)
if err != nil {
return err
}
*diagram = *cached
return nil
return fmt.Errorf("cannot get diagram ID: %w", err)
}
inPath := fmt.Sprintf("%s/%s.mmd", c.inPath, id)
outPath := fmt.Sprintf("%s/%s.svg", c.outPath, id)
if err := ioutil.WriteFile(inPath, diagram.description, 0644); err != nil {
return err
return fmt.Errorf("could not write to input file [%s]: %w", inPath, err)
}
cmd := exec.Command(c.mermaidCLIPath, "-i", inPath, "-o", outPath)
var stdOut bytes.Buffer
cmd.Stdout = bufio.NewWriter(&stdOut)
if err := cmd.Run(); err != nil {
return fmt.Errorf("%w: %s", err, string(stdOut.Bytes()))
_, err = os.Stat(c.mermaidCLIPath)
if os.IsNotExist(err) {
return fmt.Errorf("mermaid executable does not exist: %w", err)
}
if err != nil {
return fmt.Errorf("could not stat mermaid executable: %w", err)
}
args := []string{
"-i", inPath,
"-o", outPath,
}
if c.puppeteerConfigPath != "" {
args = append(args, "-p", c.puppeteerConfigPath)
}
cmd := exec.Command(c.mermaidCLIPath, args...)
var stdOut bytes.Buffer
var stdErr bytes.Buffer
cmd.Stdout = bufio.NewWriter(&stdOut)
cmd.Stderr = bufio.NewWriter(&stdErr)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed when executing mermaid: %w: %s: %s", err, string(stdOut.Bytes()), string(stdErr.Bytes()))
}
log.Printf("generated with output: %s: %s", string(stdOut.Bytes()), string(stdErr.Bytes()))
diagram.Output = outPath

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
@@ -30,6 +31,8 @@ func writeSVG(rw http.ResponseWriter, data []byte, status int) {
}
func writeErr(rw http.ResponseWriter, err error, status int) {
log.Printf("[%d] %s", status, err)
writeJSON(rw, map[string]interface{}{
"error": err,
}, status)