Skip to content

Commit f0cb3e0

Browse files
docs(srv): update handler reference MTA-5521 (#4342)
* docs(srv): update * docs(srv): update * docs(srv): update * docs(srv): update * docs(srv): update * docs(srv): update
1 parent 5c4b196 commit f0cb3e0

File tree

3 files changed

+127
-21
lines changed

3 files changed

+127
-21
lines changed

menu/navigation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3719,7 +3719,7 @@
37193719
"slug": "test-a-function"
37203720
},
37213721
{
3722-
"label": "Package function dependencies in a zip file",
3722+
"label": "Package function in a zip file",
37233723
"slug": "package-function-dependencies-in-zip"
37243724
},
37253725
{

pages/serverless-functions/how-to/package-function-dependencies-in-zip.mdx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
meta:
3-
title: How to package your function and dependencies to a zip file and upload it
3+
title: How to package your function to a zip file and upload it
44
description: Package function dependencies in a ZIP file for Scaleway Serverless Functions.
55
content:
6-
h1: How to package your function and dependencies to a zip file and upload it
6+
h1: How to package your function to a zip file and upload it
77
paragraph: Package function dependencies in a ZIP file for Scaleway Serverless Functions.
8-
tags: functions zip-file
8+
tags: functions zip-file handler package
99
dates:
10-
validation: 2024-10-23
10+
validation: 2025-02-04
1111
posted: 2021-05-26
1212
categories:
1313
- serverless
@@ -30,6 +30,10 @@ There are [different methods to deploy functions](/serverless-functions/referenc
3030

3131
To match the Scaleway build pipelines requirements, functions zip files must contain the content of the root folder you want to publish.
3232

33+
<Message type="important">
34+
Avoid compressing your function using the file explorer or finder, as this method can create an additional folder in the file structure of your function, which can lead to build errors. Refer to the [troubleshooting](#troubleshooting) section for more information.
35+
</Message>
36+
3337
### How to create a zip file
3438

3539
Use the `zip` command to create an archive of your function and its dependencies:
@@ -369,3 +373,19 @@ Like that, you can create 3 functions with the same zip file simply by changing
369373

370374
You can also create a single Serverless Function with several handlers in the same zip file, and change the [handler](/serverless-functions/concepts/#handler) parameter according to your needs.
371375

376+
## Troubleshooting
377+
378+
If you encounter build errors after packaging your function, make sure that your archive is properly structured, as shown above. Compressing a folder from its parent directory will include this folder in the archive structure that can lead to build errors while deploying your function.
379+
380+
To avoid archive structure issue, make sure to zip your function from its root folder, and not the parent directory by using the following method:
381+
382+
```bash
383+
cd folder_i_want_to_zip
384+
zip -r ../function.zip .
385+
```
386+
387+
You can then use the `unzip` command with the `-v` option to list the content of your archive to ensure it is properly structured:
388+
389+
```bash
390+
unzip -v ../func.zip
391+
```

pages/serverless-functions/reference-content/functions-handlers.mdx

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ categories:
1414
- functions
1515
---
1616

17+
A handler is a routine/function/method that processes specific events. Upon invoking your function, the handler is executed and returns an output.
18+
19+
To learn how to package a function and its dependencies in a zip file, refer to the [dedicated documentation](/serverless-functions/how-to/package-function-dependencies-in-zip).
20+
1721
## Handler syntax
1822

1923
<Tabs id="handlers">
@@ -27,6 +31,17 @@ categories:
2731

2832
- If the `handle` function is defined in a file named `handler.py` within a folder (for example, `myFunction/handler.py`): the handler will be `myFunction/handler.handle`.
2933

34+
#### Python handler example
35+
36+
```py
37+
def handle(event, context):
38+
return {
39+
"body": {
40+
"message": 'Hello, world',
41+
},
42+
"statusCode": 200,
43+
}
44+
```
3045
</TabsTab>
3146

3247
<TabsTab label="Node">
@@ -39,6 +54,18 @@ categories:
3954

4055
- If the `handle` function is defined in a file named `handler.js` within a folder (for example, `myFunction/handler.js`): the handler will be `myFunction/handler.handle`.
4156

57+
#### Node handler example
58+
59+
```js
60+
export {handle};
61+
62+
function handle (event, context, cb) {
63+
return {
64+
body: process.version,
65+
statusCode: 200,
66+
};
67+
};
68+
```
4269
</TabsTab>
4370

4471
<TabsTab label="PHP">
@@ -51,33 +78,92 @@ categories:
5178

5279
- If the `handle` function is defined in a file named `handler.php` within a folder (for example, `myFunction/handler.php`): the handler will be `myFunction/handler.handle`.
5380

81+
#### PHP handler example
82+
83+
```php
84+
<?php
85+
function handle($event, $context) {
86+
return [
87+
"body" => phpversion(),
88+
"statusCode" => 200,
89+
];
90+
}
91+
```
5492
</TabsTab>
5593

5694
<TabsTab label="Go">
5795

58-
For Go, the handler must be written in the `<folder>/<function_name>` format:
59-
60-
- If the `Handle` function is defined in a `handler.go` file in a root folder: the handler name will be `Handle`.
61-
62-
- If the `Handle` function is defined in a `handler.go` file (for example, `myFunction/handler.go`): the handler will be `myFunction/Handle`
63-
64-
<Message type="note">
65-
With Go, the method name must be capitalized.
66-
</Message>
67-
96+
For Go, the handler must be written in the `<folder>/<function_name>` format:
97+
98+
- If the `Handle` function is defined in a `handler.go` file in a root folder: the handler name will be `Handle`.
99+
100+
- If the `Handle` function is defined in a `handler.go` file (for example, `myFunction/handler.go`): the handler will be `myFunction/Handle`
101+
102+
<Message type="note">
103+
With Go, the method name must be capitalized.
104+
</Message>
105+
106+
#### Go handler example
107+
108+
```go
109+
package myfunc
110+
111+
import (
112+
"encoding/json"
113+
"net/http"
114+
)
115+
116+
// Handle - Handle event
117+
func Handle(w http.ResponseWriter, r *http.Request) {
118+
response := map[string]interface{}{
119+
"message": "We're all good",
120+
}
121+
122+
responseBytes, err := json.Marshal(response)
123+
if err != nil {
124+
w.WriteHeader(http.StatusInternalServerError)
125+
return
126+
}
127+
128+
// Set the header explicitly depending the returned data
129+
w.Header().Set("Content-Type", "application/json")
130+
131+
// Customise status code.
132+
w.WriteHeader(http.StatusOK)
133+
134+
// Add content to the response
135+
_, _ = w.Write(responseBytes)
136+
}
137+
```
68138
</TabsTab>
69139

70140
<TabsTab label="Rust">
71141

72-
For Rust, the handler must be written in the `<folder>/<function_name>` format:
142+
For Rust, the handler must be written in the `<folder>/<function_name>` format:
143+
144+
- If the `handle` function is defined in a `handler.rs` file in a root folder: the handler name will be `handle`.
73145

74-
- If the `handle` function is defined in a `handler.rs` file in a root folder: the handler name will be `handle`.
146+
- If the `handle` function is defined in a `handler.rs` file (for example, `myFunction/handler.rs`): the handler will be `myFunction/handle`
75147

76-
- If the `handle` function is defined in a `handler.rs` file (for example, `myFunction/handler.rs`): the handler will be `myFunction/handle`
148+
<Message type="note">
149+
With Rust, the method must be exported using the `pub` keyword.
150+
</Message>
77151

78-
<Message type="note">
79-
With Rust, the method must be exported using the `pub` keyword.
80-
</Message>
152+
#### Rust handler example
81153

154+
```rust
155+
use axum::{
156+
body::Body, extract::Request, response::Response,
157+
};
158+
use http::StatusCode;
159+
160+
pub async fn handle(_req: Request<Body>) -> Response<Body> {
161+
Response::builder()
162+
.status(StatusCode::OK)
163+
.header("Content-Type", "text/plain")
164+
.body(Body::from("Hello world!"))
165+
.unwrap()
166+
}
167+
```
82168
</TabsTab>
83169
</Tabs>

0 commit comments

Comments
 (0)