当前位置: 首页>>代码示例>>Golang>>正文


Golang Document.Operations方法代码示例

本文整理汇总了Golang中github.com/vikstrous/go-swagger/spec.Document.Operations方法的典型用法代码示例。如果您正苦于以下问题:Golang Document.Operations方法的具体用法?Golang Document.Operations怎么用?Golang Document.Operations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/vikstrous/go-swagger/spec.Document的用法示例。


在下文中一共展示了Document.Operations方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DefaultRouter

// DefaultRouter creates a default implemenation of the router
func DefaultRouter(spec *spec.Document, api RoutableAPI) Router {
	builder := newDefaultRouteBuilder(spec, api)
	if spec != nil {
		for method, paths := range spec.Operations() {
			for path, operation := range paths {
				builder.AddRoute(method, path, operation)
			}
		}
	}
	return builder.Build()
}
开发者ID:vikstrous,项目名称:go-swagger,代码行数:12,代码来源:router.go

示例2: newRoutableUntypedAPI

func newRoutableUntypedAPI(spec *spec.Document, api *untyped.API, context *Context) *routableUntypedAPI {
	var handlers map[string]http.Handler
	if spec == nil || api == nil {
		return nil
	}
	for _, hls := range spec.Operations() {
		for _, op := range hls {
			schemes := spec.SecurityDefinitionsFor(op)

			if oh, ok := api.OperationHandlerFor(op.ID); ok {
				if handlers == nil {
					handlers = make(map[string]http.Handler)
				}

				handlers[op.ID] = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					// lookup route info in the context
					route, _ := context.RouteInfo(r)

					// bind and validate the request using reflection
					bound, validation := context.BindAndValidate(r, route)
					if validation != nil {
						context.Respond(w, r, route.Produces, route, validation)
						return
					}

					// actually handle the request
					result, err := oh.Handle(bound)
					if err != nil {
						// respond with failure
						context.Respond(w, r, route.Produces, route, err)
						return
					}

					// respond with success
					context.Respond(w, r, route.Produces, route, result)
				})

				if len(schemes) > 0 {
					handlers[op.ID] = newSecureAPI(context, handlers[op.ID])
				}
			}
		}
	}

	return &routableUntypedAPI{
		api:             api,
		handlers:        handlers,
		defaultProduces: api.DefaultProduces,
		defaultConsumes: api.DefaultConsumes,
	}
}
开发者ID:vikstrous,项目名称:go-swagger,代码行数:51,代码来源:context.go


注:本文中的github.com/vikstrous/go-swagger/spec.Document.Operations方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。