1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-26 22:37:08 +00:00

fixes, i18n, sitemap generator and new examples

Former-commit-id: 54801dc705ee0fa66232f65063f8a68c9cc31921
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-12-13 23:06:18 +02:00
parent c66f7a6d21
commit 04477c3e41
58 changed files with 1474 additions and 1311 deletions

View File

@@ -283,7 +283,11 @@ func (api *APIBuilder) SetExecutionRules(executionRules ExecutionRules) Party {
return api
}
func (api *APIBuilder) createRoutes(methods []string, relativePath string, handlers ...context.Handler) []*Route {
// CreateRoutes returns a list of Party-based Routes.
// It does NOT registers the route. Use `Handle, Get...` methods instead.
// This method can be used for third-parties Iris helpers packages and tools
// that want a more detailed view of Party-based Routes before take the decision to register them.
func (api *APIBuilder) CreateRoutes(methods []string, relativePath string, handlers ...context.Handler) []*Route {
// if relativePath[0] != '/' {
// return nil, errors.New("path should start with slash and should not be empty")
// }
@@ -393,7 +397,7 @@ func getCaller() (string, int) {
//
// Returns a *Route, app will throw any errors later on.
func (api *APIBuilder) Handle(method string, relativePath string, handlers ...context.Handler) *Route {
routes := api.createRoutes([]string{method}, relativePath, handlers...)
routes := api.CreateRoutes([]string{method}, relativePath, handlers...)
var route *Route // the last one is returned.
for _, route = range routes {
@@ -473,7 +477,7 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio
}
requestPath = joinPath(requestPath, WildcardFileParam())
routes := api.createRoutes([]string{http.MethodGet, http.MethodHead}, requestPath, h)
routes := api.CreateRoutes([]string{http.MethodGet, http.MethodHead}, requestPath, h)
getRoute = routes[0]
// we get all index, including sub directories even if those
// are already managed by the static handler itself.
@@ -489,7 +493,7 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio
}
requestPath := s.RequestPath[strings.Index(s.RequestPath, api.relativePath)+len(api.relativePath):]
routes = append(routes, api.createRoutes([]string{http.MethodGet}, requestPath, h)...)
routes = append(routes, api.CreateRoutes([]string{http.MethodGet}, requestPath, h)...)
getRoute.StaticSites = append(getRoute.StaticSites, s)
}

View File

@@ -42,7 +42,8 @@ func (h *routerHandler) getTree(method, subdomain string) *trie {
return nil
}
func (h *routerHandler) addRoute(r *Route) error {
// AddRoute registers a route. See `Router.AddRouteUnsafe`.
func (h *routerHandler) AddRoute(r *Route) error {
var (
routeName = r.Name
method = r.Method
@@ -136,7 +137,7 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
// on route, it will be stacked shown in this build state
// and no in the lines of the user's action, they should read
// the docs better. Or TODO: add a link here in order to help new users.
if err := h.addRoute(r); err != nil {
if err := h.AddRoute(r); err != nil {
// node errors:
rp.Addf("%s: %w", r.String(), err)
continue

View File

@@ -183,6 +183,11 @@ type Party interface {
// Any registers a route for ALL of the http methods
// (Get,Post,Put,Head,Patch,Options,Connect,Delete).
Any(registeredPath string, handlers ...context.Handler) []*Route
// CreateRoutes returns a list of Party-based Routes.
// It does NOT registers the route. Use `Handle, Get...` methods instead.
// This method can be used for third-parties Iris helpers packages and tools
// that want a more detailed view of Party-based Routes before take the decision to register them.
CreateRoutes(methods []string, relativePath string, handlers ...context.Handler) []*Route
// StaticContent registers a GET and HEAD method routes to the requestPath
// that are ready to serve raw static bytes, memory cached.
//

View File

@@ -3,6 +3,7 @@ package router
import (
"fmt"
"strings"
"time"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/macro"
@@ -44,8 +45,12 @@ type Route struct {
// route, manually or automatic by the framework,
// get the route by `Application#GetRouteByPath(staticSite.RequestPath)`.
StaticSites []context.StaticSite `json:"staticSites"`
topLink *Route
topLink *Route
// Sitemap properties: https://www.sitemaps.org/protocol.html
LastMod time.Time `json:"lastMod,omitempty"`
ChangeFreq string `json:"changeFreq,omitempty"`
Priority float32 `json:"priority,omitempty"`
}
// NewRoute returns a new route based on its method,
@@ -175,6 +180,32 @@ func (r *Route) DeepEqual(other *Route) bool {
return r.Equal(other) && r.tmpl.Src == other.tmpl.Src
}
// SetLastMod sets the date of last modification of the file served by this static GET route.
func (r *Route) SetLastMod(t time.Time) *Route {
r.LastMod = t
return r
}
// SetChangeFreq sets how frequently this static GET route's page is likely to change,
// possible values:
// - "always"
// - "hourly"
// - "daily"
// - "weekly"
// - "monthly"
// - "yearly"
// - "never"
func (r *Route) SetChangeFreq(freq string) *Route {
r.ChangeFreq = freq
return r
}
// SetPriority sets the priority of this static GET route's URL relative to other URLs on your site.
func (r *Route) SetPriority(prio float32) *Route {
r.Priority = prio
return r
}
// Tmpl returns the path template,
// it contains the parsed template
// for the route's path.
@@ -339,3 +370,15 @@ func (rd routeReadOnlyWrapper) MainHandlerName() string {
func (rd routeReadOnlyWrapper) StaticSites() []context.StaticSite {
return rd.Route.StaticSites
}
func (rd routeReadOnlyWrapper) GetLastMod() time.Time {
return rd.Route.LastMod
}
func (rd routeReadOnlyWrapper) GetChangeFreq() string {
return rd.Route.ChangeFreq
}
func (rd routeReadOnlyWrapper) GetPriority() float32 {
return rd.Route.Priority
}

View File

@@ -34,6 +34,26 @@ func (router *Router) RefreshRouter() error {
return router.BuildRouter(router.cPool, router.requestHandler, router.routesProvider, true)
}
// ErrNotRouteAdder throws on `AddRouteUnsafe` when a registered `RequestHandler`
// does not implements the optional `AddRoute(*Route) error` method.
var ErrNotRouteAdder = errors.New("request handler does not implement AddRoute method")
// AddRouteUnsafe adds a route directly to the router's request handler.
// Works before or after Build state.
// Mainly used for internal cases like `iris.WithSitemap`.
// Do NOT use it on serve-time.
func (router *Router) AddRouteUnsafe(r *Route) error {
if h := router.requestHandler; h != nil {
if v, ok := h.(interface {
AddRoute(*Route) error
}); ok {
return v.AddRoute(r)
}
}
return ErrNotRouteAdder
}
// BuildRouter builds the router based on
// the context factory (explicit pool in this case),
// the request handler which manages how the main handler will multiplexes the routes