From 3cd6b92ef0c092b22424ab5fe4d6287ba8428041 Mon Sep 17 00:00:00 2001 From: WintBit Date: Tue, 12 Dec 2023 03:24:43 +0800 Subject: [PATCH] feat: optional lark ignore tag support --- .github/workflows/build.yml | 23 +++++++++++++++++++ .releaserc.json | 4 +++- provider/README.md | 28 ++++++++++++++++++---- provider/dir.go | 6 ++++- provider/file.go | 6 ++++- provider/lark.go | 8 ++++++- provider/mysql.go | 6 ++++- provider/provider.go | 20 ++++++---------- provider/sqlite.go | 6 ++++- utils/json.go | 46 ++++++++++++++++++++++++++++++++++--- utils/json_sonic.go | 23 +++++++++++++++++++ 11 files changed, 149 insertions(+), 27 deletions(-) create mode 100644 utils/json_sonic.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ea484f7..68e9434 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -73,12 +73,18 @@ jobs: - name: Build run: | go build -trimpath -ldflags "-s -w" -o ./bin/ninedns . + go build -trimpath -ldflags "-s -w" -tags "nolark" -o ./bin/ninedns-nolark . working-directory: ./ - name: Artifact uses: actions/upload-artifact@v3 with: name: ninedns-amd64 path: ./bin/ninedns + - name: Artifact + uses: actions/upload-artifact@v3 + with: + name: ninedns-nolark-amd64 + path: ./bin/ninedns-nolark backend-windows: name: Backend-Windows @@ -94,12 +100,18 @@ jobs: - name: Build run: | go build -trimpath -ldflags "-s -w" -o ./bin/ninedns.exe . + go build -trimpath -ldflags "-s -w" -tags "nolark" -o ./bin/ninedns-nolark.exe . working-directory: ./ - name: Artifact uses: actions/upload-artifact@v3 with: name: ninedns-windows path: ./bin/ninedns.exe + - name: Artifact + uses: actions/upload-artifact@v3 + with: + name: ninedns-nolark-windows + path: ./bin/ninedns-nolark.exe release: name: Release @@ -125,6 +137,11 @@ jobs: with: name: ninedns-amd64 path: ./dist + - name: Donload Artifact Linux + uses: actions/download-artifact@v3 + with: + name: ninedns-nolark-amd64 + path: ./dist - name: Donload Artifact Windows uses: actions/download-artifact@v3 @@ -132,6 +149,12 @@ jobs: name: ninedns-windows path: ./dist + - name: Donload Artifact Windows + uses: actions/download-artifact@v3 + with: + name: ninedns-nolark-windows + path: ./dist + - name: ls run: | ls -l ./dist diff --git a/.releaserc.json b/.releaserc.json index b20be42..92052b6 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -9,7 +9,9 @@ ["@semantic-release/github", { "assets": [ {"path": "dist/ninedns", "name": "ninedns-linux-amd64-${nextRelease.version}"}, - {"path": "dist/ninedns.exe", "name": "ninedns-windows-amd64-${nextRelease.version}.exe"} + {"path": "dist/ninedns.exe", "name": "ninedns-windows-amd64-${nextRelease.version}.exe"}, + {"path": "dist/ninedns-nolark", "name": "ninedns-nolark-linux-amd64-${nextRelease.version}"}, + {"path": "dist/ninedns-nolark.exe", "name": "ninedns-nolark-windows-amd64-${nextRelease.version}.exe"} ] }] ] diff --git a/provider/README.md b/provider/README.md index 217e4a4..caf9fe9 100644 --- a/provider/README.md +++ b/provider/README.md @@ -9,7 +9,7 @@ config: } } ``` -Mysql Provider connects to a mysql database and reads records from it. Table name is rule set name. +`Mysql Provider` connects to a mysql database and reads records from it. Table name is rule set name. SQL is re-read according to ttl Table schema: @@ -39,7 +39,7 @@ config: } } ``` -Like Mysql Provider, SQLite Provider connects to a sqlite database and reads records from it. Table name is rule set name. +Like `Mysql Provider`, `SQLite Provider` connects to a sqlite database and reads records from it. Table name is rule set name. SQL is re-read according to ttl > Using `github.com/glebarez/sqlite` as sqlite driver, please refer to this repo for more usage. @@ -53,7 +53,7 @@ config: } } ``` -File Provider reads records from a file. File format is standard zone file format. +`File Provider` reads records from a file. File format is standard zone file format. File Provider does not support ruleset variant, if you want to use variant, please use [Dir Provider](#dir-provider). File will be re-read according to ttl @@ -66,6 +66,24 @@ config: } } ``` -Dir Provider reads records from a directory. File format is standard zone file format. +`Dir Provider` reads records from a directory. File format is standard zone file format. ruleset name is used as file name. -files in the directory will be re-read according to ttl \ No newline at end of file +files in the directory will be re-read according to ttl + +### Lark Provider +config: +```json +{ + "provider": { + "lark": "cli_xxx xxx xxx" + } +} +``` +`Lark Provider` reads records from lark bitable. Also ruleset name is used as table name. + +#### Minimize binary size + Please note, lark provider introduced [oapi-lark-go](https://github.com/larksuite/oapi-sdk-go) and [sonic](https://github.com/bytedance/sonic), + which largely increases binary size. If you don't need lark provider, please disable it while build: + ```bash +go build -tags "nolark" +``` \ No newline at end of file diff --git a/provider/dir.go b/provider/dir.go index 924f1e7..bbf7eed 100644 --- a/provider/dir.go +++ b/provider/dir.go @@ -9,7 +9,11 @@ type DirProvider struct { dir string } -func newDirProvider(dir string) (*DirProvider, error) { +func init() { + constructors["dir"] = newDirProvider +} + +func newDirProvider(dir string) (Provider, error) { provider := &DirProvider{ dir: dir, } diff --git a/provider/file.go b/provider/file.go index 0b26278..ce97143 100644 --- a/provider/file.go +++ b/provider/file.go @@ -17,7 +17,11 @@ type FileProvider struct { file string } -func newFileProvider(file string) (*FileProvider, error) { +func init() { + constructors["file"] = newFileProvider +} + +func newFileProvider(file string) (Provider, error) { provider := &FileProvider{ file: file, } diff --git a/provider/lark.go b/provider/lark.go index c652e7b..beb38d6 100644 --- a/provider/lark.go +++ b/provider/lark.go @@ -1,3 +1,5 @@ +//go:build !nolark + package provider import ( @@ -17,7 +19,11 @@ type LarkProvider struct { baseId string } -func newLarkProvider(config string) (*LarkProvider, error) { +func init() { + constructors["lark"] = newLarkProvider +} + +func newLarkProvider(config string) (Provider, error) { sp := strings.Split(config, " ") if len(sp) != 3 { return nil, fmt.Errorf("invalid lark config: %s you should use `${appId} ${appSecret} ${baseId}` format", config) diff --git a/provider/mysql.go b/provider/mysql.go index 12f6565..a9ee38a 100644 --- a/provider/mysql.go +++ b/provider/mysql.go @@ -12,7 +12,11 @@ type MysqlProvider struct { dsn string } -func newMysqlProvider(dsn string) (*MysqlProvider, error) { +func init() { + constructors["mysql"] = newMysqlProvider +} + +func newMysqlProvider(dsn string) (Provider, error) { provider := &MysqlProvider{ dsn: dsn, } diff --git a/provider/provider.go b/provider/provider.go index d2f1d78..7f5a2c9 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -6,24 +6,18 @@ import ( "github.com/wintbiit/ninedns/model" ) +var constructors = make(map[string]func(string) (Provider, error)) + type Provider interface { Provide(ruleset string) ([]model.Record, error) AutoMigrate(table string) error } func NewProvider(name, config string) (Provider, error) { - switch name { - case "mysql": - return newMysqlProvider(config) - case "sqlite": - return newSQLiteProvider(config) - case "file": - return newFileProvider(config) - case "dir": - return newDirProvider(config) - case "lark": - return newLarkProvider(config) - default: - return nil, fmt.Errorf("unknown provider: %s", name) + constructor, ok := constructors[name] + if !ok { + return nil, fmt.Errorf("provider %s not found", name) } + + return constructor(config) } diff --git a/provider/sqlite.go b/provider/sqlite.go index 387a4bf..6781a91 100644 --- a/provider/sqlite.go +++ b/provider/sqlite.go @@ -12,7 +12,11 @@ type SQLiteProvider struct { file string } -func newSQLiteProvider(file string) (*SQLiteProvider, error) { +func init() { + constructors["sqlite"] = newSQLiteProvider +} + +func newSQLiteProvider(file string) (Provider, error) { provider := &SQLiteProvider{ file: file, } diff --git a/utils/json.go b/utils/json.go index d01e01e..492fc5e 100644 --- a/utils/json.go +++ b/utils/json.go @@ -3,10 +3,50 @@ package utils import ( "io" - "github.com/bytedance/sonic" + jsonstd "encoding/json" ) -var json = sonic.ConfigFastest +type API interface { + MarshalToString(v interface{}) (string, error) + Marshal(v interface{}) ([]byte, error) + MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) + UnmarshalFromString(str string, v interface{}) error + Unmarshal(data []byte, v interface{}) error + UnmarshalFromReader(reader io.Reader, v interface{}) error +} + +type JsonStd struct{} + +var json API = JsonStd{} + +func (JsonStd) MarshalToString(v interface{}) (string, error) { + bytes, err := jsonstd.Marshal(v) + if err != nil { + return "", err + } + + return string(bytes), nil +} + +func (JsonStd) Marshal(v interface{}) ([]byte, error) { + return jsonstd.Marshal(v) +} + +func (JsonStd) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { + return jsonstd.MarshalIndent(v, prefix, indent) +} + +func (JsonStd) UnmarshalFromString(str string, v interface{}) error { + return jsonstd.Unmarshal([]byte(str), v) +} + +func (JsonStd) Unmarshal(data []byte, v interface{}) error { + return jsonstd.Unmarshal(data, v) +} + +func (JsonStd) UnmarshalFromReader(reader io.Reader, v interface{}) error { + return jsonstd.NewDecoder(reader).Decode(v) +} func MarshalToString(v interface{}) (string, error) { return json.MarshalToString(v) @@ -29,5 +69,5 @@ func Unmarshal(data []byte, v interface{}) error { } func UnmarshalFromReader(reader io.Reader, v interface{}) error { - return json.NewDecoder(reader).Decode(v) + return json.UnmarshalFromReader(reader, v) } diff --git a/utils/json_sonic.go b/utils/json_sonic.go new file mode 100644 index 0000000..03d2fd3 --- /dev/null +++ b/utils/json_sonic.go @@ -0,0 +1,23 @@ +//go:build !nolark + +package utils + +import ( + "io" + + "github.com/bytedance/sonic" +) + +type JsonSonic struct { + sonic.API +} + +func (j JsonSonic) UnmarshalFromReader(reader io.Reader, v interface{}) error { + return j.NewDecoder(reader).Decode(v) +} + +func init() { + json = JsonSonic{ + API: sonic.ConfigFastest, + } +}