Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generated code

When using the provided templates, the STACKIT Functions builder supplies an entrypoint for your function that takes care of starting an HTTP server and handling health and readiness checks.

This way you can focus on writing only the parts of your function that matter.

Generated code - Go

When using the Go template, place your code in a package called “function”, and exposes a Handler function with the same signature as that expected by ServeMux.HandleFunc:

package function

func Handle(w http.ResponseWriter, r *http.Request) {
    // ... Your code goes here
}

Generated code - NodeJS

When using the JavaScript template, place your code in a nodejs package, named "fn", with it’s "main" file exposing either a handler configuation, or a function called new which returns the same.

{ // in package.json
    // ..
    "name": "fn",
    "main": "handler.js"
}
// handler.js

// Option 1: implement the FnInstance interface directly:
module.exports = {
    async handle(context, data) {
        // ... Your code goes here
    }
}
// Option 2: export a new function which returns a FnInstance:
module.exports.new = function() {
    return {
        async handle(context, data) {
            // ... Your code goes here
        }
    }
}

The handler is expected to fit the following interface:

type FnInstance = {
    init?: () => void | Promise<void>;
    shutdown?: () => void | Promise<void>;
    handle: (context: Context, data: string | object) => Promise<Response>,
};
type Context = {
    req: http.IncomingMessage;
    res: http.ServerResponse;

    method: string;
    path: string;
    url: string;
    headers: Record<string, unknown>;
    query: Record<string, string>;
    httpVersionMajor: number;
    httpVersionMinor: number;
    rawBody: Buffer;
    body: string | object;
    log?: { info: (...a: any[]) => void; error: (...a: any[]) => void };
    cloudevent?: CloudEvent;
    cloudEventResponse(data): CloudEventBuilder;
};
type Response =
    void |
    {statusCode?: number, headers?: Record<string, unknown>, body: Body} |
    Body;
type Body =  CloudEvent | Buffer | object;

Generated code - Python

When using the Python template, place your code in a file called handler.py, which has a function called new() that returns a handler object.

The handle function of the handler is an ASGI application.

def new():
    return Function()

class Function:
    async def start(self):
        return
    async def stop(self):
        return
    def handler(self, scope, receive, send):
        # ... Your code goes here, as an ASGI handler
        pass

Generated code - Rust

When using the Rust template, create a Rust library package with the following structure:

#![allow(unused)]
fn main() {
pub mod handler {
    pub const index: impl actix_web::Handler;
    // e.g.: pub async fn index(actix_web::HttpRequest) -> actix_web::HttpResponse;
}
pub mod config {
    pub fn configure(cfg: &mut actix_web::web::ServiceConfig);
}
}

You can define the config::configure function to change how the actix_web HTTP server is configured, and define any handler::index function signature that matches the actix_web::Handler trait to handle the incoming HTTP requests.

Generated code - Java Quarkus

The Quarkus template expects a codebase which uses the Quarkus Fungy framework.

Generated code - Java Spring Boot

The Spring Boot template expects a codebase with a main method, which follows the Functions container interface.

Note that we use Spring Boot Native in order to minimize the memory usage of your Spring Boot-based function.