mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Nothing special here
Former-commit-id: af61c47c0462ec4b8d3699e3798c215a3feceb92
This commit is contained in:
parent
805ba56863
commit
b3bc8e71fb
5
doc.go
5
doc.go
|
@ -385,6 +385,7 @@ Static Files
|
||||||
|
|
||||||
Example code:
|
Example code:
|
||||||
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -392,8 +393,6 @@ Example code:
|
||||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
// if your ide cannot find the ./static folder try to build that program and after execute it
|
|
||||||
// or try to download & run this example via LiteIDE.
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -407,7 +406,7 @@ Example code:
|
||||||
// This will serve the ./static/favicons/iris_favicon_32_32.ico to: 127.0.0.1:8080/favicon_32_32.ico
|
// This will serve the ./static/favicons/iris_favicon_32_32.ico to: 127.0.0.1:8080/favicon_32_32.ico
|
||||||
|
|
||||||
app.Get("/", func(ctx *iris.Context) {
|
app.Get("/", func(ctx *iris.Context) {
|
||||||
ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser, if not please refresh or clear the browser's cache.")
|
ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser.")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Listen(":8080")
|
app.Listen(":8080")
|
||||||
|
|
13
fs.go
13
fs.go
|
@ -3,6 +3,7 @@ package iris
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -42,6 +43,16 @@ func toWebPath(systemPath string) string {
|
||||||
return webpath
|
return webpath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// abs calls filepath.Abs but ignores the error and
|
||||||
|
// returns the original value if any error occured.
|
||||||
|
func abs(path string) string {
|
||||||
|
absPath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
return absPath
|
||||||
|
}
|
||||||
|
|
||||||
// NewStaticHandlerBuilder returns a new Handler which serves static files
|
// NewStaticHandlerBuilder returns a new Handler which serves static files
|
||||||
// supports gzip, no listing and much more
|
// supports gzip, no listing and much more
|
||||||
// Note that, this static builder returns a Handler
|
// Note that, this static builder returns a Handler
|
||||||
|
@ -52,7 +63,7 @@ func toWebPath(systemPath string) string {
|
||||||
// structure and want a fluent api to work on.
|
// structure and want a fluent api to work on.
|
||||||
func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder {
|
func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder {
|
||||||
return &fsHandler{
|
return &fsHandler{
|
||||||
directory: http.Dir(dir),
|
directory: http.Dir(abs(dir)),
|
||||||
// default route path is the same as the directory
|
// default route path is the same as the directory
|
||||||
requestPath: toWebPath(dir),
|
requestPath: toWebPath(dir),
|
||||||
// enable strip path by-default
|
// enable strip path by-default
|
||||||
|
|
|
@ -82,7 +82,7 @@ Edit your main .go source file to adapt one of these routers and restart your ap
|
||||||
|
|
||||||
// the rest of your source code...
|
// the rest of your source code...
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
app.Listen("%s")
|
app.Listen("%s")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,7 +405,7 @@ func (router *Router) StaticServe(systemPath string, requestPath ...string) Rout
|
||||||
|
|
||||||
// StaticContent serves bytes, memory cached, on the reqPath
|
// StaticContent serves bytes, memory cached, on the reqPath
|
||||||
// a good example of this is how the websocket server uses that to auto-register the /iris-ws.js
|
// a good example of this is how the websocket server uses that to auto-register the /iris-ws.js
|
||||||
func (router *Router) StaticContent(reqPath string, cType string, content []byte) RouteInfo { // func(string) because we use that on websockets
|
func (router *Router) StaticContent(reqPath string, cType string, content []byte) RouteInfo {
|
||||||
modtime := time.Now()
|
modtime := time.Now()
|
||||||
h := func(ctx *Context) {
|
h := func(ctx *Context) {
|
||||||
if err := ctx.SetClientCachedBody(StatusOK, content, cType, modtime); err != nil {
|
if err := ctx.SetClientCachedBody(StatusOK, content, cType, modtime); err != nil {
|
||||||
|
@ -504,6 +504,8 @@ func (router *Router) StaticEmbedded(requestPath string, vdir string, assetFn fu
|
||||||
//
|
//
|
||||||
// panics on error
|
// panics on error
|
||||||
func (router *Router) Favicon(favPath string, requestPath ...string) RouteInfo {
|
func (router *Router) Favicon(favPath string, requestPath ...string) RouteInfo {
|
||||||
|
favPath = abs(favPath)
|
||||||
|
|
||||||
f, err := os.Open(favPath)
|
f, err := os.Open(favPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(errDirectoryFileNotFound.Format(favPath, err.Error()))
|
panic(errDirectoryFileNotFound.Format(favPath, err.Error()))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user