net/http Package
The net/http Adapter makes it easy to use Scale Functions with golang net/http standard library.
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:
Next, install the Scale Runtime and the Scale HTTP Adapters package:
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:
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:
Now lets build the Scale Function and export it locally:
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:
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:
Now we can run our Golang application and test out our Scale Function:
Last updated on