Exemplo n.º 1
0
func loadDenco(routes []route) http.Handler {
	handler := &dencoHandler{routerMap: map[string]*denco.Router{
		"GET":    denco.New(),
		"POST":   denco.New(),
		"PUT":    denco.New(),
		"PATCH":  denco.New(),
		"DELETE": denco.New(),
	}}
	recordMap := make(map[string][]denco.Record)
	for _, route := range routes {
		var f http.HandlerFunc
		switch route.method {
		case "GET":
			f = handler.Get
		case "POST":
			f = handler.Post
		case "PUT":
			f = handler.Put
		case "PATCH":
			f = handler.Patch
		case "DELETE":
			f = handler.Delete
		}
		recordMap[route.method] = append(recordMap[route.method], denco.NewRecord(route.path, f))
	}
	for method, records := range recordMap {
		if err := handler.routerMap[method].Build(records); err != nil {
			panic(err)
		}
	}
	return handler
}
Exemplo n.º 2
0
func makeTestSingle2ParamRecords(n int) []denco.Record {
	records := make([]denco.Record, n)
	for i := 0; i < len(records); i++ {
		records[i] = denco.NewRecord(fmt.Sprintf("/user%d/:name/comment/:id", i), fmt.Sprintf("testroute%d", i))
	}
	return records
}
Exemplo n.º 3
0
func makeTestStaticRecords(n int) []denco.Record {
	records := make([]denco.Record, n)
	for i := 0; i < n; i++ {
		records[i] = denco.NewRecord("/"+randomString(50), fmt.Sprintf("testroute%d", i))
	}
	return records
}
Exemplo n.º 4
0
// buildForward builds forward router.
func (router *Router) buildForward() error {
	records := make([]denco.Record, len(router.routeTable))
	for i, route := range router.routeTable {
		records[i] = denco.NewRecord(route.Path, route)
	}
	router.forward = denco.New()
	return router.forward.Build(records)
}
Exemplo n.º 5
0
func loadDencoSingle(method, path string, handler *dencoHandler, hfunc http.HandlerFunc) http.Handler {
	handler.routerMap = map[string]*denco.Router{
		method: denco.New(),
	}

	if err := handler.routerMap[method].Build([]denco.Record{
		denco.NewRecord(path, hfunc),
	}); err != nil {
		panic(err)
	}
	return handler
}
Exemplo n.º 6
0
// Handle registers handlers for given patterns.
// If a handler already exists for pattern, it will be overridden.
// If it exists but with another method, a new method will be added.
func (t *Router) Handle(routes Routes) *Router {
	for i := range routes {
		// Check whether we have already had such route.
		index, ok := t.indexes[routes[i].Pattern]

		// If we haven't, add the route.
		if !ok {
			// Save pattern's index to simplify its search
			// in next iteration.
			t.indexes[routes[i].Pattern] = len(t.records)

			// Add the route to the slice.
			t.records = append(t.records, denco.NewRecord(routes[i].Pattern, routes[i]))
			continue
		}

		// Otherwise, just add new HTTP methods to the existing route.
		r := t.records[index].Value.(*Route)
		r.Handlers.Join(routes[i].Handlers)
	}
	return t
}
Exemplo n.º 7
0
func (d *defaultRouteBuilder) AddRoute(method, path string, operation *spec.Operation) {
	mn := strings.ToUpper(method)

	if handler, ok := d.api.HandlerFor(operation.ID); ok {
		consumes := d.spec.ConsumesFor(operation)
		produces := d.spec.ProducesFor(operation)
		parameters := d.spec.ParamsFor(method, path)
		definitions := d.spec.SecurityDefinitionsFor(operation)

		record := denco.NewRecord(pathConverter.ReplaceAllString(path, ":$1"), &routeEntry{
			Operation:      operation,
			Handler:        handler,
			Consumes:       consumes,
			Produces:       produces,
			Consumers:      d.api.ConsumersFor(consumes),
			Producers:      d.api.ProducersFor(produces),
			Parameters:     parameters,
			Formats:        d.api.Formats(),
			Binder:         newUntypedRequestBinder(parameters, d.spec.Spec(), d.api.Formats()),
			Authenticators: d.api.AuthenticatorsFor(definitions),
		})
		d.records[mn] = append(d.records[mn], record)
	}
}