我有两个云运行服务(Next.js和API server),我想通过一个端点为它们提供服务。我希望到/api
的请求被转发到API服务,所有其他请求(/*
)被转发到Next.js服务器。Cloud Run documentation建议我使用端点,但它似乎不支持通配符路径。可能的替代方案是什么?
发布于 2021-11-28 20:11:58
Google API Gateway支持不同的通配符,我使用Google函数作为后端,但当使用GW和Google Run时,这不应该有什么不同。
我的场景是:
/
应路由至/index.html
/assets
应路由至/assets/any-file-here.png
/logo-256.png
应路由至/logo-256.png
/endpoint-1
应仅路由至API,但托管在另一function/endpoint-2/some-param
上的应仅路由至托管在与assets相同功能上的API。
有了这种配置,get的所有路由都像想要的那样,使用Double Wildcard Matching。
通配符在特定路由之前并不重要,这是由网关正确处理的。
{
"swagger": "2.0",
"info": {
"version": "0.0.1",
"title": "Some API w/ Assets"
},
"paths": {
"/": {
"get": {
"summary": "home",
"operationId": "home",
"parameters": [],
"x-google-backend": {
"address": "https://THE-GOOGLE-RUN-OR-FUNCTION",
"path_translation": "CONSTANT_ADDRESS"
},
"responses": {
"200": {
"description": "Home"
}
}
}
},
"/{files=**}": {
"get": {
"summary": "assets",
"operationId": "assets",
"parameters": [
{
"in": "path",
"name": "files",
"type": "string",
"required": true
}
],
"x-google-backend": {
"address": "https://THE-GOOGLE-RUN-OR-FUNCTION",
"path_translation": "APPEND_PATH_TO_ADDRESS"
},
"responses": {
"200": {
"description": "assets"
}
}
}
},
"/endpoint-1": {
"get": {
"summary": "Some pure backend api",
"operationId": "ep1",
"x-google-backend": {
"address": "https://SOME-OTHER-GOOGLE-RUN-OR-FUNCTION",
"path_translation": "APPEND_PATH_TO_ADDRESS"
},
"parameters": [],
"responses": {
"200": {
"description": "result values"
}
}
}
},
"/endpoint-2/{some_param}": {
"get": {
"summary": "Some pure backend API with path param",
"operationId": "ep2",
"parameters": [
{
"in": "path",
"name": "some_param",
"type": "string",
"required": true
}
],
"x-google-backend": {
"address": "https://THE-GOOGLE-RUN-OR-FUNCTION",
"path_translation": "APPEND_PATH_TO_ADDRESS"
},
"responses": {
"200": {
"description": "result values"
}
}
}
}
}
}
但是使用这种设置,你的页面不会那么快,我建议你在提供文件时,在你的API网关之前添加带有Google CDN的Google Load Balancer。
https://stackoverflow.com/questions/59518397
复制相似问题