Skip to content

Commit

Permalink
feat: optional lark ignore tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
wintbiit committed Dec 11, 2023
1 parent 5fae5f7 commit 3cd6b92
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 27 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -125,13 +137,24 @@ 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
with:
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
Expand Down
4 changes: 3 additions & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
]
}]
]
Expand Down
28 changes: 23 additions & 5 deletions provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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
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"
```
6 changes: 5 additions & 1 deletion provider/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
6 changes: 5 additions & 1 deletion provider/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
8 changes: 7 additions & 1 deletion provider/lark.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nolark

package provider

import (
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion provider/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
20 changes: 7 additions & 13 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion provider/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
46 changes: 43 additions & 3 deletions utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
23 changes: 23 additions & 0 deletions utils/json_sonic.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 3cd6b92

Please sign in to comment.