Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nginx static-files wasm plugin #1643

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions plugins/wasm-go/extensions/static-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 功能说明
`static-files`插件支持nginx静态文件的转发配置,static files插件配置支持指令:
- `root`: 定义root路径,转发请求为root path + request path。
- `alias`: 定义alias路径,转发请求为alias path。
- `index`: 默认首页文件列表,比如["index.html", "index.php"]等。
- `try paths`: 请求基于不同的路径进行重试,直到请求到正确返回的请求。

# 配置字段

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我倾向于只配置一条路由转发给oss服务,然后这个路由上配置这个插件,通过 location 数组,指定好每个兼容nginx location的静态文件转发规则

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
具体插件配置类似这种的对吧,可以的,原来我是想着一个服务转发一个插件配置的,你这样类似nginx的所有前端都在一个插件里面进行配置了。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的

| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- | -------- |
| `root` | string | 非必填 | - | root路径 |
| `alias_path` | string | 非必填 | - | alias待替换的路径 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alias path 用户不容易理解,还是用 location 来配置路径吧

| `alias` | string | 非必填 | - | alias路径 |
| `index` | array of string | 非必填 | - | 首页文件列表 |
| `try_paths` | array of string | 必填 | - | 转发路径列表,比如`index.html`,`$uri`, `index.html` |
| `try_codes` | array of int | 非必填 | [403, 404] | 重试状态码,可自定义 |
| `timeout` | int | 非必填 | 1000 | 重试请求的超时时间,单位ms |


# 配置示例

## root

```yaml
root: "/b"

```

基于该配置开启插件,触发插件的请求`http://a.com/a`, 请求会转发为`http://upstream/b/a`。

## alias

```yaml
alias_path: "/a"
alias: "/b"

```

基于该配置开启插件,触发插件的请求`http://a.com/a`, 请求会转发为`http://upstream/b`。

## index

```yaml
alias_path: "/a"
alias: "/b"
index:
- index.html

```

基于该配置开启插件,触发插件的请求`http://a.com/a`, 请求会依次转发为:
- http://upstream/b
- http://upstream/b/index.html


## try_paths

```yaml
try_paths:
- "$uri/"
- "$uri.html"
- "/index.html"

```

基于该配置开启插件,触发插件的请求`http://a.com/a`, 请求会依次转发为:
- http://upstream/a/
- http://upstream/a.html
- http://upstream/index.html
如果请求返回码不是重试状态码,会直接返回该请求体,否则继续转发下一个请求,所有请求都不是重试状态码,会继续转发到默认后端服务。
1 change: 1 addition & 0 deletions plugins/wasm-go/extensions/static-files/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
22 changes: 22 additions & 0 deletions plugins/wasm-go/extensions/static-files/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module static-files

go 1.19

require (
github.com/alibaba/higress/plugins/wasm-go v1.4.0
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 // indirect
github.com/magefile/mage v1.14.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/resp v0.1.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 28 additions & 0 deletions plugins/wasm-go/extensions/static-files/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/alibaba/higress/plugins/wasm-go v1.4.0 h1:uFf+mbZ2iuRXJzRbmWBuxiHvNDMGf3PCBJ6TI86bopY=
github.com/alibaba/higress/plugins/wasm-go v1.4.0/go.mod h1:10jQXKsYFUF7djs+Oy7t82f4dbie9pISfP9FJwpPLuk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA=
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew=
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc h1:t2AT8zb6N/59Y78lyRWedVoVWHNRSCBh0oWCC+bluTQ=
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc/go.mod h1:hNFjhrLUIq+kJ9bOcs8QtiplSQ61GZXtd2xHKx4BYRo=
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/resp v0.1.1 h1:Ly20wkhqKTmDUPlyM1S7pWo5kk0tDu8OoC/vFArXmwE=
github.com/tidwall/resp v0.1.1/go.mod h1:3/FrruOBAxPTPtundW0VXgmsQ4ZBA0Aw714lVYgwFa0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
122 changes: 122 additions & 0 deletions plugins/wasm-go/extensions/static-files/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"net/http"
"strings"

"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
)

// 默认超时时间为1s
var defaultTimeout uint32 = 1000

const VariableReplacedStr = "$uri"

func main() {
wrapper.SetCtx(
"static-files",
wrapper.ParseConfigBy(parseConfig),
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
)
}

type StaticFilesConfig struct {
root string
alias string
aliasPath string
index []string
tryPaths []string
timeout uint32
tryCodes []int
client wrapper.HttpClient
}

func parseConfig(json gjson.Result, config *StaticFilesConfig, log wrapper.Log) error {
config.root = json.Get("root").String()
config.alias = json.Get("alias").String()
config.aliasPath = json.Get("alias_path").String()
for _, result := range json.Get("index").Array() {
config.index = append(config.index, result.String())
}

for _, result := range json.Get("try_paths").Array() {
config.tryPaths = append(config.tryPaths, result.String())
}

if json.Get("try_codes").String() == "" {
// tryCodes默认值为["404", "403"]
config.tryCodes = []int{http.StatusNotFound, http.StatusForbidden}
} else {
for _, result := range json.Get("try_codes").Array() {
config.tryCodes = append(config.tryCodes, int(result.Int()))
}
}

timeout := json.Get("timeout").Int()
if timeout == 0 {
// tryPaths的timeout默认值为1s
config.timeout = defaultTimeout
} else {
config.timeout = uint32(timeout)
}

config.client = wrapper.NewClusterClient(wrapper.RouteCluster{
Host: json.Get("host").String(),
})
return nil
}

func tryHttpCall(tryPaths []string, config StaticFilesConfig, index int, path string, log wrapper.Log) {
if index >= len(tryPaths) {
proxywasm.ResumeHttpRequest()
return
}

requestPath := strings.Replace(tryPaths[index], VariableReplacedStr, path, -1)
log.Debugf("try path request, path: %s", requestPath)
err := config.client.Get(requestPath, nil,
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
if !contains(config.tryCodes, statusCode) {
proxywasm.SendHttpResponse(uint32(statusCode), convertHttpHeadersToStruct(responseHeaders), responseBody, -1)
return
}
tryHttpCall(tryPaths, config, index+1, path, log)
}, config.timeout)

if err != nil {
log.Errorf("try path request failed, path %s, error: %s", requestPath, err.Error())
tryHttpCall(tryPaths, config, index+1, path, log)
}
}

func onHttpRequestHeaders(ctx wrapper.HttpContext, config StaticFilesConfig, log wrapper.Log) types.Action {
log.Debugf("static files request config: %+v", config)
path := ctx.Path()
tryPaths := make([]string, 0)
requestPath := path
if config.root != "" {
requestPath = getRootRequestPath(config.root, path)
tryPaths = append(tryPaths, requestPath)
} else if config.aliasPath != "" {
requestPath = getAliasRequestPath(config.alias, config.aliasPath, path)
tryPaths = append(tryPaths, requestPath)
}

if len(config.index) != 0 {
tryPaths = append(tryPaths, *getIndexRequestPath(config.index, requestPath)...)
}

if len(config.tryPaths) != 0 {
tryPaths = append(tryPaths, config.tryPaths...)
}

if len(tryPaths) == 0 {
return types.ActionContinue
}

tryHttpCall(tryPaths, config, 0, path, log)
return types.ActionPause
}
46 changes: 46 additions & 0 deletions plugins/wasm-go/extensions/static-files/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"net/http"
"strings"
)

func convertHttpHeadersToStruct(responseHeaders http.Header) [][2]string {
headerStruct := make([][2]string, len(responseHeaders))
i := 0
for key, values := range responseHeaders {
headerStruct[i][0] = key
headerStruct[i][1] = values[0]
i++
}
return headerStruct
}

func getRootRequestPath(root string, path string) string {
return root + path
}

func getAliasRequestPath(alias string, aliasPath string, path string) string {
return strings.Replace(path, aliasPath, alias, -1)
}

func getIndexRequestPath(index []string, path string) *[]string {
paths := make([]string, 0)
for _, v := range index {
if strings.HasSuffix(path, "/") {
paths = append(paths, path+v)
} else {
paths = append(paths, path+"/"+v)
}
}
return &paths
}

func contains(array []int, value int) bool {
for _, v := range array {
if v == value {
return true
}
}
return false
}
Loading
Loading