From 183fbc3face2bc8ea3fb30a97f50eb4916949b96 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Thu, 9 Jun 2022 05:36:02 +0300 Subject: [PATCH] update plugins --- .github/workflows/ci.yml | 5 ----- examples/demo-database/main.go | 2 +- examples/demo-geoip/main.go | 2 +- plugins/database/sqlite.go | 28 ++++++++++++++-------------- plugins/http/http.go | 5 +++-- plugins/http/router.go | 26 ++++++++++++++++++-------- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e699119..4245186 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,11 +12,6 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.17 - - name: CI env: COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }} diff --git a/examples/demo-database/main.go b/examples/demo-database/main.go index 0a65436..e3e7b1c 100644 --- a/examples/demo-database/main.go +++ b/examples/demo-database/main.go @@ -21,7 +21,7 @@ func main() { app.Plugins( plugins.Plugin{ Inject: NewController, - Resolve: func(routes *http.RouterPool, c *Controller) { + Resolve: func(routes http.RouterPool, c *Controller) { router := routes.Main() router.Use(middlewares.ThrottlingMiddleware(100)) router.Get("/users", c.Users) diff --git a/examples/demo-geoip/main.go b/examples/demo-geoip/main.go index 084e7e8..1c78227 100644 --- a/examples/demo-geoip/main.go +++ b/examples/demo-geoip/main.go @@ -23,7 +23,7 @@ func main() { ) app.Plugins( plugins.Plugin{ - Resolve: func(routes *http.RouterPool, gip geoip.GeoIP) { + Resolve: func(routes http.RouterPool, gip geoip.GeoIP) { router := routes.Main() router.Use( middlewares.CloudflareMiddleware(), diff --git a/plugins/database/sqlite.go b/plugins/database/sqlite.go index c21be42..c69fb5d 100644 --- a/plugins/database/sqlite.go +++ b/plugins/database/sqlite.go @@ -13,20 +13,20 @@ import ( "github.com/deweppro/go-orm/schema/sqlite" ) -type ( - ConfigSqlite struct { - Pool []Item `yaml:"sqlite"` - } - Item struct { - Name string `yaml:"name"` - File string `yaml:"file"` - InitMigration []string `yaml:"init_migration"` - } -) +//ConfigSqlite sqlite config model +type ConfigSqlite struct { + Pool []item `yaml:"sqlite"` +} + +type item struct { + Name string `yaml:"name"` + File string `yaml:"file"` + InitMigration []string `yaml:"init_migration"` +} func (v *ConfigSqlite) Default() { if len(v.Pool) == 0 { - v.Pool = []Item{ + v.Pool = []item{ { Name: "main", File: "./sqlite.db", @@ -47,13 +47,13 @@ func (v *ConfigSqlite) List() (list []schema.ItemInterface) { } //GetName getting config name -func (i Item) GetName() string { return i.Name } +func (i item) GetName() string { return i.Name } //GetDSN connection params -func (i Item) GetDSN() string { return i.File } +func (i item) GetDSN() string { return i.File } //Setup setting config connections params -func (i Item) Setup(_ schema.SetupInterface) {} +func (i item) Setup(_ schema.SetupInterface) {} //WithSQLite launch SQLite connection pool func WithSQLite() plugins.Plugin { diff --git a/plugins/http/http.go b/plugins/http/http.go index 300667a..03de8c0 100644 --- a/plugins/http/http.go +++ b/plugins/http/http.go @@ -23,8 +23,9 @@ func (v *Config) Default() { func WithHTTP() plugins.Plugin { return plugins.Plugin{ Config: &Config{}, - Inject: func(conf *Config, log logger.Logger) *RouterPool { - return newRoutePool(conf.Config, log) + Inject: func(conf *Config, log logger.Logger) (*routeProvider, RouterPool) { + rp := newRouteProvider(conf.Config, log) + return rp, rp }, } } diff --git a/plugins/http/router.go b/plugins/http/router.go index 1bf6549..f1a271d 100644 --- a/plugins/http/router.go +++ b/plugins/http/router.go @@ -217,14 +217,24 @@ type ( active bool route *route } + //RouterPool router pool handler - RouterPool struct { + RouterPool interface { + //All method to get all route handlers + All(call func(name string, router Router)) + //Main method to get Main route handler + Main() Router + //Get method to get route handler by key + Get(name string) Router + } + + routeProvider struct { pool map[string]*routePoolItem } ) -func newRoutePool(configs map[string]servers.Config, log logger.Logger) *RouterPool { - v := &RouterPool{ +func newRouteProvider(configs map[string]servers.Config, log logger.Logger) *routeProvider { + v := &routeProvider{ pool: make(map[string]*routePoolItem), } for name, config := range configs { @@ -237,26 +247,26 @@ func newRoutePool(configs map[string]servers.Config, log logger.Logger) *RouterP } //All method to get all route handlers -func (v *RouterPool) All(call func(name string, router Router)) { +func (v *routeProvider) All(call func(name string, router Router)) { for n, r := range v.pool { call(n, r.route) } } //Main method to get Main route handler -func (v *RouterPool) Main() Router { +func (v *routeProvider) Main() Router { return v.Get("main") } //Get method to get route handler by key -func (v *RouterPool) Get(name string) Router { +func (v *routeProvider) Get(name string) Router { if r, ok := v.pool[name]; ok { return r.route } panic(fmt.Sprintf("Route with name `%s` is not found", name)) } -func (v *RouterPool) Up() error { +func (v *routeProvider) Up() error { for n, r := range v.pool { r.active = true if err := r.route.Up(); err != nil { @@ -266,7 +276,7 @@ func (v *RouterPool) Up() error { return nil } -func (v *RouterPool) Down() error { +func (v *routeProvider) Down() error { for n, r := range v.pool { if !r.active { continue