mirror of
https://github.com/go-micro/go-micro.git
synced 2025-05-31 21:59:42 +02:00
make router configurable in api
This commit is contained in:
parent
73dc3723c7
commit
e313863a2a
@ -7,20 +7,21 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go-micro.dev/v4/api/router"
|
||||||
"go-micro.dev/v4/registry"
|
"go-micro.dev/v4/registry"
|
||||||
"go-micro.dev/v4/server"
|
"go-micro.dev/v4/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The Api interface provides a way to
|
// The Api interface provides a way to
|
||||||
// create composable API gateways
|
// create composable API gateways
|
||||||
type Api interface {
|
type Api interface {
|
||||||
// Initialise options
|
// Initialise options
|
||||||
Init(...Option) error
|
Init(...Option) error
|
||||||
// Get the options
|
// Get the options
|
||||||
Options() Options
|
Options() Options
|
||||||
// Register an endpoint
|
// Register an endpoint
|
||||||
Register(*Endpoint) error
|
Register(*Endpoint) error
|
||||||
// Deregister an endpoint
|
// Deregister an endpoint
|
||||||
Deregister(*Endpoint) error
|
Deregister(*Endpoint) error
|
||||||
// Run the api
|
// Run the api
|
||||||
Run(context.Context) error
|
Run(context.Context) error
|
||||||
@ -31,6 +32,8 @@ type Api interface {
|
|||||||
type Options struct {
|
type Options struct {
|
||||||
// Address of the server
|
// Address of the server
|
||||||
Address string
|
Address string
|
||||||
|
// Router for resolving routes
|
||||||
|
Router router.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Options) error
|
type Option func(*Options) error
|
||||||
|
@ -19,8 +19,12 @@ type api struct {
|
|||||||
func newApi(opts ...Option) Api {
|
func newApi(opts ...Option) Api {
|
||||||
options := NewOptions(opts...)
|
options := NewOptions(opts...)
|
||||||
|
|
||||||
// TODO: make configurable
|
rtr := options.Router
|
||||||
rtr := registry.NewRouter()
|
|
||||||
|
if rtr == nil {
|
||||||
|
// TODO: make configurable
|
||||||
|
rtr = registry.NewRouter()
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: make configurable
|
// TODO: make configurable
|
||||||
hdlr := rpc.NewHandler(
|
hdlr := rpc.NewHandler(
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-micro.dev/v4/api/router"
|
||||||
|
)
|
||||||
|
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Address: ":8080",
|
Address: ":8080",
|
||||||
@ -11,3 +15,11 @@ func NewOptions(opts ...Option) Options {
|
|||||||
|
|
||||||
return 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user