Skip to content

Commit

Permalink
update plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 committed Jun 9, 2022
1 parent 419ba9d commit 183fbc3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-geoip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
28 changes: 14 additions & 14 deletions plugins/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions plugins/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
}
26 changes: 18 additions & 8 deletions plugins/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 183fbc3

Please sign in to comment.