Skip to content

Commit

Permalink
make router configurable in api
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jul 2, 2022
1 parent 73dc372 commit e313863
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
9 changes: 6 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"regexp"
"strings"

"go-micro.dev/v4/api/router"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/server"
)

// The Api interface provides a way to
// The Api interface provides a way to
// create composable API gateways
type Api interface {
// Initialise options
Init(...Option) error
// Get the options
Options() Options
// Register an endpoint
// Register an endpoint
Register(*Endpoint) error
// Deregister an endpoint
// Deregister an endpoint
Deregister(*Endpoint) error
// Run the api
Run(context.Context) error
Expand All @@ -31,6 +32,8 @@ type Api interface {
type Options struct {
// Address of the server
Address string
// Router for resolving routes
Router router.Router
}

type Option func(*Options) error
Expand Down
8 changes: 6 additions & 2 deletions api/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ type api struct {
func newApi(opts ...Option) Api {
options := NewOptions(opts...)

// TODO: make configurable
rtr := registry.NewRouter()
rtr := options.Router

if rtr == nil {
// TODO: make configurable
rtr = registry.NewRouter()
}

// TODO: make configurable
hdlr := rpc.NewHandler(
Expand Down
12 changes: 12 additions & 0 deletions api/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package api

import (
"go-micro.dev/v4/api/router"
)

func NewOptions(opts ...Option) Options {
options := Options{
Address: ":8080",
Expand All @@ -11,3 +15,11 @@ func NewOptions(opts ...Option) Options {

return options
}

// WithRouter sets the router to use e.g static or registry
func WithRouter(r router.Router) Option {
return func(o *Options) error {
o.Router = r
return nil
}
}

0 comments on commit e313863

Please sign in to comment.