Skip to content

Libraries

If you'd like to move serverless functions code outside of the code repository, you can do so by creating a library. Libraries allow you to share codebase but also create granular access as they have their own repositories.

Creating a library

Navigate to the Libraries tab on the left sidebar. Then click on + to create a new library.

Name the library then validate.

💡 Note: If you'd like to import an existing library, click on --Generate-- and select the library you'd like to import.

At the time of writing, only one template is available, so click on Generate to create a repository and populate it with the templates code.

Push the configuration changes. Click on the push button on the bottom right. Save the github repository id (in the example 905452907) and its fullname (in the example taubyte0/tb_library_tauhow_example_library), we'll use them later.

Type in a commit message and click on Finish to push the changes.

We can trigger a build in dream, but we'll skip it for now as the library ins not in use yet.

Using the library

Edit the library code

First, we need to edit the library code. Click on the Open icon to open the repository in a new tab.

Edit the empty.go file on github.

Change the code as follows, then click on Commit changes...

Here's the code if you'd like to copy/paste it:

package lib

import (
    "github.com/taubyte/go-sdk/event"
)

//export ping
func ping(e event.Event) uint32 {
  h, err := e.HTTP()
  if err != nil {
    return 1
  }

  h.Write([]byte("PONG"))

  return 0
}

Click on Commit changes to commit and push.

Use the library in a function

Navigate to the Functions tab on the left sidebar. Then click on + to create a new function. Name it, set the timout to 1 second, the memory limnit to 10MB, the method to GET, the domain to GeneratedDomain and the path to /lib/ping.

Click on Select a source, then select your newly created library. Don't forget to set the entry point to ping.

Push the changes. Notice the source is now your library.

Ignore the changes on the code repository. Click on Next.

💡 Note: Webconsole create some structure to facilitate an eventual switch to inline code.

Enter a commit message and click on Finish.

Head back to your terminal and trigger a build for the library:

dream inject push-specific -u blackhole \
-rid 905452907 \
-fn taubyte0/tb_library_tauhow_example_library

💡 Note: use your own repository id and fullname. If you didn't save them you can Navigate to the library, click on it then switch to YAML to find them. Another way if to just open the config repository and find them in libraries/tauhow_example_library.yaml.

Next, let's trigger a build for the configuration changes.

dream inject push-all

Wait for the builds to finish. Then click on ⚡️ to open the function's endpoint in a new tab.

You should see the PONG message.

Congratulations! You've just created and used a library in a function.

Using the library as a dependency

You can also import the library as a dependency in your inline function code as follows:

Library code

Let's add an other function to our library (which will compile to a wasm module). Open the library repository in another tab. The click on Add file > Create new file.

Name the file add.go and add the following code:

Here's the code if you'd like to copy/paste it:

package lib

//export add
func add(a, b uint32) uint64 {
  return uint64(a) + uint64(b)
}

Click on Commit changes... to commit and push.

Function code

back to Web Console. Navigate to the Functions tab on the left sidebar. Then click on + to create a new function. Click on Template Select. Select Go and empty. Close the template modal.

Name the function add, set the timout to 1 second, the memory limnit to 10MB, the method to GET, the domain to GeneratedDomain , the path to /lib/add and the entry point to doAdd.

Click on Code to switch to the code tab. Then paste the following code:

package lib

import (
    "fmt"
    "strconv"

    "github.com/taubyte/go-sdk/event"
    http "github.com/taubyte/go-sdk/http/event"
)

// Import `add` the library
//
//go:wasmimport libraries/tauhow_example_library add
func add(a, b uint32) uint64

func getQueryVarAsUint32(h http.Event, varName string) uint32 {
    varStr, err := h.Query().Get(varName)
    if err != nil {
        panic(err)
    }

    varUint, err := strconv.ParseUint(varStr, 10, 32)
    if err != nil {
        panic(err)
    }

    return uint32(varUint)
}

//export doAdd
func doAdd(e event.Event) uint32 {
    h, err := e.HTTP()
    if err != nil {
        return 1
    }

    // call the library function
    sum := add(getQueryVarAsUint32(h, "a"), getQueryVarAsUint32(h, "b"))

    // send the result over http
    h.Write([]byte(fmt.Sprintf("%d", sum)))

    return 0
}

💡 Note: libraries/<library name> is resolved within the context of the application the function is part of, then globaly. In this case the function is global so the library is only resolved globaly.

Push the changes.

Next, let's trigger a build for the configuration changes.

dream inject push-all

And you can also use curl to test the function.

curl 'http://evy8manx0.blackhole.localtau:11605/lib/add?a=40&b=2'

Output:

42

Congratulations! You've just created and used a library as a dependency in a function.