|
1 | | -OpenFaaS Customer Code Sample |
| 1 | +# OpenFaaS Function Editor Example |
2 | 2 |
|
| 3 | +This repo contains a sample application that shows how OpenFaaS can be used to build a basic function editor that let's users edit, deploy and invoke custom code from the browser. |
| 4 | + |
| 5 | +The sample app consists of two parts. A frontend implemented as a single page [React](https://react.dev/) application and [Express](https://expressjs.com/) server for the backend API. Users can edit a Node.js function in the UI using a code editor. Clicking the `Publish and Deploy` button deploys the function to OpenFaaS. Once deployed the `Test Function` page can be used to invoke the function, inspect responses and view the function logs. |
| 6 | + |
| 7 | +It sample app is a basic implementation of the use case described in the our blog post: [Integrate FaaS Capabilities into Your Platform with OpenFaaS](https://www.openfaas.com/blog/add-a-faas-capability/) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## How it works |
| 12 | + |
| 13 | +The application uses readily available OpenFaaS APIs to take user-supplied source-code, produce an OpenFaaS function image and deploy it to OpenFaaS to get a custom HTTP endpoint. |
| 14 | + |
| 15 | +OpenFaaS components used by the sample application: |
| 16 | + |
| 17 | +- [Function Builder API](https://docs.openfaas.com/openfaas-pro/builder/) |
| 18 | + |
| 19 | + Allows code to be submitted, built, and deployed seamlessly. |
| 20 | + |
| 21 | + This REST API accepts a Docker build context and publishes a container image to a remote registry. |
| 22 | + |
| 23 | +- [OpenFaaS REST API](https://docs.openfaas.com/reference/rest-api/) |
| 24 | + |
| 25 | + API for managing and invoking functions, secrets and namespaces. |
| 26 | + |
| 27 | + The OpenFaaS REST API has endpoints to create and manage tenant namespaces, to deploy new functions, list and query existing ones, invoke them and query function logs. |
| 28 | + |
| 29 | +**Overview** |
| 30 | + |
| 31 | +1. User supplied source-code from the editor is send to the backends `/api/publish` endpoint. |
| 32 | +2. The publish endpoint prepares the build context using a function template and invokes the [OpenFaaS Function Builder REST API](https://docs.openfaas.com/openfaas-pro/builder/). |
| 33 | +3. The OpenFaaS function builder builds the container image for the function and published it to the configured registry. |
| 34 | +4. After the function has been published the `/api/publish` endpoint is called. The backend server calls the [OpenFaaS API](https://docs.openfaas.com/reference/rest-api/) to deploy the function. |
| 35 | +5. The function is ready to be invoked over HTTP. |
| 36 | + |
| 37 | +A couple of additional OpenFaaS API endpoints are exposed through the backed server that allow users to invoke the function and inspect logs in the UI: |
| 38 | + |
| 39 | +- `/api/invoke` - Proxies the functions HTTP endpoint. |
| 40 | +- `/api/logs` - Uses the [OpenFaaS APIs logs endpoint](https://docs.openfaas.com/reference/rest-api/#logs) to get the logs for the function. |
| 41 | + |
| 42 | +> Note: Authentication for the backend API as this is out of scope for this examples. Keep in mind the for a production ready app some form of authentication should be added to protect the API endpoint. |
| 43 | +
|
| 44 | +## Quick start |
| 45 | + |
| 46 | +Run the sample application locally. |
| 47 | + |
| 48 | +### Prerequisites |
| 49 | + |
| 50 | + |
| 51 | +A Kubernetes cluster with OpenFaaS and the [OpenFaaS Function Builder API](https://docs.openfaas.com/openfaas-pro/builder/). |
| 52 | + |
| 53 | +> The Function Builder API provides a simple REST API to create your functions from source code. See [Function Builder API docs](https://docs.openfaas.com/openfaas-pro/builder/) for installation instructions. |
| 54 | +
|
| 55 | +You will need a recent version of [Node.js](https://nodejs.org/en) to run the sample app locally. |
| 56 | + |
| 57 | +### Run the app |
| 58 | + |
| 59 | +Install node_modules: |
| 60 | + |
| 61 | +```sh |
| 62 | +cd client |
| 63 | +npm install |
| 64 | +``` |
| 65 | + |
| 66 | +**Run the API server** |
| 67 | + |
| 68 | +Configuration parameters: |
| 69 | + |
| 70 | +- `IMAGE_PREFIX` - Image prefix used for pushing the images, e.g. `docker.io/openfaas`. Make sur your function builder [has the correct permissions](https://github.com/openfaas/faas-netes/tree/master/chart/pro-builder#registry-authentication) to push to this registry. |
| 71 | +- `BUILDER_URL` - URl of the function builder API (default: http://127.0.0.1:8081) |
| 72 | +- `BUILDER_PAYLOAD_SECRET` - Path the file containing the HMAC signing secret created during the installation of the function builder. (default: "./builder/payload.txt") |
| 73 | +- `GATEWAY_URL` - URL of the OpenFaaS Gateway (default: http://127.0.0.1:8080) |
| 74 | +- `BASIC_AUTH_SECRET` - Basic auth secret to authenticate with the OpenFaaS Gateway (default: "./secrets/basic-auth-password.txt") |
| 75 | + |
| 76 | +- [Function Builder examples](https://github.com/openfaas/function-builder-examples) |
| 77 | + |
| 78 | +Make sure the pro-builder is port-forwarded to port 8081 on the local host. |
| 79 | + |
| 80 | +```sh |
| 81 | +kubectl port-forward \ |
| 82 | + -n openfaas \ |
| 83 | + svc/pro-builder 8081:8080 |
| 84 | +``` |
| 85 | + |
| 86 | +Save the HMAC signing secret created during the installation to a file `./client/.secrets/payload.txt`. |
| 87 | + |
| 88 | +```sh |
| 89 | +kubectl get secret \ |
| 90 | + -n openfaas payload-secret -o jsonpath='{.data.payload-secret}' \ |
| 91 | + | base64 --decode \ |
| 92 | + > .secrets/payload.txt |
| 93 | +``` |
| 94 | + |
| 95 | +Port forward the OpenFaaS Gateway: |
| 96 | + |
| 97 | +```sh |
| 98 | +kubectl port-forward \ |
| 99 | + -n openfaas \ |
| 100 | + svc/gateway 8080:8080 |
| 101 | +``` |
| 102 | + |
| 103 | +Start the server: |
| 104 | + |
| 105 | +```sh |
| 106 | +IMAGE_PREFIX="docker.io/your-repo" \ |
| 107 | +npm run server |
| 108 | +``` |
| 109 | + |
| 110 | +**Run the frontend** |
| 111 | + |
| 112 | +Start the frontend server: |
| 113 | + |
| 114 | +```sh |
| 115 | +npm run dev |
| 116 | +``` |
| 117 | + |
| 118 | +Access the UI at: `http://127.0.0.1:5173/` |
0 commit comments