1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00

make router configurable in api

This commit is contained in:
Asim Aslam 2022-07-02 14:15:02 +01:00
parent 73dc3723c7
commit e313863a2a
3 changed files with 24 additions and 5 deletions

View File

@ -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
@ -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

View File

@ -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(

View File

@ -1,5 +1,9 @@
package api
import (
"go-micro.dev/v4/api/router"
)
func NewOptions(opts ...Option) Options {
options := Options{
Address: ":8080",
@ -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
}
}