Skip to content

Go plugin guide

Ryan Wynn edited this page Jun 26, 2022 · 4 revisions

This page shows how you can build and run a custom monstache docker container image with an embedded go plugin.

Given a folder with contents...

Dockerfile  plugin.go

plugin.go as...

package main
import (
	"github.com/rwynn/monstache/v6/monstachemap"
	"strings"
)
// a plugin to convert document values to uppercase
func Map(input *monstachemap.MapperPluginInput) (output *monstachemap.MapperPluginOutput, err error) {
	doc := input.Document
	for k, v := range doc {
		switch v.(type) {
		case string:
			doc[k] = strings.ToUpper(v.(string))
		}
	}
	output = &monstachemap.MapperPluginOutput{Document: doc}
	return
}

Dockerfile as...

####################################################################################################
# Step 1: Build the app and plugin
####################################################################################################

FROM rwynn/monstache-builder-cache-rel6:1.0.7 AS build-app

RUN mkdir /app

WORKDIR /app

RUN git clone --depth 1 --branch v6.7.10 https://github.com/rwynn/monstache.git .

COPY ./plugin.go .

RUN make release

RUN go build -buildmode=plugin -o build/plugin.so plugin.go

####################################################################################################
# Step 2: Copy output build files to an alpine image
####################################################################################################

FROM rwynn/monstache-alpine:3.15.0 AS final

ENTRYPOINT ["/bin/monstache"]

COPY --from=build-app /app/build/linux-amd64/monstache /bin/monstache
COPY --from=build-app /app/build/plugin.so /bin/plugin.so

Build the image...

docker build -t monstache .

Run the container with the plugin...

docker run --rm --net=host monstache:latest -mapper-plugin-path /bin/plugin.so
Clone this wiki locally