Skip to content

Commit

Permalink
Initial commit with basic HTTP endpoint working
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Apr 6, 2020
0 parents commit c770403
Show file tree
Hide file tree
Showing 18 changed files with 1,012 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mermaidcli/node_modules/
vendor/
in/
out/
.DS_Store
.idea/
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/mermaid-server.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/watcherTasks.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 144 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This stage builds the mermaidcli executable.
FROM node:12.12.0-buster as node

WORKDIR /root

# copy the mermaidcli node package into the container and install
COPY ./mermaidcli/* .

RUN npm install


# This stage builds the go executable.
FROM golang:1.13-buster as go

WORKDIR /root
COPY . .

RUN go build -o bin/app cmd/app/main.go


# Final stage that will be pushed.
FROM debian:buster-slim

ENV DEBIAN_FRONTEND=noninteractive
RUN apt update 2>/dev/null && \
apt install -y --no-install-recommends \
ca-certificates \
2>/dev/null

COPY --from=node /root/node_modules/.bin/mmdc ./mermaidcli
COPY --from=go /root/bin/app ./app

# We should now have all the required dependencies to build the proto files.
CMD ["./app", "--mermaid=./mermaidcli"]
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# mermaid-server

Use mermaid-js to generate diagrams in a HTTP endpoint.

While this currently serves the diagrams via HTTP, it could easily be manipulated to server diagrams via other means.

## Basic usage

Start the HTTP server:
```
go run cmd/app/main.go --mermaid=./mermaidcli/node_modules/.bin/mmdc --in=./in --out=./out
```

Send CURL request to generate a diagram:
```
curl --location --request POST 'http://localhost:80/generate' \
--header 'Content-Type: text/plain' \
--data-raw 'graph LR
A-->B
B-->C
C-->D
C-->F
'
```
98 changes: 98 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"flag"
"fmt"
"mermaid-server/internal"
"net/http"
"os"
)

var testInput = []byte(`
graph TB
subgraph "Jira"
createTicket["Create ticket"]
updateTicket["Update ticket"]
fireWebhook["Fire webhook"]
createTicket-->fireWebhook
updateTicket-->fireWebhook
end
subgraph "Jira Webhook"
receiveWebhook["Receive webhook"]
storeEvent["Store event in immutable list"]
publishNewStoredEventEvent["Publish message to notify system of new event"]
createEvent["Create internal event that will be stored"]
setCreatedAt["Set created at date to now"]
setSourceJira["Set source to Jira webhook"]
receiveWebhook-->createEvent-->setCreatedAt-->setSourceJira-->storeEvent
storeEvent-->publishNewStoredEventEvent
end
fireWebhook-->receiveWebhook
subgraph "Play Event"
publishEventUpdated["Publish message to notify system of new status"]
verifyEventSource["Verify event source"]
parsePayload["Parse event payload using source to determine structure"]
findEventHandler["Find the handler for the specific event type + version"]
getLatestPersistedState["Get latest persisted state"]
changeInMemoryStateUsingEventData["Change in-memory state using event data"]
persistUpdatedState["Persist updated state"]
verifyEventSource-->parsePayload
parsePayload-->findEventHandler
findEventHandler-->getLatestPersistedState-->changeInMemoryStateUsingEventData-->persistUpdatedState
persistUpdatedState-->publishEventUpdated
end
publishNewStoredEventEvent-->verifyEventSource
`)

func main() {
mermaid := flag.String("mermaid", "", "The full path to the mermaidcli executable.")
in := flag.String("in", "", "Directory to store input files.")
out := flag.String("out", "", "Directory to store output files.")
flag.Parse()

if *mermaid == "" {
_, _ = fmt.Fprintf(os.Stderr, "Missing required argument `mermaid`")
os.Exit(1)
}

if *in == "" {
_, _ = fmt.Fprintf(os.Stderr, "Missing required argument `in`")
os.Exit(1)
}

if *out == "" {
_, _ = fmt.Fprintf(os.Stderr, "Missing required argument `out`")
os.Exit(1)
}

cache := internal.NewDiagramCache()
generator := internal.NewGenerator(cache, *mermaid, *in, *out)

httpHandler := internal.GenerateHTTPHandler(generator)

r := http.NewServeMux()
r.Handle("/generate", http.HandlerFunc(httpHandler))

httpServer := &http.Server{
Addr: ":80",
Handler: r,
}
_, _ = fmt.Fprintf(os.Stdout, "Listening on address %s", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
_, _ = fmt.Fprintf(os.Stderr, "Could not listen for http connections: %s", err)
os.Exit(1)
}

_, _ = fmt.Fprintf(os.Stdout, "Shutdown")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tomwright/mermaid-server

go 1.13
Loading

0 comments on commit c770403

Please sign in to comment.