// Code generated by ogen, DO NOT EDIT.
package api
import (
"net/http"
"net/url"
"strings"
"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) {
elem := r.URL.Path
elemIsEscaped := false
if rawPath := r.URL.RawPath; rawPath != "" {
if normalized, ok := uri.NormalizeEscapedPath(rawPath); ok {
elem = normalized
elemIsEscaped = strings.ContainsRune(elem, '%')
}
}
elem, ok := s.cutPrefix(elem)
if !ok || len(elem) == 0 {
s.notFound(w, r)
return
}
args := [2]string{}
// Static code generated router with unwrapped path search.
switch {
default:
if len(elem) == 0 {
break
}
switch elem[0] {
case '/': // Prefix: "/me/todo/lists"
if l := len("/me/todo/lists"); len(elem) >= l && elem[0:l] == "/me/todo/lists" {
elem = elem[l:]
} else {
break
}
if len(elem) == 0 {
switch r.Method {
case "GET":
s.handleListListsRequest([0]string{}, elemIsEscaped, w, r)
case "POST":
s.handleCreateListRequest([0]string{}, elemIsEscaped, w, r)
default:
s.notAllowed(w, r, "GET,POST")
}
return
}
switch elem[0] {
case '/': // Prefix: "/"
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
elem = elem[l:]
} else {
break
}
// Param: "listId"
// Match until "/"
idx := strings.IndexByte(elem, '/')
if idx < 0 {
idx = len(elem)
}
args[0] = elem[:idx]
elem = elem[idx:]
if len(elem) == 0 {
switch r.Method {
case "DELETE":
s.handleDeleteListRequest([1]string{
args[0],
}, elemIsEscaped, w, r)
case "GET":
s.handleGetListRequest([1]string{
args[0],
}, elemIsEscaped, w, r)
case "PATCH":
s.handleUpdateListRequest([1]string{
args[0],
}, elemIsEscaped, w, r)
default:
s.notAllowed(w, r, "DELETE,GET,PATCH")
}
return
}
switch elem[0] {
case '/': // Prefix: "/tasks"
if l := len("/tasks"); len(elem) >= l && elem[0:l] == "/tasks" {
elem = elem[l:]
} else {
break
}
if len(elem) == 0 {
switch r.Method {
case "GET":
s.handleListTasksRequest([1]string{
args[0],
}, elemIsEscaped, w, r)
case "POST":
s.handleCreateTaskRequest([1]string{
args[0],
}, elemIsEscaped, w, r)
default:
s.notAllowed(w, r, "GET,POST")
}
return
}
switch elem[0] {
case '/': // Prefix: "/"
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
elem = elem[l:]
} else {
break
}
// Param: "taskId"
// Leaf parameter, slashes are prohibited
idx := strings.IndexByte(elem, '/')
if idx >= 0 {
break
}
args[1] = elem
elem = ""
if len(elem) == 0 {
// Leaf node.
switch r.Method {
case "DELETE":
s.handleDeleteTaskRequest([2]string{
args[0],
args[1],
}, elemIsEscaped, w, r)
case "GET":
s.handleGetTaskRequest([2]string{
args[0],
args[1],
}, elemIsEscaped, w, r)
case "PATCH":
s.handleUpdateTaskRequest([2]string{
args[0],
args[1],
}, elemIsEscaped, w, r)
default:
s.notAllowed(w, r, "DELETE,GET,PATCH")
}
return
}
}
}
}
}
}
s.notFound(w, r)
}
// Route is route object.
type Route struct {
name string
summary string
operationID string
operationGroup string
pathPattern string
count int
args [2]string
}
// Name returns ogen operation name.
//
// It is guaranteed to be unique and not empty.
func (r Route) Name() string {
return r.name
}
// Summary returns OpenAPI summary.
func (r Route) Summary() string {
return r.summary
}
// OperationID returns OpenAPI operationId.
func (r Route) OperationID() string {
return r.operationID
}
// OperationGroup returns the x-ogen-operation-group value.
func (r Route) OperationGroup() string {
return r.operationGroup
}
// PathPattern returns OpenAPI path.
func (r Route) PathPattern() string {
return r.pathPattern
}
// Args returns parsed arguments.
func (r Route) Args() []string {
return r.args[:r.count]
}
// FindRoute finds Route for given method and path.
//
// Note: this method does not unescape path or handle reserved characters in path properly. Use FindPath instead.
func (s *Server) FindRoute(method, path string) (Route, bool) {
return s.FindPath(method, &url.URL{Path: path})
}
// FindPath finds Route for given method and URL.
func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) {
var (
elem = u.Path
args = r.args
)
if rawPath := u.RawPath; rawPath != "" {
if normalized, ok := uri.NormalizeEscapedPath(rawPath); ok {
elem = normalized
}
defer func() {
for i, arg := range r.args[:r.count] {
if unescaped, err := url.PathUnescape(arg); err == nil {
r.args[i] = unescaped
}
}
}()
}
elem, ok := s.cutPrefix(elem)
if !ok {
return r, false
}
// Static code generated router with unwrapped path search.
switch {
default:
if len(elem) == 0 {
break
}
switch elem[0] {
case '/': // Prefix: "/me/todo/lists"
if l := len("/me/todo/lists"); len(elem) >= l && elem[0:l] == "/me/todo/lists" {
elem = elem[l:]
} else {
break
}
if len(elem) == 0 {
switch method {
case "GET":
r.name = ListListsOperation
r.summary = "Get all task lists"
r.operationID = "listLists"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists"
r.args = args
r.count = 0
return r, true
case "POST":
r.name = CreateListOperation
r.summary = "Create a task list"
r.operationID = "createList"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists"
r.args = args
r.count = 0
return r, true
default:
return
}
}
switch elem[0] {
case '/': // Prefix: "/"
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
elem = elem[l:]
} else {
break
}
// Param: "listId"
// Match until "/"
idx := strings.IndexByte(elem, '/')
if idx < 0 {
idx = len(elem)
}
args[0] = elem[:idx]
elem = elem[idx:]
if len(elem) == 0 {
switch method {
case "DELETE":
r.name = DeleteListOperation
r.summary = "Delete a task list"
r.operationID = "deleteList"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}"
r.args = args
r.count = 1
return r, true
case "GET":
r.name = GetListOperation
r.summary = "Get a task list"
r.operationID = "getList"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}"
r.args = args
r.count = 1
return r, true
case "PATCH":
r.name = UpdateListOperation
r.summary = "Update a task list"
r.operationID = "updateList"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}"
r.args = args
r.count = 1
return r, true
default:
return
}
}
switch elem[0] {
case '/': // Prefix: "/tasks"
if l := len("/tasks"); len(elem) >= l && elem[0:l] == "/tasks" {
elem = elem[l:]
} else {
break
}
if len(elem) == 0 {
switch method {
case "GET":
r.name = ListTasksOperation
r.summary = "Get all tasks in a list"
r.operationID = "listTasks"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}/tasks"
r.args = args
r.count = 1
return r, true
case "POST":
r.name = CreateTaskOperation
r.summary = "Create a task"
r.operationID = "createTask"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}/tasks"
r.args = args
r.count = 1
return r, true
default:
return
}
}
switch elem[0] {
case '/': // Prefix: "/"
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
elem = elem[l:]
} else {
break
}
// Param: "taskId"
// Leaf parameter, slashes are prohibited
idx := strings.IndexByte(elem, '/')
if idx >= 0 {
break
}
args[1] = elem
elem = ""
if len(elem) == 0 {
// Leaf node.
switch method {
case "DELETE":
r.name = DeleteTaskOperation
r.summary = "Delete a task"
r.operationID = "deleteTask"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}/tasks/{taskId}"
r.args = args
r.count = 2
return r, true
case "GET":
r.name = GetTaskOperation
r.summary = "Get a task"
r.operationID = "getTask"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}/tasks/{taskId}"
r.args = args
r.count = 2
return r, true
case "PATCH":
r.name = UpdateTaskOperation
r.summary = "Update a task"
r.operationID = "updateTask"
r.operationGroup = ""
r.pathPattern = "/me/todo/lists/{listId}/tasks/{taskId}"
r.args = args
r.count = 2
return r, true
default:
return
}
}
}
}
}
}
}
return r, false
}