Skip to content

Workaround for #33 - go sub-modules #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,53 @@ func Handle(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(result))
}
```

#### Advanced usage - Go sub-modules via `GO_REPLACE.txt`

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`.

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.

```Golang
package handlers

import (
"io/ioutil"
"net/http"
)

func Echo(w http.ResponseWriter, r *http.Request) {

if r.Body != nil {
defer r.Body.Close()
b, _ := ioutil.ReadAll(r.Body)
w.Write(b)
}

}
```

To include a relative module such as this new `handlers` package, you should create a `GO_REPLACE.txt` file as follows.

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/`

```
replace github.com/alexellis/vault/purchase/handlers => ./function/handlers
```

Now if you want to reference the handlers package from within your `handler.go` write the following:

```golang
package function

import (
"net/http"

"github.com/alexellis/vault/purchase/handlers"
)

func Handle(w http.ResponseWriter, r *http.Request) {

handlers.Echo(w, r)
}
```
17 changes: 11 additions & 6 deletions template/golang-middleware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

RUN $(cat function/GO_REPLACE.txt >> ./go.mod || exit 0) && cat go.mod

# Run a gofmt and exclude all vendored code.
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; }

ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

WORKDIR /go/src/handler/function

RUN go test ./... -cover

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

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

WORKDIR /home/app

COPY --from=build /go/src/handler/handler .
COPY --from=build /usr/bin/fwatchdog .
COPY --from=build /go/src/handler/function/ .

RUN chown -R app /home/app
COPY --from=build --chown=app /go/src/handler/handler .
COPY --from=build --chown=app /usr/bin/fwatchdog .
COPY --from=build --chown=app /go/src/handler/function/ .

USER app

Expand Down