Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion docs/languages/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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/).
To see the full range of configuration options, see [Module Configuration](https://opentelemetry.io/docs/zero-code/js/configuration/).
68 changes: 67 additions & 1 deletion docs/languages/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<?php

namespace App;

/**
* Class Handler
* @package App
*/
class Handler
{
/**
* @param string $data
* @return string
*/
public function handle(string $data): string
{
// Check if Http_Path environment variable equals '/static'
$httpPath = $_ENV['Http_Path'] ?? '';
if ($httpPath =='/static') {
try {
// Get the directory where this file is located and go up one level
$dataFilePath = dirname(__DIR__) . '/data.json';

// Check if the file exists
if (!file_exists($dataFilePath)) {
return json_encode([
'error' => '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;
}
}
}
```
33 changes: 33 additions & 0 deletions docs/languages/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down