mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 12:07:28 +00:00
Update to 7.2.0. Read https://github.com/kataras/iris/blob/master/HISTORY.md#th-15-june-2017--v720
Fix https://github.com/iris-contrib/community-board/issues/12 . Read more: https://github.com/kataras/iris/blob/master/HISTORY.md#th-15-june-2017--v720 Former-commit-id: 398cb69fdc7e5367b147693371287ffb7b912feb
This commit is contained in:
96
core/router/spa.go
Normal file
96
core/router/spa.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// AssetValidator returns true if "filename"
|
||||
// is asset, i.e: strings.Contains(filename, ".").
|
||||
type AssetValidator func(filename string) bool
|
||||
|
||||
// SPABuilder helps building a single page application server
|
||||
// which serves both routes and files from the root path.
|
||||
type SPABuilder struct {
|
||||
IndexNames []string
|
||||
AssetHandler context.Handler
|
||||
AssetValidators []AssetValidator
|
||||
}
|
||||
|
||||
// NewSPABuilder returns a new Single Page Application builder
|
||||
// It does what StaticWeb expected to do when serving files and routes at the same time
|
||||
// from the root "/" path.
|
||||
//
|
||||
// Accepts a static asset handler, which can be an app.StaticHandler, app.StaticEmbeddedHandler...
|
||||
func NewSPABuilder(assetHandler context.Handler) *SPABuilder {
|
||||
if assetHandler == nil {
|
||||
assetHandler = func(ctx context.Context) {
|
||||
ctx.Writef("empty asset handler")
|
||||
}
|
||||
}
|
||||
|
||||
return &SPABuilder{
|
||||
IndexNames: []string{"index.html"},
|
||||
AssetHandler: assetHandler,
|
||||
AssetValidators: []AssetValidator{
|
||||
func(path string) bool {
|
||||
return strings.Contains(path, ".")
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SPABuilder) isAsset(reqPath string) bool {
|
||||
for _, v := range s.AssetValidators {
|
||||
if !v(reqPath) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// BuildWrapper returns a wrapper which serves the single page application
|
||||
// with the declared configuration.
|
||||
//
|
||||
// It should be passed to the router's `WrapRouter`:
|
||||
// https://godoc.org/github.com/kataras/iris/core/router#Router.WrapRouter
|
||||
//
|
||||
// Example: https://github.com/kataras/iris/tree/master/_examples/beginner/file-server/single-page-application-builder
|
||||
func (s *SPABuilder) BuildWrapper(cPool *context.Pool) WrapperFunc {
|
||||
|
||||
fileServer := s.AssetHandler
|
||||
indexNames := s.IndexNames
|
||||
|
||||
wrapper := func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
|
||||
path := r.URL.Path
|
||||
|
||||
if !s.isAsset(path) {
|
||||
// it's not asset, execute the registered route's handlers
|
||||
router(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := cPool.Acquire(w, r)
|
||||
|
||||
for _, index := range indexNames {
|
||||
if strings.HasSuffix(path, index) {
|
||||
localRedirect(ctx, "./")
|
||||
cPool.Release(ctx)
|
||||
// "/" should be manually registered.
|
||||
// We don't setup an index handler here,
|
||||
// let full control to the user
|
||||
// (use middleware, ctx.ServeFile or ctx.View and so on...)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// execute file server for path
|
||||
fileServer(ctx)
|
||||
|
||||
cPool.Release(ctx)
|
||||
}
|
||||
|
||||
return wrapper
|
||||
}
|
||||
Reference in New Issue
Block a user