Lambda 入门
本指南介绍如何创建 JavaScript lambda、列出它,并通过管理路由和公开路由调用它。
前提条件
- 一个带有
X-Glue-Authenticationtoken 的 HolaCloud 账户。 - 本机已安装 curl。
步骤 1:创建 Lambda
创建一个简单的 hello-world lambda:
curl -X POST "https://api.hola.cloud/api/v0/lambdas" \
-H "X-Glue-Authentication: YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "hello-world",
"language": "javascript",
"method": "GET",
"path": "/hello-world",
"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })"
}'POST /api/v0/lambdas HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
Content-Type: application/json
{
"name": "hello-world",
"language": "javascript",
"method": "GET",
"path": "/hello-world",
"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })"
}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/api/v0/lambdas", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("X-Glue-Authentication", "YOUR_TOKEN")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['code' => 'export default (req) => ({ body: { message: "Hello, World!", method: req.method, path: req.path } })', 'language' => 'javascript', 'method' => 'GET', 'name' => 'hello-world', 'path' => '/hello-world'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/api/v0/lambdas',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'X-Glue-Authentication: YOUR_TOKEN',
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json",
}
payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/api/v0/lambdas",
headers=headers,
data=body
)
print(response.text)
const payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"};
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
method: "POST",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"};
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
method: "POST",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("code", "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language", "javascript", "method", "GET", "name", "hello-world", "path", "/hello-world");
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/api/v0/lambdas"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("X-Glue-Authentication", "YOUR_TOKEN")
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}预期响应:
1{
2 "id": "f3b2c1a0-1234-5678-9abc-def012345678",
3 "created_timestamp": 1750507200,
4 "owner": "user_123",
5 "project_id": "project_456",
6 "name": "hello-world",
7 "language": "javascript",
8 "code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })",
9 "method": "GET",
10 "path": "/hello-world"
11}
保存返回的 id;它就是运行、获取、更新和删除端点使用的 lambda_id。
步骤 2:列出 Lambdas
确认 lambda 已创建:
curl "https://api.hola.cloud/api/v0/lambdas" \
-H "X-Glue-Authentication: YOUR_TOKEN"GET /api/v0/lambdas HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/api/v0/lambdas", nil)
if err != nil {
panic(err)
}
req.Header.Set("X-Glue-Authentication", "YOUR_TOKEN")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/api/v0/lambdas',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'X-Glue-Authentication: YOUR_TOKEN',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
headers = {
"X-Glue-Authentication": "YOUR_TOKEN",
}
response = requests.request(
"GET",
"https://api.hola.cloud/api/v0/lambdas",
headers=headers
)
print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/api/v0/lambdas"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.header("X-Glue-Authentication", "YOUR_TOKEN")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}步骤 3:运行 Lambda
通过经过认证的管理路由调用它:
curl -X GET "https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID" \
-H "X-Glue-Authentication: YOUR_TOKEN"GET /api/v0/run/YOUR_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", nil)
if err != nil {
panic(err)
}
req.Header.Set("X-Glue-Authentication", "YOUR_TOKEN")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'X-Glue-Authentication: YOUR_TOKEN',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
headers = {
"X-Glue-Authentication": "YOUR_TOKEN",
}
response = requests.request(
"GET",
"https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID",
headers=headers
)
print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.header("X-Glue-Authentication", "YOUR_TOKEN")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}预期响应:
1{
2 "body": {
3 "message": "Hello, World!",
4 "method": "GET",
5 "path": "/hello-world"
6 }
7}
也可以通过公开运行路由调用 lambda:
curl -X GET "https://api.hola.cloud/run/YOUR_LAMBDA_ID"GET /run/YOUR_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/run/YOUR_LAMBDA_ID", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/run/YOUR_LAMBDA_ID',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
response = requests.request(
"GET",
"https://api.hola.cloud/run/YOUR_LAMBDA_ID"
)
print(response.text)
const response = await fetch("https://api.hola.cloud/run/YOUR_LAMBDA_ID", {
method: "GET"
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/run/YOUR_LAMBDA_ID", {
method: "GET"
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/run/YOUR_LAMBDA_ID"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}步骤 4:查看进行中的调用
列出当前正在运行的调用:
curl "https://api.hola.cloud/ongoing"GET /ongoing HTTP/1.1
Host: api.hola.cloud
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/ongoing", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/ongoing',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
response = requests.request(
"GET",
"https://api.hola.cloud/ongoing"
)
print(response.text)
const response = await fetch("https://api.hola.cloud/ongoing", {
method: "GET"
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/ongoing", {
method: "GET"
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/ongoing"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}下一步
- 在 管理 Lambda 函数 中学习如何更新代码和路由字段。
- 在 调用 Lambda 函数 中了解公开运行路由、mux 路由、
/me和/openapi。
评论