diff --git a/docs/languages/node.md b/docs/languages/node.md index d36a40a8..2d30fcb0 100644 --- a/docs/languages/node.md +++ b/docs/languages/node.md @@ -282,6 +282,35 @@ This is useful when the function is expected to receive large amounts of JSON da MAX_JSON_SIZE: '5mb' ``` +### Add static files to your function + +A common use-case for static files is when you want to serve HTML, lookup information from a JSON manifest or render some kind of templates. + +With the Node templates, static files and folders can just be added to the handler directory and will be copied into the function image. + +To read a file e.g `data.json` back at runtime you can do the following: + +```js +const fs = require("fs").promises; +const path = require("path"); + +module.exports = async (event, context) => { + if (event.path === "/static") { + // Get the path to data.json in the same directory as this handler + const dataFilePath = path.join(__dirname, "data.json"); + + // Read the data.json file + const fileContent = await fs.readFile(dataFilePath, "utf8"); + + return context.status(200).succeed(fileContent); + } else { + return context + .status(200) + .succeed("Hello from OpenFaaS!"); + } +}; +``` + ## OpenTelemetry zero-code instrumentation Using [OpenTelemetry zero-code instrumentation](https://opentelemetry.io/docs/zero-code/js/) for Node.js functions requires some minor modifications to the existing Node.js templates. @@ -323,4 +352,4 @@ functions: - `OTEL_EXPORTER_OTLP_ENDPOINT` sets the endpoint where telemetry is exported to. - The `NODE_OPTIONS` environment variable needs to have the value `--require @opentelemetry/auto-instrumentations-node/register` to register and initialize the auto instrumentation module. -To see the full range of configuration options, see [Module Configuration](https://opentelemetry.io/docs/zero-code/js/configuration/). \ No newline at end of file +To see the full range of configuration options, see [Module Configuration](https://opentelemetry.io/docs/zero-code/js/configuration/). diff --git a/docs/languages/php.md b/docs/languages/php.md index d3ba1662..d1c7a0b9 100644 --- a/docs/languages/php.md +++ b/docs/languages/php.md @@ -19,7 +19,7 @@ faas-cli new --lang php8 \ This will create several new files: -* my-php.yml - the function's YAML file +* stack.yaml - the function's YAML file * my-php/src/Handler.php - the function's handler for reading the input and writing the output * my-php/composer.json - for managing dependencies * my-php/php-extension.sh - for installing PHP extensions @@ -109,3 +109,69 @@ set -xe && \ ``` You can also refer to the PHP Docker image [documentation](https://github.com/docker-library/docs/blob/master/php/README.md#how-to-install-more-php-extensions) for additional instructions on the installation and configuration of extensions + +### Add static files to your function + +A common use-case for static files is when you want to serve HTML, lookup information from a JSON manifest or render some kind of templates. + +With the php8 template, static files and folders can just be added to the handler src directory and will be copied into the function image. + +To read a file e.g `data.json` back at runtime you can do the following: + +```php + 'data.json file not found', + 'statusCode' => 404 + ]); + } + + // Read the data.json file + $fileContent = file_get_contents($dataFilePath); + + if ($fileContent === false) { + return json_encode([ + 'error' => 'Failed to read data.json file', + 'statusCode' => 500 + ]); + } + + // Return the JSON data + return $fileContent; + + } catch (Exception $e) { + return json_encode([ + 'error' => 'An error occurred: ' . $e->getMessage(), + 'statusCode' => 500 + ]); + } + } else { + return $data; + } + } +} +``` diff --git a/docs/languages/python.md b/docs/languages/python.md index cad88786..6f746d0c 100644 --- a/docs/languages/python.md +++ b/docs/languages/python.md @@ -389,6 +389,39 @@ faas-cli publish --filter fn1\ --build-arg UPGRADE_PACKAGES=true ``` +### Add static files to your function + +A common use-case for static files is when you want to serve HTML, lookup information from a JSON manifest or render some kind of templates. + +With the python templates, static files and folders can just be added to the handler directory and will be copied into the function image. + +To read a file e.g `data.json` back at runtime you can do the following: + +```python +def handle(event, context): + if event.path=="/static": + # Get the directory where this handler.py file is located + current_dir = os.path.dirname(os.path.abspath(__file__)) + data_file_path = os.path.join(current_dir, 'data.json') + + # Read the data.json file + with open(data_file_path, 'r') as file: + data = json.load(file) + + return { + "statusCode": 200, + "body": json.dumps(data), + "headers": { + "Content-Type": "application/json" + } + } + else: + return { + "statusCode": 200, + "body": "Hello from OpenFaaS!" + } +``` + ## OpenTelemetry zero-code instrumentation Using [OpenTelemetry zero-code instrumentation](https://opentelemetry.io/docs/zero-code/python/) for python functions requires some minor modifications to the existing Python templates.