/http-go-fn

A buildpack for wrapping Go http.HandlerFunc functions with a `package main`

Primary LanguageGoApache License 2.0Apache-2.0

http-go-fn

go.dev reference Go Report Card Releases LICENSE codecov

This repository implements a Go function buildpack for wrapping functions matching http.HandlerFunc. This buildpack is not standalone, it should be composed with the Paketo Go buildpacks.

Build this buildpack

This buildpack can be built (from the root of the repo) with:

pack package-buildpack my-buildpack --config ./package.toml

Use this buildpack

# This runs the http-go-fn buildpack at HEAD within the Paketo Go order.
# You can pin to a release by replacing ":main" below with a release tag
# e.g. ":v0.0.1"
pack build -v test-container \
  --pull-policy if-not-present \
  --buildpack gcr.io/paketo-buildpacks/go-dist:0.2.5 \
  --buildpack ghcr.io/mattmoor/http-go-fn:main \
  --buildpack gcr.io/paketo-buildpacks/go-mod-vendor:0.0.169 \
  --buildpack gcr.io/paketo-buildpacks/go-build:0.1.2

Sample function

With this buildpack, users can define a trivial Go function that implements http.HandlerFunc. For example, the following function:

package fn

import (
       "fmt"
       "net/http"
)

func Handler(w http.ResponseWriter, r *http.Request) {
     fmt.Fprintf(w, "Hello World, %#v", r)
}

Configuration

You can configure both the package containing the function and the name of the function via the following configuration options in project.toml:

[[build.env]]
name = "HTTP_GO_PACKAGE"
value = "./blah"

[[build.env]]
name = "HTTP_GO_FUNCTION"
value = "MyCustomHandlerName"