Skip to content

Commit

Permalink
Merge ffcb1e3 into 60ebde5
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Oct 21, 2020
2 parents 60ebde5 + ffcb1e3 commit 872cca8
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 43 deletions.
6 changes: 0 additions & 6 deletions definition/definition.go
Expand Up @@ -18,7 +18,6 @@ package definition

import (
"context"
"net/http"
"reflect"
)

Expand Down Expand Up @@ -228,11 +227,6 @@ type Definition struct {
// In some cases, succeessful data and error data should be generated in
// different ways.
ErrorProduces []string
// Handler is a http.Handler implementation, nirvana supports to add a http.Handler (e.g. httputil.ReverseProxy)
// directly to the router.
// If this field is not nil, there is no need to set the Function, Parameters, and Results fields.
// See examples/getting-started/handler for a real example.
Handler http.Handler
// Function is a function handler. It must be func type.
Function interface{}
// Parameters describes function parameters.
Expand Down
5 changes: 3 additions & 2 deletions examples/getting-started/any-method/main.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/caicloud/nirvana/config"
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/log"
"github.com/caicloud/nirvana/service"
)

func main() {
Expand All @@ -48,7 +49,7 @@ func main() {
{
Method: definition.Any,
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEAll},
Produces: []string{definition.MIMEText},
Function: func(ctx context.Context) (string, error) {
return "hello", nil
},
Expand All @@ -64,7 +65,7 @@ func main() {
Method: definition.Any,
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEAll},
Handler: httputil.NewSingleHostReverseProxy(rpURL),
Function: service.WrapHTTPHandler(httputil.NewSingleHostReverseProxy(rpURL)),
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion examples/getting-started/handler/main.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/caicloud/nirvana/definition"
"github.com/caicloud/nirvana/log"
"github.com/caicloud/nirvana/middlewares/reqlog"
"github.com/caicloud/nirvana/service"
)

func main() {
Expand Down Expand Up @@ -68,7 +69,7 @@ func main() {
Method: definition.Get,
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEAll},
Handler: httputil.NewSingleHostReverseProxy(rpURL),
Function: service.WrapHTTPHandler(httputil.NewSingleHostReverseProxy(rpURL)),
},
},
},
Expand Down
1 change: 0 additions & 1 deletion service/builder.go
Expand Up @@ -151,7 +151,6 @@ func (b *builder) copyDefinition(d *definition.Definition, consumes []string, pr
Method: d.Method,
Summary: d.Summary,
Function: d.Function,
Handler: d.Handler,
Description: d.Description,
}
if len(d.Consumes) > 0 {
Expand Down
34 changes: 9 additions & 25 deletions service/executor.go
Expand Up @@ -64,16 +64,19 @@ func (i *inspector) addDefinition(d definition.Definition) error {
if len(d.ErrorProduces) <= 0 {
return definitionNoErrorProduces.Error(d.Method, i.path)
}
if d.Function == nil && d.Handler == nil {
if d.Function == nil {
return definitionNoFunction.Error(d.Method, i.path)
}

value := reflect.ValueOf(d.Function)
if value.Kind() != reflect.Func {
return definitionInvalidFunctionType.Error(value.Type(), d.Method, i.path)
}
c := &executor{
logger: i.logger,
method: method,
code: HTTPCodeFor(d.Method),
logger: i.logger,
method: method,
code: HTTPCodeFor(d.Method),
function: value,
}

consumeAll := false
consumes := map[string]bool{}
for _, ct := range d.Consumes {
Expand Down Expand Up @@ -118,19 +121,6 @@ func (i *inspector) addDefinition(d definition.Definition) error {
}
}
}

if d.Handler != nil {
c.handler = d.Handler
i.executors[method] = append(i.executors[method], c)
return nil
}

value := reflect.ValueOf(d.Function)
if value.Kind() != reflect.Func {
return definitionInvalidFunctionType.Error(value.Type(), d.Method, i.path)
}
c.function = value

errorProduceAll := false
errorProduces := map[string]bool{}
for _, ct := range d.ErrorProduces {
Expand Down Expand Up @@ -391,7 +381,6 @@ type executor struct {
parameters []parameter
results []result
function reflect.Value
handler http.Handler
}

type parameter struct {
Expand Down Expand Up @@ -450,11 +439,6 @@ func (e *executor) Execute(ctx context.Context) (err error) {
if c == nil {
return noContext.Error()
}
if e.handler != nil {
e.handler.ServeHTTP(c.ResponseWriter(), c.Request())
return nil
}

paramValues := make([]reflect.Value, 0, len(e.parameters))
for _, p := range e.parameters {
result, err := p.generator.Generate(ctx, c.ValueContainer(), e.consumers, p.name, p.targetType)
Expand Down
2 changes: 1 addition & 1 deletion service/utils.go
Expand Up @@ -112,7 +112,7 @@ var definitionNoConsumes = errors.InternalServerError.Build("Nirvana:Service:Def
var definitionNoProduces = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoProduces", "no content type to produce in [${method}]${path}")
var definitionNoErrorProduces = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoErrorProduces",
"no content type to produce error in [${method}]${path}")
var definitionNoFunction = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoFunction", "no function or handler in [${method}]${path}")
var definitionNoFunction = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoFunction", "no function in [${method}]${path}")
var definitionInvalidFunctionType = errors.InternalServerError.Build("Nirvana:Service:DefinitionInvalidFunctionType",
"${type} is not function in [${method}]${path}")

Expand Down
7 changes: 0 additions & 7 deletions utils/api/definitions.go
Expand Up @@ -98,10 +98,6 @@ type Definition struct {

// NewDefinition creates openapi.Definition from definition.Definition.
func NewDefinition(tc *TypeContainer, d *definition.Definition) (*Definition, error) {
if d.Function == nil {
return nil, nil
}

cd := &Definition{
Method: d.Method,
HTTPMethod: service.HTTPMethodFor(d.Method),
Expand Down Expand Up @@ -175,9 +171,6 @@ func NewDefinitions(tc *TypeContainer, definitions []definition.Definition) ([]D
if err != nil {
return nil, err
}
if cd == nil {
continue
}
result[i] = *cd
}
return result, nil
Expand Down

0 comments on commit 872cca8

Please sign in to comment.