Note
We will be using the interactive mode of the CLI. For non interactive mode refer to manual.
As we’ve previously discussed in the concepts section, functions, or dFunctions in Taubyte’s terminology, are an integral part of deploying your code. dFunctions encapsulate your application’s logic and can be triggered by dEvents. Now, we will walk through the steps of creating a dFunction, using both the Taubyte Web Console and the Taubyte CLI ‘tau’. Finally, we’ll examine how the creation of a function affects the configuration and inline code repositories.
Creating a dFunction using the Taubyte Web Console is a straightforward process:
Let’s start by creating our first function.
Click on the ➕ button. This action will open a modal. From there, click on the Template Select
button.
Select any available template. The ping_pong
one under Go
is as good as any.
You’ll see that the template fills out all fields except the domains. (1) Click on Select Domains
, and (2) Click on GeneratedDomain. Doing so will automatically create a domain for you.
Switch to the YAML view by clicking on the YAML
toggle.
The YAML maps 1:1 to the fields on the UI, except for id
, which will be generated at the end.
Now, let’s examine the code. To do so, click on the code
toggle.
We will delve deeper into the code later. For now, know that this function will return a string to the client (i.e., browser). So (1) go ahead and change the returned string — here, I’m changing it to "WASM PONG"
and (2) Click on the ✔️ button to create the function.
You should see the function now. Note that the id
will not be the same.
As explained in the previous section, since Git is the only interface with the Cloud, the Web Console clones the config and code repos in a browser virtual file system. You need to push your changes.
Click on the big green button in the bottom right corner.
A modal opens, containing code changes. Go ahead and click on functions/ping_pong.yaml
to show the diff.
Do the same for domains/GeneratedDomain.yaml
.
Click on Next
to move to the next section, which contains code
changes. If you locate ping_pong.go
in the file tree
, you will notice your code.
Next
to move to the commit message
section. Enter a message, then click on Finish
. This action will create a commit then push. In the Taubyte Web Console, once you’ve created and configured your function and pushed the changes, the CI/CD process is triggered automatically. This process validates your changes in both the configuration and code repositories. It’s important to note that the CI/CD jobs are launched in parallel for both repositories.
To monitor the CI/CD process:
Navigate to Builds
.
You will notice two jobs, one for config and one for code. Wait for them to show Success
.
Now, navigate to (1) Config, then (2) Production.
What you see is a JSON representation of the project on the TNS (Taubyte Name System). Click on Functions
.
You will see your ping_pong
function there. If you pay close attention to Asset
, you’ll notice that it’s a CID. In fact, it’s the CID of the WASM module generated through compiling your code.
Now, go back to Functions
.
Locate your function and click on the ⚡ to open the function URL. Note that the ⚡ will only be available for functions with the method set to GET.
Congratulations, you did it!
Note
We will be using the interactive mode of the CLI. For non interactive mode refer to manual.
The tau
CLI is a powerful tool to interact with your projects. If you have created a project using tau
from previous section you’re all set. if not you’ll need to clone it, to do so you’ll need to:
Once both repositories are cloned, your directory structure should look something like this:
├── code
└── config
└── config.yaml
In this structure, code
is the directory that contains the inline code for your project, and config
is the directory that contains the configuration files for your project, including the config.yaml
file which outlines the overall configuration of your project.
Let’s kick things off by creating our first function. The interactive prompt will first ask you to input the name of the function. Following that, you’ll be given the option to select a template. For our purpose, we’ll choose a Golang template named ‘ping_pong’.
This template will auto-fill the function attributes, but don’t worry — you’ll have the opportunity to modify them as each attribute is prompted. Once all attributes have been set, a recap table will be displayed for you to review. If everything looks good, confirm that you wish to create the function. And just like that, your function is ready.
When a function is created, both the code and config directories expand with additional files and subdirectories. Here’s a brief overview of the new structure: When a function is created, both the code
and config
directories expand with additional files and subdirectories. Here’s a brief overview of the new structure:
├── code
│ └── functions
│ └── test_doc_func_cli
│ ├── go.mod
│ ├── ping_pong.go
│ └── .taubyte
│ ├── build.sh
│ └── config.yaml
└── config
├── config.yaml
├── domains
│ └── generated.yaml
└── functions
└── test_doc_func_cli.yaml
Here’s a brief explanation of the new files:
go.mod
: This is a Go module file. It defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements.ping_pong.go
: This is the main Go code file for your function..taubyte
: Defines how the function will be built.domains/generated.yaml
: This YAML file represents the generated domain for your function.functions/test_doc_func_cli.yaml
: This is the configuration file for your function.Note that both code
and config
directories maintain a 1:1 relationship in their structure. This makes it easy to locate the configuration file or the code file for any given function, event, or domain.
The ping_pong.go
file is the source code for your function. By modifying this file, you can change the behavior of your function. Let’s append “WASM ” to the string that your function returns.
Open a terminal and navigate to the directory containing your function’s source code:
cd code/functions/test_doc_func_cli
Use the Vim text editor to open the ping_pong.go
file:
vim ping_pong.go
Once you’re in Vim, locate the following line in the function code:
h.Write([]byte("PONG"))
Replace "PONG"
with "WASM PONG"
so the line now reads:
h.Write([]byte("WASM PONG"))
Save and close the file. In Vim, you can do this by pressing Esc
to enter command mode, then typing :wq
and pressing Enter
.
You have now successfully modified your function. Instead of returning “PONG”, it will now return “WASM PONG” when it’s invoked.
So far, we’ve created a function, which led to the generation of a configuration file in the configuration repository and code files in the code repository. Additionally, we’ve modified the code slightly. For these changes to be reflected on the network, it’s necessary to push the changes in both repositories.
While it’s possible to accomplish this using standard Git commands, Taubyte’s CLI, tau, streamlines this process by simultaneously pushing changes to both repositories. This means, in one fell swoop, tau will stage, commit, and push your changes, ensuring that the updates to your function are live on the Taubyte network.
Refer to the asciinema recording below for a step-by-step visual demonstration of this process:
Keep in mind that pushing your changes will trigger the build jobs. You can view the progress of these build jobs via the web console. Check out the CI/CD section for more details.
After creating and deploying your function via the Tau CLI and pushing the changes to both repos, the function is live and can be accessed through the provided URL, in this case, https://97ft4emn0.gtau.link/ping.
If you’d like to interact with your newly deployed function, you can do so by using the curl command in your terminal as follows:
curl https://97ft4emn0.gtau.link/ping
The curl
command sends a GET request to the URL, which triggers the function. If everything is working correctly, you should see the output WASM PONG
printed to your terminal. This response indicates that your function was successfully triggered and executed the logic you defined within it.