mirror of
https://github.com/kataras/iris.git
synced 2026-01-11 05:55:57 +00:00
create the new FileServer and HandleDir, deprecate the rest APIBuilder/Party static methods and more
relative: https://github.com/kataras/iris/issues/1283 and removing pongo2 from vendor: https://github.com/kataras/iris/issues/1284 Former-commit-id: 3ec57b349f99faca2b8e36d9f7252db0b6ea080d
This commit is contained in:
@@ -29,25 +29,19 @@ type Route struct {
|
||||
// Execution happens after Begin and main Handler(s), can be empty.
|
||||
doneHandlers context.Handlers
|
||||
|
||||
Path string `json:"path"` // "/api/user/{id:uint64}"
|
||||
Path string `json:"path"` // the underline router's representation, i.e "/api/user/:id"
|
||||
// FormattedPath all dynamic named parameters (if any) replaced with %v,
|
||||
// used by Application to validate param values of a Route based on its name.
|
||||
FormattedPath string `json:"formattedPath"`
|
||||
|
||||
// StaticTarget if not empty, refers to the system (or virtual if embedded) directory
|
||||
// that this route is serving static files/resources from
|
||||
// or to a single static filename if this route created via `APIBuilder#Favicon`
|
||||
// or to a `StaticContentTarget` type if this rotue created by `APIBuilder#StaticContent`.
|
||||
//
|
||||
// If a route is serving static files via `APIBuilder`
|
||||
// there are two routes with the same dir/filename set to this field,
|
||||
// one for "HEAD" and the other for the "GET" http method.
|
||||
StaticTarget string
|
||||
// StaticSites if not empty, refers to the system (or virtual if embedded) directory
|
||||
// and sub directories that this "GET" route was registered to serve files and folders
|
||||
// that contain index.html (a site). The index handler may registered by other
|
||||
// route, manually or automatic by the framework,
|
||||
// get the route by `Application#GetRouteByPath(staticSite.RequestPath)`.
|
||||
StaticSites []context.StaticSite `json:"staticSites"`
|
||||
}
|
||||
|
||||
// StaticContentTarget used whenever a `Route#StaticTarget` refers to a raw []byte static content instead of a directory or a file.
|
||||
const StaticContentTarget = "content"
|
||||
|
||||
// NewRoute returns a new route based on its method,
|
||||
// subdomain, the path (unparsed or original),
|
||||
// handlers and the macro container which all routes should share.
|
||||
@@ -69,7 +63,7 @@ func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
|
||||
handlers = append(context.Handlers{macroEvaluatorHandler}, handlers...)
|
||||
}
|
||||
|
||||
path = cleanPath(path) // maybe unnecessary here but who cares in this moment
|
||||
path = cleanPath(path) // maybe unnecessary here.
|
||||
defaultName := method + subdomain + tmpl.Src
|
||||
formattedPath := formatPath(path)
|
||||
|
||||
@@ -87,26 +81,26 @@ func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
|
||||
return route, nil
|
||||
}
|
||||
|
||||
// use adds explicit begin handlers(middleware) to this route,
|
||||
// It's being called internally, it's useless for outsiders
|
||||
// because `Handlers` field is exported.
|
||||
// The callers of this function are: `APIBuilder#UseGlobal` and `APIBuilder#Done`.
|
||||
// Use adds explicit begin handlers to this route.
|
||||
// Alternatively the end-dev can prepend to the `Handlers` field.
|
||||
// Should be used before the `BuildHandlers` which is
|
||||
// called by the framework itself on `Application#Run` (build state).
|
||||
//
|
||||
// BuildHandlers should be called to build the route's `Handlers`.
|
||||
func (r *Route) use(handlers context.Handlers) {
|
||||
// Used internally at `APIBuilder#UseGlobal` -> `beginGlobalHandlers` -> `APIBuilder#Handle`.
|
||||
func (r *Route) Use(handlers ...context.Handler) {
|
||||
if len(handlers) == 0 {
|
||||
return
|
||||
}
|
||||
r.beginHandlers = append(r.beginHandlers, handlers...)
|
||||
}
|
||||
|
||||
// use adds explicit done handlers to this route.
|
||||
// It's being called internally, it's useless for outsiders
|
||||
// because `Handlers` field is exported.
|
||||
// The callers of this function are: `APIBuilder#UseGlobal` and `APIBuilder#Done`.
|
||||
// Done adds explicit finish handlers to this route.
|
||||
// Alternatively the end-dev can append to the `Handlers` field.
|
||||
// Should be used before the `BuildHandlers` which is
|
||||
// called by the framework itself on `Application#Run` (build state).
|
||||
//
|
||||
// BuildHandlers should be called to build the route's `Handlers`.
|
||||
func (r *Route) done(handlers context.Handlers) {
|
||||
// Used internally at `APIBuilder#DoneGlobal` -> `doneGlobalHandlers` -> `APIBuilder#Handle`.
|
||||
func (r *Route) Done(handlers ...context.Handler) {
|
||||
if len(handlers) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -161,6 +155,13 @@ func (r Route) String() string {
|
||||
r.Method, r.Subdomain, r.Tmpl().Src)
|
||||
}
|
||||
|
||||
// Equal compares the method, subdomaind and the
|
||||
// underline representation of the route's path,
|
||||
// instead of the `String` function which returns the front representation.
|
||||
func (r *Route) Equal(other *Route) bool {
|
||||
return r.Method == other.Method && r.Subdomain == other.Subdomain && r.Path == other.Path
|
||||
}
|
||||
|
||||
// Tmpl returns the path template,
|
||||
// it contains the parsed template
|
||||
// for the route's path.
|
||||
@@ -235,12 +236,12 @@ func (r Route) StaticPath() string {
|
||||
if bidx == -1 || len(src) <= bidx {
|
||||
return src // no dynamic part found
|
||||
}
|
||||
if bidx == 0 { // found at first index,
|
||||
// but never happens because of the prepended slash
|
||||
if bidx <= 1 { // found at first{...} or second index (/{...}),
|
||||
// although first index should never happen because of the prepended slash.
|
||||
return "/"
|
||||
}
|
||||
|
||||
return src[:bidx]
|
||||
return src[:bidx-1] // (/static/{...} -> /static)
|
||||
}
|
||||
|
||||
// ResolvePath returns the formatted path's %v replaced with the args.
|
||||
@@ -272,10 +273,15 @@ func (r Route) Trace() string {
|
||||
}
|
||||
printfmt += fmt.Sprintf(" %s ", r.Tmpl().Src)
|
||||
|
||||
mainHandlerName := r.MainHandlerName
|
||||
if !strings.HasSuffix(mainHandlerName, ")") {
|
||||
mainHandlerName += "()"
|
||||
}
|
||||
|
||||
if l := r.RegisteredHandlersLen(); l > 1 {
|
||||
printfmt += fmt.Sprintf("-> %s() and %d more", r.MainHandlerName, l-1)
|
||||
printfmt += fmt.Sprintf("-> %s and %d more", mainHandlerName, l-1)
|
||||
} else {
|
||||
printfmt += fmt.Sprintf("-> %s()", r.MainHandlerName)
|
||||
printfmt += fmt.Sprintf("-> %s", mainHandlerName)
|
||||
}
|
||||
|
||||
// printfmt := fmt.Sprintf("%s: %s >> %s", r.Method, r.Subdomain+r.Tmpl().Src, r.MainHandlerName)
|
||||
@@ -316,3 +322,7 @@ func (rd routeReadOnlyWrapper) Tmpl() macro.Template {
|
||||
func (rd routeReadOnlyWrapper) MainHandlerName() string {
|
||||
return rd.Route.MainHandlerName
|
||||
}
|
||||
|
||||
func (rd routeReadOnlyWrapper) StaticSites() []context.StaticSite {
|
||||
return rd.Route.StaticSites
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user