Skip to content

Dart serverless templates to be used with OpenFaaS

License

Notifications You must be signed in to change notification settings

hypermodern-dev/dart_openfaas

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dart OpenFaaS function templates

This repository contains OpenFaaS function templates for writing serverless functions in the Dart programming language.

Usage

  1. Make sure OpenFaaS has been deployed to your Kubernetes cluster and the OpenFaaS CLI tool has been installed. See here and here for a brief introduction on how to do this.

  2. Download the Dart function templates from this repo:

faas-cli template pull https://github.com/nicklasxyz/dart_openfaas#main
  1. Create a new function:
faas-cli new --lang dart test-function

Note: This essentially creates a usual Dart project stucture, but with a pre-defined package name and files. The main functionality should be implemented in the files contained in the newly created test-function/function directory. Extra dependencies should be added to the respectivepubspec.yaml files in the root of the test-function or the test-function/function directory. The project can be compiled and tested locally as usual. See the forllowing README.md for more information.

  1. Add new functionality to the function that is going to be deployed and managed by OpenFaaS:
code template/function/lib/src/function_base.dart
# ... Inside this file extend or add whatever you want to inside the 'Service' class  
  1. Make sure a valid container registry, to where functions can be pushed, has been defined in the test-function.yml file:
code test-function.yml
  1. Finally, build, push and deploy the function:
# Authenticate with OpenFaaS (assuming kubectl is used with the k3s Kubernetes distribution):
PASSWORD=$(k3s kubectl -n openfaas get secret basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode) && \
echo "OpenFaaS admin password: $PASSWORD"
faas-cli login --gateway http://localhost:31112 --password=$PASSWORD

# Build, push and deploy:
faas-cli build -f test-function.yml
faas-cli push -f test-function.yml
faas-cli deploy -f test-function.yml --gateway=http://localhost:31112

# ... or just:
faas-cli up -f test-function.yml --gateway=http://localhost:31112

# To remove function deployments run:
faas-cli remove -f test-function.yml --gateway=http://localhost:31112
  1. Wait a few seconds, then we can invoke the function by sending a request through curl:
### Example GET request
curl -k \
  -X GET http://localhost:31112/function/test-function; \
  echo

# If nothing was changed in the 'test-function/function' directory before
# deployment then we should just see the default response:
>> Hello from Dart & Openfaas!

### Example POST request:
curl -k \
  -d "{\"name\": \"Peter\", \"age\": \"42\", \"height\": \"180.5\"}" \
  -H "Content-Type: application/json" \
  -X POST http://localhost:31112/function/test-function; \
  echo

# If nothing was changed in the 'test-function/function' directory before
# deployment then we should just see the default response:
>> [{"string_field":"Peter"}, {"int_field":42}, {"double_field":180.5}]
  1. If we just want to check that the requests were handled properly then we can check the logs:
faas-cli logs test-function --gateway http://localhost:31112

The function can also be invoked through the public interface at http://localhost:31112/ui/ using username admin and the previously defined password contained in environment variable $PASSWORD.

About

Dart serverless templates to be used with OpenFaaS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 80.7%
  • Dockerfile 19.3%