diff --git a/internal/oas/oas_client_gen.go b/internal/oas/oas_client_gen.go index 4c942b7..b19ee1c 100644 --- a/internal/oas/oas_client_gen.go +++ b/internal/oas/oas_client_gen.go @@ -21,6 +21,34 @@ import ( "github.com/ogen-go/ogen/uri" ) +// Invoker invokes operations described by OpenAPI v3 specification. +type Invoker interface { + // AddPet invokes addPet operation. + // + // Add a new pet to the store. + // + // POST /pet + AddPet(ctx context.Context, request *Pet) (*Pet, error) + // DeletePet invokes deletePet operation. + // + // Deletes a pet. + // + // DELETE /pet/{petId} + DeletePet(ctx context.Context, params DeletePetParams) error + // GetPetById invokes getPetById operation. + // + // Returns a single pet. + // + // GET /pet/{petId} + GetPetById(ctx context.Context, params GetPetByIdParams) (GetPetByIdRes, error) + // UpdatePet invokes updatePet operation. + // + // Updates a pet in the store. + // + // POST /pet/{petId} + UpdatePet(ctx context.Context, params UpdatePetParams) error +} + // Client implements OAS client. type Client struct { serverURL *url.URL diff --git a/internal/oas/oas_router_gen.go b/internal/oas/oas_router_gen.go index 9a72605..28844d1 100644 --- a/internal/oas/oas_router_gen.go +++ b/internal/oas/oas_router_gen.go @@ -10,6 +10,19 @@ import ( "github.com/ogen-go/ogen/uri" ) +func (s *Server) cutPrefix(path string) (string, bool) { + prefix := s.cfg.Prefix + if prefix == "" { + return path, true + } + if !strings.HasPrefix(path, prefix) { + // Prefix doesn't match. + return "", false + } + // Cut prefix from the path. + return strings.TrimPrefix(path, prefix), true +} + // ServeHTTP serves http request as defined by OpenAPI v3 specification, // calling handler that matches the path or returning not found error. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -21,17 +34,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { elemIsEscaped = strings.ContainsRune(elem, '%') } } - if prefix := s.cfg.Prefix; len(prefix) > 0 { - if strings.HasPrefix(elem, prefix) { - // Cut prefix from the path. - elem = strings.TrimPrefix(elem, prefix) - } else { - // Prefix doesn't match. - s.notFound(w, r) - return - } - } - if len(elem) == 0 { + + elem, ok := s.cutPrefix(elem) + if !ok || len(elem) == 0 { s.notFound(w, r) return } @@ -158,6 +163,11 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) { }() } + elem, ok := s.cutPrefix(elem) + if !ok { + return r, false + } + // Static code generated router with unwrapped path search. switch { default: