Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions template/golang-http/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.10.11 AS watchdog
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.24.0-alpine AS build
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.10.12 AS watchdog
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.25-alpine AS build

ARG TARGETPLATFORM
ARG BUILDPLATFORM
Expand Down Expand Up @@ -36,7 +36,7 @@ WORKDIR /go/src/handler
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOFLAGS=${GOFLAGS} \
go build --ldflags "-s -w" -o handler .

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.22.0 AS ship
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.22.2 AS ship
# Add non root user and certs

RUN apk --no-cache add ca-certificates \
Expand Down
1 change: 1 addition & 0 deletions template/golang-middleware-inproc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/handler
59 changes: 59 additions & 0 deletions template/golang-middleware-inproc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.25-alpine AS build

ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

RUN apk --no-cache add git

RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

ARG GO111MODULE="on"
ARG GOPROXY=""
ARG GOFLAGS=""
ARG CGO_ENABLED=0
ENV CGO_ENABLED=${CGO_ENABLED}

# 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; }

WORKDIR /go/src/handler/function
RUN mkdir -p /go/src/handler/function/static

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover

WORKDIR /go/src/handler
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build --ldflags "-s -w" -o handler .

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.22.1 AS ship

# Add non root user and certs
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app

# Split instructions so that buildkit can run & cache
# the previous command ahead of time.
RUN mkdir -p /home/app \
&& chown app /home/app

WORKDIR /home/app

COPY --from=build --chown=app /go/src/handler/handler .
COPY --from=build --chown=app /go/src/handler/function/static static

USER app

ENV fprocess=""
ENV mode="inproc"
ENV upstream_url=""
ENV prefix_logs="false"

CMD ["./handler"]
3 changes: 3 additions & 0 deletions template/golang-middleware-inproc/function/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module handler/function

go 1.24.0
37 changes: 37 additions & 0 deletions template/golang-middleware-inproc/function/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package function

import (
"fmt"
"io"
"log/slog"
"os"

"net/http"
"time"
)

func Handle(w http.ResponseWriter, r *http.Request) {
var input []byte

log := slog.New(slog.NewTextHandler(os.Stdout, nil)).
With("X-Call-Id", r.Header.Get("X-Call-Id"))

log.Info("received request")

if r.Body != nil {
defer r.Body.Close()

body, _ := io.ReadAll(r.Body)

input = body
}

log.Info("Sleeping for 1 milliseconds")
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammatical error: '1 milliseconds' should be '1 millisecond' (singular).

Suggested change
log.Info("Sleeping for 1 milliseconds")
log.Info("Sleeping for 1 millisecond")

Copilot uses AI. Check for mistakes.

time.Sleep(time.Millisecond * 1)

log.Info("Sleep done")

w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Body: %s", string(input))))
}
29 changes: 29 additions & 0 deletions template/golang-middleware-inproc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module handler

go 1.24.0

require (
github.com/google/uuid v1.6.0
github.com/openfaas/of-watchdog v0.0.0-20251114100857-67a6309db21b
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openfaas/faas-middleware v1.2.5 // indirect
github.com/openfaas/faas-provider v0.25.8 // indirect
github.com/prometheus/client_golang v1.23.0 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.65.0 // indirect
github.com/prometheus/procfs v0.17.0 // indirect
github.com/rakutentech/jwk-go v1.2.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sys v0.35.0 // indirect
google.golang.org/protobuf v1.36.7 // indirect
)

// replace github.com/openfaas/of-watchdog => ../../../of-watchdog
68 changes: 68 additions & 0 deletions template/golang-middleware-inproc/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/pprof v0.0.0-20250125003558-7fdb3d7e6fa0 h1:my2ucqBZmv+cWHIhZNSIYKzgN8EBGyHdC7zD5sASRAg=
github.com/google/pprof v0.0.0-20250125003558-7fdb3d7e6fa0/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo/v2 v2.22.2 h1:/3X8Panh8/WwhU/3Ssa6rCKqPLuAkVY2I0RoyDLySlU=
github.com/onsi/ginkgo/v2 v2.22.2/go.mod h1:oeMosUL+8LtarXBHu/c0bx2D/K9zyQ6uX3cTyztHwsk=
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
github.com/openfaas/faas-middleware v1.2.5 h1:RXPsuXw1PsxPsQ0MJz0ud3mU6X+Rg8v50ZAPxapVJ1g=
github.com/openfaas/faas-middleware v1.2.5/go.mod h1:YuGnJ7wVW/dJEzZCltHYKGGHwfelQMPWt205m6PNfVE=
github.com/openfaas/faas-provider v0.25.8 h1:3W1ZyhUvpqTOiNQoV7jzdZhDJeJJlJelosAtjFzMNyw=
github.com/openfaas/faas-provider v0.25.8/go.mod h1:rMXbj+AYVpn82UoHIOgWHiDeV118t0bSxyoC9d00jpc=
github.com/openfaas/of-watchdog v0.0.0-20251111152958-6696d30d7c8a h1:RInh0UKCMMA8QG40kcDNs4TQHOQcwR3xtcWtO21RsTo=
github.com/openfaas/of-watchdog v0.0.0-20251111152958-6696d30d7c8a/go.mod h1:HjFTzt+rGkPtXEagz6qk3yTuqjJj52UPWeeSF02fosw=
github.com/openfaas/of-watchdog v0.0.0-20251114100857-67a6309db21b h1:xrTwtQQBowj2SAfIHxZjXWddYPlU9Yb9g52XwrJ70s4=
github.com/openfaas/of-watchdog v0.0.0-20251114100857-67a6309db21b/go.mod h1:HjFTzt+rGkPtXEagz6qk3yTuqjJj52UPWeeSF02fosw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.23.0 h1:ust4zpdl9r4trLY/gSjlm07PuiBq2ynaXXlptpfy8Uc=
github.com/prometheus/client_golang v1.23.0/go.mod h1:i/o0R9ByOnHX0McrTMTyhYvKE4haaf2mW08I+jGAjEE=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE=
github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0=
github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw=
github.com/rakutentech/jwk-go v1.2.0 h1:vNJwedPkRR+32V5WGNj0JP4COes93BGERvzQLBjLy4c=
github.com/rakutentech/jwk-go v1.2.0/go.mod h1:pI0bYVntqaJ27RCpaC75MTUacheW0Rk4+8XzWWe1OWM=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
google.golang.org/protobuf v1.36.7 h1:IgrO7UwFQGJdRNXH/sQux4R1Dj1WAKcLElzeeRaXV2A=
google.golang.org/protobuf v1.36.7/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 6 additions & 0 deletions template/golang-middleware-inproc/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.25.0

use (
.
./function
)
80 changes: 80 additions & 0 deletions template/golang-middleware-inproc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"context"
"handler/function"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/google/uuid"
"github.com/openfaas/of-watchdog/config"

"github.com/openfaas/of-watchdog/pkg"
)

const defaultTimeout = 10 * time.Second

func main() {

os.Setenv("mode", "inproc")

if _, ok := os.LookupEnv("exec_timeout"); !ok {
os.Setenv("exec_timeout", defaultTimeout.String())
}

extendedTimeout := time.Duration(defaultTimeout + time.Millisecond*100)

if _, ok := os.LookupEnv("read_timeout"); !ok {
os.Setenv("read_timeout", extendedTimeout.String())
}
if _, ok := os.LookupEnv("write_timeout"); !ok {
os.Setenv("write_timeout", extendedTimeout.String())
}

if v, ok := os.LookupEnv("healthcheck_interval"); !ok {
os.Setenv("healthcheck_interval", os.Getenv("write_timeout"))
} else {
interval, _ := time.ParseDuration(v)
if interval <= time.Millisecond*0 {
os.Setenv("healthcheck_interval", "1ms")
}
}

cfg, err := config.New(os.Environ())
if err != nil {
log.Fatalf("failed to parse watchdog config: %v", err)
}

log.Printf("Watchdog config: %+v\n", cfg)

h := func(w http.ResponseWriter, r *http.Request) {
if _, ok := r.Header["X-Call-Id"]; !ok {
r.Header.Set("X-Call-Id", uuid.New().String())
}
function.Handle(w, r)
}

cfg.SetHandler(h)
cfg.OperationalMode = config.ModeInproc

watchdog := pkg.NewWatchdog(cfg)

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
s := <-sigs
log.Printf("Signal received: %s", s.String())
cancel()
}()

if err := watchdog.Start(ctx); err != nil {
log.Fatalf("failed to start watchdog: %v", err)
}
}
12 changes: 12 additions & 0 deletions template/golang-middleware-inproc/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: golang-middleware-inproc
fprocess: ./handler
welcome_message: |
You have created a new function which uses Go 1.25 and Alpine
Linux as its base image. The go.mod version is set to 1.24.0
for compatibility.

To disable the go module, for private vendor code, please use
"--build-arg GO111MODULE=off" with faas-cli build or configure this
via your stack.yml file.

Learn more: https://docs.openfaas.com/languages/go/
6 changes: 3 additions & 3 deletions template/golang-middleware/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.10.11 AS watchdog
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.24.0-alpine AS build
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.10.12 AS watchdog
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.25-alpine AS build

# Multi-arch build args
ARG TARGETPLATFORM
Expand Down Expand Up @@ -34,7 +34,7 @@ WORKDIR /go/src/handler
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build --ldflags "-s -w" -o handler .

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.22.0 AS ship
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.22.2 AS ship

# Add non root user and certs
RUN apk --no-cache add ca-certificates \
Expand Down
5 changes: 3 additions & 2 deletions template/golang-middleware/template.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: golang-middleware
fprocess: ./handler
welcome_message: |
You have created a new function which uses go 1.23 and Alpine
Linux as its base image.
You have created a new function which uses Go 1.25 and Alpine
Linux as its base image. The go.mod version is set to 1.24.0
for compatibility.

To disable the go module, for private vendor code, please use
"--build-arg GO111MODULE=off" with faas-cli build or configure this
Expand Down
Loading