File: types.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.124.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: sh: 344; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package routers

import (
	"net/http"

	"github.com/getkin/kin-openapi/openapi3"
)

// Router helps link http.Request.s and an OpenAPIv3 spec
type Router interface {
	// FindRoute matches an HTTP request with the operation it resolves to.
	// Hosts are matched from the OpenAPIv3 servers key.
	//
	// If you experience ErrPathNotFound and have localhost hosts specified as your servers,
	// turning these server URLs as relative (leaving only the path) should resolve this.
	//
	// See openapi3filter for example uses with request and response validation.
	FindRoute(req *http.Request) (route *Route, pathParams map[string]string, err error)
}

// Route describes the operation an http.Request can match
type Route struct {
	Spec      *openapi3.T
	Server    *openapi3.Server
	Path      string
	PathItem  *openapi3.PathItem
	Method    string
	Operation *openapi3.Operation
}

// ErrPathNotFound is returned when no route match is found
var ErrPathNotFound error = &RouteError{"no matching operation was found"}

// ErrMethodNotAllowed is returned when no method of the matched route matches
var ErrMethodNotAllowed error = &RouteError{"method not allowed"}

// RouteError describes Router errors
type RouteError struct {
	Reason string
}

func (e *RouteError) Error() string { return e.Reason }