Skip to content

Commit

Permalink
expose http handler
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Mar 28, 2024
1 parent 3b5c693 commit b22f766
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 45 deletions.
6 changes: 4 additions & 2 deletions gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,12 @@ func (r *Router) newMatcher(ctx context.Context) (*matcher.Matcher, []*contract.
}
}

if r.statusHandler != nil {
routes = append(routes, r.NewStatusRoute())
}

routes = append(
routes,
r.NewStatusRoute(),

r.NewConfigRoute(),
)

Expand Down
44 changes: 17 additions & 27 deletions gateway/runtime/standalone/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package standalone

import (
"context"
"fmt"
"embed"
"github.com/viant/datly/gateway"
"github.com/viant/datly/service/auth/jwt"
)
Expand All @@ -12,7 +12,7 @@ type Options struct {
ConfigURL string `short:"c" long:"cfg" description:"config URIPrefix"`
Version bool `short:"v" long:"version" description:"Version"`
config *Config
auth gateway.Authorizer
options []gateway.Option
useSingleton *bool
}

Expand All @@ -28,47 +28,37 @@ func NewOptions(ctx context.Context, opts ...Option) (*Options, error) {
for _, opt := range opts {
opt(options)
}
if options.config == nil {
if options.ConfigURL == "" {
return nil, fmt.Errorf("config url was empty")
}
ctx = context.Background()
config, err := NewConfigFromURL(ctx, options.ConfigURL)
if err != nil {
return nil, err
}
options.config = config
}
var err error
if options.config.Cognito != nil || options.config.JWTValidator != nil {
if options.auth, err = jwt.Init(options.config.Config, nil); err != nil {
return nil, err
}
options.options = append(options.options, gateway.WithAuthProvider(func(config *gateway.Config, fs *embed.FS) (gateway.Authorizer, error) {
return jwt.Init(config, fs)
}))
if options.config != nil {
options.options = append(options.options, gateway.WithConfig(options.config.Config))
} else if options.ConfigURL == "" {
options.options = append(options.options, gateway.WithConfigURL(options.ConfigURL))
}
return options, nil
}

// Option represents standalone option
type Option func(*Options)

// WithAuth sets an authorizer
func WithAuth(auth gateway.Authorizer) Option {
// WithOptions sets options
func WithOptions(options ...gateway.Option) Option {
return func(o *Options) {
o.auth = auth
o.options = options
}
}

// WithConfig sets a config
func WithConfig(config *Config) Option {
// WithUseSingleton sets a singleton
func WithUseSingleton(useSingleton bool) Option {
return func(o *Options) {
o.config = config
o.useSingleton = &useSingleton
}
}

// WithVersion sets a version
func WithUseSingleton(useSingleton *bool) Option {
func WithConfig(config *Config) Option {
return func(o *Options) {
o.useSingleton = useSingleton
o.config = config
}
}

Expand Down
9 changes: 3 additions & 6 deletions gateway/runtime/standalone/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/viant/datly/gateway"
"github.com/viant/datly/gateway/runtime/standalone/handler"
"github.com/viant/datly/view/extension"
"github.com/viant/gmetric"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -43,18 +42,16 @@ func New(ctx context.Context, opts ...Option) (*Server, error) {
config := options.config
config.Init(ctx)
//mux := http.NewServeMux()
metric := gmetric.New()
if config.Config == nil {
return nil, fmt.Errorf("gateway config was empty")
}

var service *gateway.Service
var gOptions = []gateway.Option{
gateway.WithExtensions(extension.Config),
gateway.WithConfig(config.Config),
gateway.WithMetrics(metric),
gateway.WithStatusHandler(handler.NewStatus(config.Version, &config.Meta)),
gateway.WithAuthorizer(options.auth),
}
if options.options != nil {
gOptions = append(gOptions, options.options...)
}
if options.UseSingleton() {
service, err = gateway.Singleton(ctx, gOptions...)
Expand Down
9 changes: 2 additions & 7 deletions internal/translator/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,8 @@ func (r *Rule) applyDefaults() {
r.Route.Content.Marshaller.XML.TypeName = r.XMLUnmarshalType
}
if r.Route.Cors == nil {
r.Route.Cors = &dpath.Cors{
AllowCredentials: setter.BoolPtr(true),
AllowHeaders: setter.StringsPtr("*"),
AllowMethods: setter.StringsPtr("*"),
AllowOrigins: setter.StringsPtr("*"),
ExposeHeaders: setter.StringsPtr("*"),
}
dC := dpath.DefaultCors()
r.Route.Cors = dC
}
}

Expand Down
13 changes: 13 additions & 0 deletions repository/path/cors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package path

import "github.com/viant/datly/internal/setter"

type Cors struct {
AllowCredentials *bool `yaml:"AllowCredentials,omitempty"`
AllowHeaders *[]string `yaml:"AllowHeaders,omitempty"`
Expand Down Expand Up @@ -38,3 +40,14 @@ func (c *Cors) inherit(cors *Cors) {
c.MaxAge = cors.MaxAge
}
}

func DefaultCors() *Cors {
ret := &Cors{
AllowCredentials: setter.BoolPtr(true),
AllowHeaders: setter.StringsPtr("*"),
AllowMethods: setter.StringsPtr("*"),
AllowOrigins: setter.StringsPtr("*"),
ExposeHeaders: setter.StringsPtr("*"),
}
return ret
}
24 changes: 24 additions & 0 deletions repository/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@ func (s *Service) inheritFromPath(component *Component, aPath *path.Path) {
}
}

func (s *Service) Register(components ...*Component) {
s.registry.Register(components...)
var paths = map[string]*path.Item{}
for _, pathItem := range s.paths.Container.Items {
for _, aPath := range pathItem.Paths {
paths[aPath.Key()] = pathItem
}
}
for _, component := range components {
pathItem, ok := paths[component.Path.Key()]
if !ok {
pathItem = &path.Item{}
s.paths.Container.Items = append(s.paths.Container.Items, pathItem)
paths[component.Path.Key()] = pathItem
}
pathItem.Paths = append([]*path.Path{}, &path.Path{
Settings: path.Settings{
Cors: path.DefaultCors(),
},
Path: component.Path,
})
}
}

func (s *Service) Constants() []*state.Parameter {
res, err := s.resources.Lookup(view.ResourceConstants)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datly
import (
"context"
"fmt"
"github.com/viant/datly/gateway"
"github.com/viant/datly/repository"
"github.com/viant/datly/repository/contract"
"github.com/viant/datly/repository/locator/component/dispatcher"
Expand Down Expand Up @@ -37,6 +38,7 @@ type (
options []repository.Option
signer *signer.Service
substitutes map[string]view.Substitutes
handler http.Handler
}

sessionOptions struct {
Expand Down Expand Up @@ -328,7 +330,7 @@ func (s *Service) AddViews(ctx context.Context, views ...*view.View) (*repositor
if err := components.Init(ctx); err != nil {
return nil, err
}
s.repository.Registry().Register(component)
s.repository.Register(component)
return component, nil
}

Expand Down Expand Up @@ -365,7 +367,7 @@ func (s *Service) AddComponent(ctx context.Context, component *repository.Compon
return err
}

s.repository.Registry().Register(components.Components...)
s.repository.Register(components.Components...)

return nil
}
Expand All @@ -375,7 +377,7 @@ func (s *Service) AddComponents(ctx context.Context, components *repository.Comp
if err := components.Init(ctx); err != nil {
return err
}
s.repository.Registry().Register(components.Components...)
s.repository.Register(components.Components...)
return nil
}

Expand Down Expand Up @@ -465,6 +467,19 @@ func (s *Service) LoadComponents(ctx context.Context, URL string, opts ...reposi
return components, nil
}

func (s *Service) HTTPHandler(ctx context.Context, options ...gateway.Option) (http.Handler, error) {
if s.handler != nil {
return s.handler, nil
}
var err error
options = append(options, gateway.WithRepository(s.repository))
s.handler, err = gateway.New(ctx, options...)
if err != nil {
return nil, err
}
return s.handler, nil
}

// New creates a datly service, repository allows you to bootstrap empty or existing yaml repository
func New(ctx context.Context, options ...repository.Option) (*Service, error) {
options = append([]repository.Option{
Expand Down

0 comments on commit b22f766

Please sign in to comment.