Skip to content

Commit

Permalink
refactored standalone endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Mar 27, 2024
1 parent 661cf3a commit b11c5bb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 40 deletions.
12 changes: 1 addition & 11 deletions cmd/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/viant/datly/cmd/options"
"github.com/viant/datly/gateway/runtime/standalone"
"github.com/viant/datly/internal/setter"
"github.com/viant/datly/service/auth/jwt"
)

func (s *Service) Run(ctx context.Context, options *options.Options) (err error) {
Expand Down Expand Up @@ -35,18 +34,9 @@ func (s *Service) run(ctx context.Context, run *options.Run) (*standalone.Server
parent, _ := url.Split(s.config.JobURL, file.Scheme)
s.config.FailedJobURL = url.Join(parent, "failed", "jobs")
}

if run.LoadPlugin && s.config.Config.PluginsURL != "" {
parent, _ := url.Split(run.PluginInfo, file.Scheme)
_ = s.fs.Copy(ctx, parent, s.config.Config.PluginsURL)
}

authenticator, err := jwt.Init(s.config.Config, nil)
var srv *standalone.Server
if authenticator == nil {
srv, err = standalone.New(ctx, s.config)
} else {
srv, err = standalone.NewWithAuth(ctx, s.config, authenticator)
}
return srv, err
return standalone.New(ctx, standalone.WithConfig(s.config))
}
9 changes: 8 additions & 1 deletion e2e/local/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ init:
pipeline:

deploy:
setPath:
action: exec:run
target: $target
checkError: true
commands:
- export GOPATH=${env.GOPATH}
- export PATH=/usr/local/go/bin:$PATH
set_sdk:
action: sdk.set
target: $target
sdk: go:1.21
sdk: go:1.22

buildValidator:
action: exec:run
Expand Down
4 changes: 3 additions & 1 deletion e2e/local/regression/regression.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
init:
v1: abc
v2: def
pipeline:
set_sdk:
action: sdk.set
target: $target
sdk: go:1.21
sdk: go:1.22

database:
action: run
Expand Down
9 changes: 1 addition & 8 deletions gateway/runtime/standalone/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ func RunApp(version string, args []string) error {
if err != nil {
log.Fatal(err)
}

if options.Version {
log.Printf("RuleIndexer: Version: %v\n", version)
return nil
}

configURL := options.ConfigURL
srv, err := NewWithURL(configURL, version)
if err != nil {
Expand All @@ -29,12 +27,7 @@ func RunApp(version string, args []string) error {
// NewWithURL create service with config URL
func NewWithURL(configURL, version string) (*Server, error) {
ctx := context.Background()
config, err := NewConfigFromURL(ctx, configURL)
if err != nil {
return nil, err
}
config.Version = version
srv, err := New(ctx, config)
srv, err := New(ctx, WithConfigURL(configURL))
if err != nil {
return nil, err
}
Expand Down
67 changes: 65 additions & 2 deletions gateway/runtime/standalone/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
package standalone

import (
"context"
"fmt"
"github.com/viant/datly/gateway"
"github.com/viant/datly/service/auth/jwt"
)

// Options represents standalone options
type Options struct {
ConfigURL string `short:"c" long:"cfg" description:"config URIPrefix"`
Version bool `short:"v" long:"version" description:"Version"`
ConfigURL string `short:"c" long:"cfg" description:"config URIPrefix"`
Version bool `short:"v" long:"version" description:"Version"`
config *Config
auth gateway.Authorizer
useSingleton *bool
}

func NewOptions(ctx context.Context, opts ...Option) (*Options, error) {
options := &Options{}
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
}
}
return options, nil
}

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

func WithAuth(auth gateway.Authorizer) Option {
return func(o *Options) {
o.auth = auth
}
}

func WithConfig(config *Config) Option {
return func(o *Options) {
o.config = config
}
}

func WithUseSingleton(useSingleton *bool) Option {
return func(o *Options) {
o.useSingleton = useSingleton
}
}

func WithConfigURL(configURL string) Option {
return func(o *Options) {
o.ConfigURL = configURL
}
}
31 changes: 15 additions & 16 deletions gateway/runtime/standalone/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (

type Server struct {
http.Server
Service *gateway.Service
Service *gateway.Service
auth gateway.Authorizer
useSingleton *bool //true by default
}

// shutdownOnInterrupt server on interupts
Expand All @@ -36,18 +38,19 @@ func (r *Server) shutdownOnInterrupt() {
}()
}

func NewWithAuth(ctx context.Context, gwayConfig *Config, auth gateway.Authorizer) (*Server, error) {
gwayConfig.Init(ctx)
func New(ctx context.Context, opts ...Option) (*Server, error) {
options, err := NewOptions(ctx, opts...)
config := options.config
config.Init(ctx)
//mux := http.NewServeMux()
metric := gmetric.New()
if gwayConfig.Config == nil {
if config.Config == nil {
return nil, fmt.Errorf("gateway config was empty")
}

service, err := gateway.SingletonWithConfig(
gwayConfig.Config,
handler.NewStatus(gwayConfig.Version, &gwayConfig.Meta),
auth,
config.Config,
handler.NewStatus(config.Version, &config.Meta),
options.auth,
extension.Config,
metric,
)
Expand All @@ -63,18 +66,14 @@ func NewWithAuth(ctx context.Context, gwayConfig *Config, auth gateway.Authorize
server := &Server{
Service: service,
Server: http.Server{
Addr: ":" + strconv.Itoa(gwayConfig.Endpoint.Port),
Addr: ":" + strconv.Itoa(config.Endpoint.Port),
Handler: service,
ReadTimeout: time.Millisecond * time.Duration(gwayConfig.Endpoint.ReadTimeoutMs),
WriteTimeout: time.Millisecond * time.Duration(gwayConfig.Endpoint.WriteTimeoutMs),
MaxHeaderBytes: gwayConfig.Endpoint.MaxHeaderBytes,
ReadTimeout: time.Millisecond * time.Duration(config.Endpoint.ReadTimeoutMs),
WriteTimeout: time.Millisecond * time.Duration(config.Endpoint.WriteTimeoutMs),
MaxHeaderBytes: config.Endpoint.MaxHeaderBytes,
},
}

server.shutdownOnInterrupt()
return server, nil
}

func New(ctx context.Context, config *Config) (*Server, error) {
return NewWithAuth(ctx, config, nil)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/viant/datly

go 1.21.7
go 1.22

require (
github.com/aerospike/aerospike-client-go v4.5.2+incompatible
Expand Down

0 comments on commit b11c5bb

Please sign in to comment.