Comments (10)
I understand, I made a mistake that should not be made.
Thank you very much!
from atreugo.
Yes, the static files hasn't middlewares and filters.
I will add it in a few days.
from atreugo.
I've just added Filters
option inside StaticFS.
Now static files will process the global middlewares and allow filters ;)
from atreugo.
Thanks!
Yesterday I have implemented the demand with RequestHandlerPathWithFilters. Because I have been busy, I have not given you feedback in time.
server.RequestHandlerPathWithFilters("GET", "/default/*filepath", staticFile, filters)
func staticFile(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Cache-Control"), []byte("public, max-age=3600"))
fs := &fasthttp.FS{
Root: "/static",
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: false,
PathNotFound: noFile,
}
fsHandler := fs.NewRequestHandler()
fsHandler(ctx)
}
This method of RequestHandlerPathWithFilters is very useful. Can use this method to achieve upload
server.RequestHandlerPathWithFilters("POST", "/upload", UploadHandler, filters)
// UploadHandler is here
func UploadHandler(ctx *fasthttp.RequestCtx) {
data, err := ctx.MultipartForm()
if err != nil {
ctx.SetStatusCode(500)
fmt.Println("get upload file error:", err)
return
}
fileObj := data.File["uploadfile"][0]
err = fasthttp.SaveMultipartFile(fileObj, fileObj.Filename)
if err != nil {
ctx.SetStatusCode(500)
fmt.Println("save upload file error:", err)
return
}
ctx.Write([]byte("save file successfully!"))
}
from atreugo.
I recommend you to use StaticCustom
and atreugo Paths with filters, It's easier to use and avoid extra allocation, increasing performance
from atreugo.
See this example: https://github.com/savsgio/atreugo/tree/master/examples/static_files
from atreugo.
avoid extra allocation, increasing performance
I am a newbie, In the follow code, the Path is:
server.RequestHandlerPathWithFilters("GET", "/default/*filepath", staticFile, filters)
But in the StaticCustom method, the Path is:
r.RequestHandlerPathWithFilters("GET", url+"/*filepath", ffs.NewRequestHandler(), fs.Filters)
My understanding should be the same, Where is the difference?
from atreugo.
I have already seen https://github.com/savsgio/atreugo/tree/master/examples/static_files
This StaticCustom method is also finally passed the RequestHandlerPathWithFilters method add to router
from atreugo.
Here, you are creating a new FS (extra allocations, it's better preallocating before and use the request handler of FS) on every requests:
func staticFile(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Cache-Control"), []byte("public, max-age=3600"))
fs := &fasthttp.FS{
Root: "/static",
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: false,
PathNotFound: noFile,
}
fsHandler := fs.NewRequestHandler()
fsHandler(ctx)
}
You should move this two lines outside.
fs := &fasthttp.FS{
Root: "/static",
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: false,
PathNotFound: noFile,
}
fsHandler := fs.NewRequestHandler()
In StaticCustom
you create a new FS, but just be added the fsHandler
in view path.
Something like this:
fs := &fasthttp.FS{
Root: "/static",
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: false,
PathNotFound: noFile,
}
fsHandler := fs.NewRequestHandler()
func staticFile(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Cache-Control"), []byte("public, max-age=3600"))
fsHandler(ctx)
}
from atreugo.
In Atreugo
filters := atreugo.Filters{
After: []atreugo.Middleware{func(ctx *atreugo.RequestCtx) (int, error) {
ctx.Response.Header.SetCanonical([]byte("Cache-Control"), []byte("public, max-age=3600"))
return 200, nil
}},
}
server.StaticCustom("/mypath", &StaticFS{
Filters: filters,
Root: "/static",
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: false,
PathNotFound: noFile,
})
from atreugo.
Related Issues (20)
- Possible breaking change in patch version? "path must begin with '/' in path ''" HOT 2
- AttachContext overrides previous AttachContext HOT 2
- Hi, Savsgio, how to add CORS middleware? HOT 30
- Is it possible to run "after" middleware even when "before" middleware exits error? HOT 5
- Unable to match URL with "/" ending HOT 1
- Unsupported path parameters with root / prefix HOT 1
- Problem with trailing "/" when optional url param is empty (11 version) - 308 redirect HOT 3
- I can not install HOT 1
- Error! installing with go get github.com/savsgio/atreugo HOT 5
- Cannot update/download deps HOT 1
- any sort of html template kind of real life example code to go with? router with html hello world at least HOT 1
- Instrumenting servers / paths to add metrics doesn't work when view returns an error HOT 1
- Use atreugo and autocert HOT 2
- Support custom logger HOT 4
- Possible race condition in context propagation HOT 3
- rate limit middleware request HOT 3
- Race condition when calling RequestCtx.Value() HOT 4
- Sentry APM Middleware HOT 1
- Occasional panic HOT 1
- Configurable JSON Marshaller HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from atreugo.