Skip to content

Commit 62fa9ba

Browse files
committed
Workaround for #33 - go sub-modules
Fixes: #33 Go sub-modules do not work with this template, because the of limitations on how local imports and the "replace" word works in Go modules. You can only use "replace" in the root-level module i.e. "main.go" https://github.com/golang/go/wiki/Modules#gomod This patch was tested with the following repo: [email protected]:alexellis/golang-middleware-relative-import.git * Documentation in the README has been updated * Tested e2e and with a local "go test" in GOPATH with modules turned on and Go 1.13 * Updated copy commands in Dockerfile so that they use a faster mechanism, vs creating a new container step to chown (must be tested with buildkit before merging, since buildkit did not like this approach in the past when running as a restricted user at runtime) Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent f4de136 commit 62fa9ba

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,53 @@ func Handle(w http.ResponseWriter, r *http.Request) {
299299
w.Write([]byte(result))
300300
}
301301
```
302+
303+
#### Advanced usage - Go sub-modules via `GO_REPLACE.txt`
304+
305+
For this example you will need to be using Go 1.13 or newer and Go modules, enable this via `faas-cli build --build-arg GO111MODULE=on`.
306+
307+
Imagine you have a package which you want to store outside of the `handler.go` file, it's another middleware which can perform an echo.
308+
309+
```Golang
310+
package handlers
311+
312+
import (
313+
"io/ioutil"
314+
"net/http"
315+
)
316+
317+
func Echo(w http.ResponseWriter, r *http.Request) {
318+
319+
if r.Body != nil {
320+
defer r.Body.Close()
321+
b, _ := ioutil.ReadAll(r.Body)
322+
w.Write(b)
323+
}
324+
325+
}
326+
```
327+
328+
To include a relative module such as this new `handlers` package, you should create a `GO_REPLACE.txt` file as follows.
329+
330+
Let's say your GOPATH for your GitHub repo is: `github.com/alexellis/vault/` and your function is called `purchase`, this makes a total path of: `github.com/alexellis/vault/purchase/`
331+
332+
```
333+
replace github.com/alexellis/vault/purchase/handlers => ./function/handlers
334+
```
335+
336+
Now if you want to reference the handlers package from within your `handler.go` write the following:
337+
338+
```golang
339+
package function
340+
341+
import (
342+
"net/http"
343+
344+
"github.com/alexellis/vault/purchase/handlers"
345+
)
346+
347+
func Handle(w http.ResponseWriter, r *http.Request) {
348+
349+
handlers.Echo(w, r)
350+
}
351+
```

template/golang-middleware/Dockerfile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ RUN mkdir -p /go/src/handler
1212
WORKDIR /go/src/handler
1313
COPY . .
1414

15+
RUN $(cat function/GO_REPLACE.txt >> ./go.mod || exit 0) && cat go.mod
16+
1517
# Run a gofmt and exclude all vendored code.
1618
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }
1719

1820
ARG GO111MODULE="off"
1921
ARG GOPROXY=""
22+
ARG GOFLAGS=""
23+
24+
WORKDIR /go/src/handler/function
25+
26+
RUN go test ./... -cover
2027

28+
WORKDIR /go/src/handler
2129
RUN go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
22-
RUN go test handler/function/... -cover
2330

2431
FROM alpine:3.11
2532
# Add non root user and certs
@@ -30,11 +37,9 @@ RUN apk --no-cache add ca-certificates \
3037

3138
WORKDIR /home/app
3239

33-
COPY --from=build /go/src/handler/handler .
34-
COPY --from=build /usr/bin/fwatchdog .
35-
COPY --from=build /go/src/handler/function/ .
36-
37-
RUN chown -R app /home/app
40+
COPY --from=build --chown=app /go/src/handler/handler .
41+
COPY --from=build --chown=app /usr/bin/fwatchdog .
42+
COPY --from=build --chown=app /go/src/handler/function/ .
3843

3944
USER app
4045

0 commit comments

Comments
 (0)