2 Commits

Author SHA1 Message Date
Tom Wright
93ca2b4966 Update build workflow 2020-08-09 11:44:25 +01:00
Tom Wright
7f9e6c91bd Update build workflow 2020-08-09 11:41:47 +01:00
10 changed files with 33 additions and 82 deletions

View File

@@ -7,7 +7,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.15.x]
go-version: [1.13.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
@@ -16,7 +16,7 @@ jobs:
- name: Set env
run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF:10}
- name: Build
run: docker build -t tomwright/mermaid-server:latest -t tomwright/mermaid-server:${{ env.RELEASE_VERSION }} -f Dockerfile .
run: docker build --ssh default -t tomwright/mermaid-server:latest -t tomwright/mermaid-server:${{ env.RELEASE_VERSION }} -f Dockerfile .
- name: Login
run: echo ${{ secrets.DOCKER_PASS }} | docker login -u${{ secrets.DOCKER_USER }} --password-stdin
- name: Push

View File

@@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.15.x]
go-version: [1.13.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:

View File

@@ -1,5 +1,5 @@
# This stage builds the go executable.
FROM golang:1.15-buster as go
FROM golang:1.13-buster as go
WORKDIR /root
COPY ./ ./

View File

@@ -1,21 +0,0 @@
DOCKER_IMAGE=tomwright/mermaid-server:latest
CONTAINER_NAME=mermaid-server
docker-image:
docker build -t ${DOCKER_IMAGE} .
docker-run:
docker run -d --name ${CONTAINER_NAME} -p 80:80 ${DOCKER_IMAGE}
docker-stop:
docker stop ${CONTAINER_NAME} || true
docker-rm:
make docker-stop
docker rm ${CONTAINER_NAME} || true
docker-logs:
docker logs -f ${CONTAINER_NAME}
docker-push:
docker push ${DOCKER_IMAGE}

View File

@@ -22,8 +22,6 @@ go run cmd/app/main.go --mermaid=./mermaidcli/node_modules/.bin/mmdc --in=./in -
### Diagram creation
Use the query param 'type' to change between 'png' and 'svg' defaults to 'svg'.
#### POST
Send a CURL request to generate a diagram via `POST`:

2
go.mod
View File

@@ -1,5 +1,5 @@
module github.com/tomwright/mermaid-server
go 1.15
go 1.13
require github.com/tomwright/lifetime v1.0.0

View File

@@ -10,12 +10,11 @@ import (
)
// NewDiagram returns a new diagram.
func NewDiagram(description []byte, imgType string) *Diagram {
func NewDiagram(description []byte) *Diagram {
return &Diagram{
description: []byte(strings.TrimSpace(string(description))),
lastTouched: time.Now(),
mu: &sync.RWMutex{},
imgType: imgType,
}
}
@@ -31,8 +30,6 @@ type Diagram struct {
mu *sync.RWMutex
// lastTouched is the time that the diagram was last used.
lastTouched time.Time
// the type of image to generate svg or png
imgType string
}
// Touch updates the last touched time of the diagram.
@@ -58,7 +55,7 @@ func (d *Diagram) ID() (string, error) {
encoded := base64.StdEncoding.EncodeToString(d.description)
hash := md5.Sum([]byte(encoded))
d.id = hex.EncodeToString(hash[:]) + d.imgType
d.id = hex.EncodeToString(hash[:])
return d.id, nil
}

View File

@@ -79,7 +79,7 @@ func (c cachingGenerator) generate(diagram *Diagram) error {
}
inPath := fmt.Sprintf("%s/%s.mmd", c.inPath, id)
outPath := fmt.Sprintf("%s/%s.%s", c.outPath, id, diagram.imgType)
outPath := fmt.Sprintf("%s/%s.svg", c.outPath, id)
if err := ioutil.WriteFile(inPath, diagram.description, 0644); err != nil {
return fmt.Errorf("could not write to input file [%s]: %w", inPath, err)

View File

@@ -22,20 +22,12 @@ func writeJSON(rw http.ResponseWriter, value interface{}, status int) {
}
}
func writeImage(rw http.ResponseWriter, data []byte, status int, imgType string) error {
switch imgType {
case "png":
rw.Header().Set("Content-Type", "image/png")
case "svg":
rw.Header().Set("Content-Type", "image/svg+xml")
default:
return fmt.Errorf("unhandled image type: %s", imgType)
}
func writeSVG(rw http.ResponseWriter, data []byte, status int) {
rw.Header().Set("Content-Type", "image/svg+xml")
rw.WriteHeader(status)
if _, err := rw.Write(data); err != nil {
return fmt.Errorf("could not write image bytes: %w", err)
panic("could not write bytes to response: " + err.Error())
}
return nil
}
func writeErr(rw http.ResponseWriter, err error, status int) {
@@ -49,72 +41,59 @@ func writeErr(rw http.ResponseWriter, err error, status int) {
// URLParam is the URL parameter getDiagramFromGET uses to look for data.
const URLParam = "data"
func getDiagramFromGET(r *http.Request, imgType string) (*Diagram, error) {
func getDiagramFromGET(rw http.ResponseWriter, r *http.Request) *Diagram {
if r.Method != http.MethodGet {
return nil, fmt.Errorf("expected HTTP method GET")
writeErr(rw, fmt.Errorf("expected HTTP method GET"), http.StatusBadRequest)
return nil
}
queryVal := strings.TrimSpace(r.URL.Query().Get(URLParam))
if queryVal == "" {
return nil, fmt.Errorf("missing data")
writeErr(rw, fmt.Errorf("missing data"), http.StatusBadRequest)
return nil
}
data, err := url.QueryUnescape(queryVal)
if err != nil {
return nil, fmt.Errorf("could not read query param: %s", err)
writeErr(rw, fmt.Errorf("could not read query param: %s", err), http.StatusBadRequest)
return nil
}
// Create a diagram from the description
d := NewDiagram([]byte(data), imgType)
return d, nil
d := NewDiagram([]byte(data))
return d
}
func getDiagramFromPOST(r *http.Request, imgType string) (*Diagram, error) {
func getDiagramFromPOST(rw http.ResponseWriter, r *http.Request) *Diagram {
if r.Method != http.MethodPost {
return nil, fmt.Errorf("expected HTTP method POST")
writeErr(rw, fmt.Errorf("expected HTTP method POST"), http.StatusBadRequest)
return nil
}
// Get description from request body
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("could not read body: %s", err)
writeErr(rw, fmt.Errorf("could not read body: %s", err), http.StatusInternalServerError)
return nil
}
// Create a diagram from the description
d := NewDiagram(bytes, imgType)
return d, nil
d := NewDiagram(bytes)
return d
}
const URLParamImageType = "type"
// generateHTTPHandler returns a HTTP handler used to generate a diagram.
func generateHTTPHandler(generator Generator) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
var diagram *Diagram
imgType := r.URL.Query().Get(URLParamImageType)
switch imgType {
case "png", "svg":
case "":
imgType = "svg"
default:
writeErr(rw, fmt.Errorf("unsupported image type (%s) use svg or png", imgType), http.StatusBadRequest)
return
}
var err error
switch r.Method {
case http.MethodGet:
diagram, err = getDiagramFromGET(r, imgType)
diagram = getDiagramFromGET(rw, r)
case http.MethodPost:
diagram, err = getDiagramFromPOST(r, imgType)
diagram = getDiagramFromPOST(rw, r)
default:
writeErr(rw, fmt.Errorf("unexpected HTTP method %s", r.Method), http.StatusBadRequest)
return
}
if err != nil {
writeErr(rw, err, http.StatusBadRequest)
return
}
if diagram == nil {
writeErr(rw, fmt.Errorf("could not create diagram"), http.StatusInternalServerError)
return
@@ -133,8 +112,6 @@ func generateHTTPHandler(generator Generator) func(rw http.ResponseWriter, r *ht
writeErr(rw, fmt.Errorf("could not read diagram bytes: %s", err), http.StatusInternalServerError)
return
}
if err := writeImage(rw, diagramBytes, http.StatusOK, imgType); err != nil {
writeErr(rw, fmt.Errorf("could not write diagram: %w", err), http.StatusInternalServerError)
}
writeSVG(rw, diagramBytes, http.StatusOK)
}
}

View File

@@ -59,9 +59,9 @@
"integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g=="
},
"bl": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.0.3.tgz",
"integrity": "sha512-fs4G6/Hu4/EE+F75J8DuN/0IpQqNjAdC7aEQv7Qt8MHGUH7Ckv2MwTEEeN9QehD0pfIDkMI1bkHYkKy7xHyKIg==",
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.0.2.tgz",
"integrity": "sha512-j4OH8f6Qg2bGuWfRiltT2HYGx0e1QcBTrK9KAHNMwMZdQnDZFk0ZSYIpADjYCB3U12nicC5tVJwSIhwOWjb4RQ==",
"requires": {
"buffer": "^5.5.0",
"inherits": "^2.0.4",