To make using Scale Functions from Golang webservers easier, we’ve created a net/http adapter that allows you to use a compiled Scale Function as a handler for your net/http server.

Getting Started

First, you’ll need a Golang application that uses the net/http standard library. If you don’t have a Golang application, you can use the following example to get started:

main.go
package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })

    log.Fatal(http.ListenAndServe(":8080", nil))
}

Next, install the Scale Runtime and the Scale HTTP Adapters package:

go get github.com/loopholelabs/scale && go get github.com/loopholelabs/scale-http-adapters

Now you’re ready to start using Scale Functions in your Golang application!

Embedding a Scale Function

To get started, let’s create a Scale Function that returns a simple ‘Hello World’ message. This is as simple as running the following command:

scale fn new hello

We’re implicitly using the Go Guest Language in this example, but you can use any of the supported Guest Languages. To use a different Guest Language, you can use the --language flag when creating a new Scale Function.

This will create a new Scale Function in the current directory. You can see the code for the Scale Function in the scale.go file that gets generated. The Scale Function is a simple HTTP handler that returns a Hello World message:

//go:build tinygo || js || wasm
package scale

import (
	signature "github.com/loopholelabs/scale-signature-http"
)

func Scale(ctx *signature.Context) (*signature.Context, error) {
	ctx.Response().SetBody("Hello, World!")
	return ctx
}

Now lets build the Scale Function and export it locally:

scale build

At this point, we can choose to either embed the Scale Function into our Golang application at build time or import it using the Scale Registry at runtime. In this example we’ll embed the Scale Function into our Golang application at build time, but you can learn how to import the Scale Function from the Scale Registry using our Importing Scale Functions guide.

Run the following command to export the Scale Function locally:

scale function export hello:latest .

This will create a hello-latest.scale file in the current directory. Now we have everything we need to import the Scale Function into our Golang application.

Check out the Scale CLI documentation for more information on how to use the Scale CLI to build and export Scale Functions.

To import the Scale Function into our Next.js App, it’s as simple as using the native import statement:

main.go
package main

import (
    "fmt"
    "log"
	"embed"
	"io"
    "net/http"
	scale "github.com/loopholelabs/scale/go"
	adapter "github.com/loopholelabs/scale-http-adapters/http"
    "github.com/loopholelabs/scalefile/scalefunc"
)

//go:embed hello-latest.scale
var embeddedFunction []byte

func main() {
	sf := new(ScaleFunc)
	_ = sf.Decode(embeddedFunction)

	r, _ := scale.New(context.Background(), []*scalefunc.ScaleFunc{sf})
    handler := adapter.New(nil, r)

    http.Handle("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Now we can run our Golang application and test out our Scale Function:

go run main.go