GitHub

Applications

Function

Adding application

To add a static website application:

ok apps add

and follow the wizard questions:

Function runtime

Check out the list of supported execution environments (GCP) here.

% ok app add
? Application name: my-function
? Application type: function
? Application dir: /code/my-app/test/function/my-function
Run plugin used for application: direct
? Dir to save application YAML: /code/my-app/test/function/my-function
? URL of application: my-function.my-domian.io
? Run command of application to serve app during dev (optional, e.g. yarn dev):
? Deploy plugin used for application: gcp
? Function runtime (the runtime in which the function is going to run): nodejs14
? Function entrypoint (name of the function that will be executed when the function is triggered): main

The wizard will build a config file for you in function/my-function folder:

# Function app config.


# You can use ${var.*} expansion to source it from values.yaml per environment,
# e.g. url: ${var.base_url}/app1/


# Name of the app.
name: my-function


# The runtime in which the function is going to run, refer to cloud provider docs for possible options.
runtime: nodejs14


# Name of the function that will be executed when the function is triggered.
entrypoint: main


# Working directory of the app where all commands will be run. All other dirs will be relative to this one.
dir: ./function/my-function


# Type of the app.
type: function


# URL of the app.
url: my-function.outblocks.io
# Path redirect rewrites URL to specified path. URL path from 'url' field will be stripped and replaced with value below.
# '/' should be fine for most apps.
path_redirect: /
# If app is not meant to be accessible without auth, mark it as private.
# private: true


# Deploy defines where how deployment is handled of application during `ok deploy`.
deploy:
  plugin: gcp


# Run defines where how development is handled of application during `ok run`.
run:
  plugin: direct
  # Command to be run to for dev mode.
  command: ''


  # Additional environment variables to pass.
  # env:
  #   BROWSER: none  # disable opening browser for react app


  # Port override, by default just assigns next port starting from listen-port.
  # port: 8123

Create the function

Here is a source code of example function in index.js file:

exports.main = async (req, res) => {
  res.status(200).send('Hello World!')
}

FFor the purpose of this JS example, you will also need to add package.json file:

  "dependencies": {
    "@google-cloud/functions-framework": "^3.1.2",
    "npm-watch": "^0.11.0"
  },
  "watch": {
    "start": "*.js"
  },
  "scripts": {
    "watch": "npm-watch start",
    "start": "npx @google-cloud/functions-framework --target=main --port=8888"
  }
}

Save both files in function/my-function folder.

Running app locally

To start the app locally, you need to configure a command:

run:
  plugin: direct
  port: 8888
  # Command to be run to for dev mode.
  command: 'npm run watch'

Then run in:

ok run my-function

The command will start serving function locally and stream logs to the console:

% ok run my-function
Previous
Service