From 96164127e16c6fce584d40d0deb270a3e9b73d6a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 18 Apr 2021 16:49:46 +0200 Subject: [PATCH] feat: add Use() method to add adapters to the aicra builder --- builder.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/builder.go b/builder.go index 65c5240..f65fefa 100644 --- a/builder.go +++ b/builder.go @@ -5,6 +5,7 @@ import ( "io" "net/http" + "git.xdrm.io/go/aicra/api" "git.xdrm.io/go/aicra/datatype" "git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/dynfunc" @@ -14,6 +15,7 @@ import ( type Builder struct { conf *config.Server handlers []*apiHandler + adapters []api.Adapter } // represents an api handler (method-pattern combination) @@ -31,9 +33,23 @@ func (b *Builder) AddType(t datatype.T) { if b.conf.Services != nil { panic(errLateType) } + if b.conf.Types == nil { + b.conf.Types = make([]datatype.T, 0) + } b.conf.Types = append(b.conf.Types, t) } +// Use adds an http adapter (middleware) +func (b *Builder) Use(adapter api.Adapter) { + if b.conf == nil { + b.conf = &config.Server{} + } + if b.adapters == nil { + b.adapters = make([]api.Adapter, 0) + } + b.adapters = append(b.adapters, adapter) +} + // Setup the builder with its api definition file // panics if already setup func (b *Builder) Setup(r io.Reader) error {