From c71f298a9daffffdd8f2c6228c52c3a54b566cb9 Mon Sep 17 00:00:00 2001 From: WintBit Date: Tue, 12 Dec 2023 03:49:07 +0800 Subject: [PATCH] feat: provider all build tags --- .github/workflows/build.yml | 35 ++++++------------------ .releaserc.json | 10 ++++--- README.md | 12 ++++++++- go.mod | 10 ++++++- go.sum | 16 +++++++++++ provider/README.md | 36 ++++++++++++++++++++++--- provider/postgres.go | 53 +++++++++++++++++++++++++++++++++++++ provider/sqlite.go | 2 ++ 8 files changed, 138 insertions(+), 36 deletions(-) create mode 100644 provider/postgres.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 51f6e50..a5bdefa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -72,19 +72,15 @@ jobs: - run: go version - name: Build run: | - go build -trimpath -ldflags "-s -w" -o ./bin/ninedns . - go build -trimpath -ldflags "-s -w" -tags "lark" -o ./bin/ninedns-full . + go build -trimpath -ldflags "-s -w" -o ./bin/ninedns-mini . + go build -trimpath -ldflags "-s -w" -tags "mysql" -o ./bin/ninedns . + go build -trimpath -ldflags "-s -w" -tags "mysql lark sqlite postgres" -o ./bin/ninedns-full . 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-full-amd64 - path: ./bin/ninedns-full + path: ./bin backend-windows: name: Backend-Windows @@ -99,19 +95,15 @@ jobs: - run: go version - name: Build run: | - go build -trimpath -ldflags "-s -w" -o ./bin/ninedns.exe . - go build -trimpath -ldflags "-s -w" -tags "lark" -o ./bin/ninedns-full.exe . + go build -trimpath -ldflags "-s -w" -o ./bin/ninedns-mini.exe . + go build -trimpath -ldflags "-s -w" -tags "mysql" -o ./bin/ninedns.exe . + go build -trimpath -ldflags "-s -w" -tags "mysql lark sqlite postgres" -o ./bin/ninedns-full.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-full-windows - path: ./bin/ninedns-full.exe + path: ./bin release: name: Release @@ -137,11 +129,6 @@ jobs: with: name: ninedns-amd64 path: ./dist - - name: Download Artifact Linux - uses: actions/download-artifact@v3 - with: - name: ninedns-full-amd64 - path: ./dist - name: Download Artifact Windows uses: actions/download-artifact@v3 @@ -149,12 +136,6 @@ jobs: name: ninedns-windows path: ./dist - - name: Download Artifact Windows - uses: actions/download-artifact@v3 - with: - name: ninedns-full-windows - path: ./dist - - name: ls run: | ls -l ./dist diff --git a/.releaserc.json b/.releaserc.json index 9ded638..f75978d 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -8,10 +8,12 @@ "@semantic-release/release-notes-generator", ["@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-full", "name": "ninedns-full-linux-amd64-${nextRelease.version}"}, - {"path": "dist/ninedns-full.exe", "name": "ninedns-full-windows-amd64-${nextRelease.version}.exe"} + {"path": "dist/bin/ninedns-mini", "name": "ninedns-mini-linux-amd64-${nextRelease.version}"}, + {"path": "dist/bin/ninedns", "name": "ninedns-linux-amd64-${nextRelease.version}"}, + {"path": "dist/bin/ninedns-full", "name": "ninedns-full-linux-amd64-${nextRelease.version}"}, + {"path": "dist/bin/ninedns-mini.exe", "name": "ninedns-mini-windows-amd64-${nextRelease.version}.exe"}, + {"path": "dist/bin/ninedns.exe", "name": "ninedns-windows-amd64-${nextRelease.version}.exe"}, + {"path": "dist/bin/ninedns-full.exe", "name": "ninedns-full-windows-amd64-${nextRelease.version}.exe"} ] }] ] diff --git a/README.md b/README.md index 983a134..4eb209f 100644 --- a/README.md +++ b/README.md @@ -62,4 +62,14 @@ And that's all! Run `NineDNS` with config file now: ```shell $ ninedns -c config.json ``` -> `NineDNS` autoloads `ninedns.json` in current directory if `-c` is not specified. \ No newline at end of file +> `NineDNS` autoloads `ninedns.json` in current directory if `-c` is not specified. + + +## Downloads +Download from [releases](https://github.com/wintbiit/NineDNS/releases) page. + +| Name | Description | +|----------------|-------------------------------------------------------| +| `ninedns-mini` | NineDNS binary without most providers. | +| `ninedns` | NineDNS binary with mysql provider and file providers | +| `ninedns-full` | NineDNS binary with all providers. | diff --git a/go.mod b/go.mod index 67f2341..3f7afff 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,18 @@ module github.com/wintbiit/ninedns go 1.20 require ( + github.com/bytedance/sonic v1.10.2 github.com/glebarez/sqlite v1.10.0 github.com/miekg/dns v1.1.57 github.com/redis/go-redis/v9 v9.3.0 github.com/wintbiit/larki v0.1.0 go.uber.org/zap v1.26.0 gorm.io/driver/mysql v1.5.2 + gorm.io/driver/postgres v1.5.4 gorm.io/gorm v1.25.5 ) require ( - github.com/bytedance/sonic v1.10.2 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect @@ -22,6 +23,10 @@ require ( github.com/glebarez/go-sqlite v1.21.2 // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/google/uuid v1.4.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect + github.com/jackc/pgx/v5 v5.5.1 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect @@ -31,9 +36,12 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.6.0 // indirect + golang.org/x/crypto v0.16.0 // indirect golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.19.0 // indirect + golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.16.0 // indirect modernc.org/libc v1.37.0 // indirect modernc.org/mathutil v1.6.0 // indirect diff --git a/go.sum b/go.sum index 8ec1612..3a87bba 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,14 @@ github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9 github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= +github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= @@ -53,6 +61,7 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -70,15 +79,20 @@ go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM= golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -87,6 +101,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= +gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= +gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/provider/README.md b/provider/README.md index bc3c206..b795650 100644 --- a/provider/README.md +++ b/provider/README.md @@ -30,6 +30,11 @@ CREATE TABLE if not exists `rule_set_name` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` +Not include in default build, you need to add `mysql` tag to build it. +```bash +go build -tags "mysql" +``` + ### SQLite Provider config: ```json @@ -44,6 +49,11 @@ SQL is re-read according to ttl > Using `github.com/glebarez/sqlite` as sqlite driver, please refer to this repo for more usage. +Desabled by default, you need to add `sqlite` tag to build it. +```bash +go build -tags "sqlite" +``` + ### File Provider config: ```json @@ -70,6 +80,8 @@ config: ruleset name is used as file name. files in the directory will be re-read according to ttl + + ### Lark Provider config: ```json @@ -79,11 +91,29 @@ config: } } ``` -`Lark Provider` reads records from lark bitable. Also ruleset name is used as table name. +`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, so it's disabled by default. You can build it with `lark` tag to enable it. + which largely increases binary size, it's disabled by default. You need to add `lark` tag to build it. ```bash go build -tags "lark" -``` \ No newline at end of file +``` + +### Postgres Provider +config: +```json +{ + "provider": { + "postgres": "postgres://user:password@host:port/dbname?sslmode=disable" + } +} +``` + +`Postgres Provider` connects to a postgres database and reads records from it. Table name is rule set name. + +Desabled by default, you need to add `postgres` tag to build it. +```bash +go build -tags "postgres" +``` + diff --git a/provider/postgres.go b/provider/postgres.go new file mode 100644 index 0000000..010fc5c --- /dev/null +++ b/provider/postgres.go @@ -0,0 +1,53 @@ +//go:build postgres + +package provider + +import ( + "github.com/wintbiit/ninedns/model" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +type PostgresProvider struct { + Provider + *gorm.DB + dsn string +} + +func init() { + constructors["postgres"] = newPostgresProvider +} + +func newPostgresProvider(dsn string) (Provider, error) { + provider := &PostgresProvider{ + dsn: dsn, + } + + db, err := gorm.Open(postgres.Open(provider.dsn), &gorm.Config{}) + if err != nil { + return nil, err + } + + provider.DB = db + + return provider, nil +} + +func (p *PostgresProvider) Provide(ruleset string) ([]model.Record, error) { + tx := p.Begin() + defer tx.Rollback() + + var records []model.Record + + if err := tx.Table(ruleset).Find(&records).Error; err != nil { + return nil, err + } + + tx.Commit() + + return records, nil +} + +func (p *PostgresProvider) AutoMigrate(table string) error { + return p.DB.Table(table).AutoMigrate(&model.Record{}) +} diff --git a/provider/sqlite.go b/provider/sqlite.go index 6781a91..a58b5b0 100644 --- a/provider/sqlite.go +++ b/provider/sqlite.go @@ -1,3 +1,5 @@ +//go:build sqlite + package provider import (