Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can add the StaticCustomWithFilters function? #33

Closed
imxxiv opened this issue Aug 20, 2019 · 10 comments
Closed

Can add the StaticCustomWithFilters function? #33

imxxiv opened this issue Aug 20, 2019 · 10 comments

Comments

@imxxiv
Copy link

imxxiv commented Aug 20, 2019

Please forgive my request because the project(static file server) needs ip whitelist and add Cache-Control header.
Atreugo's static file functions are not middlewares and filter.
I am going to write a StaticCustom function with filter, but find i can't use the router.ServeFilesCustom().

@savsgio
Copy link
Owner

savsgio commented Aug 22, 2019

Yes, the static files hasn't middlewares and filters.

I will add it in a few days.

@savsgio
Copy link
Owner

savsgio commented Aug 23, 2019

I've just added Filters option inside StaticFS.

Now static files will process the global middlewares and allow filters ;)

@savsgio savsgio closed this as completed Aug 23, 2019
@imxxiv
Copy link
Author

imxxiv commented Aug 23, 2019

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!"))
}

@savsgio
Copy link
Owner

savsgio commented Aug 23, 2019

I recommend you to use StaticCustom and atreugo Paths with filters, It's easier to use and avoid extra allocation, increasing performance

@savsgio
Copy link
Owner

savsgio commented Aug 23, 2019

@imxxiv
Copy link
Author

imxxiv commented Aug 23, 2019

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?

@imxxiv
Copy link
Author

imxxiv commented Aug 23, 2019

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

@savsgio
Copy link
Owner

savsgio commented Aug 23, 2019

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)
}

@savsgio
Copy link
Owner

savsgio commented Aug 23, 2019

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,
})

@imxxiv
Copy link
Author

imxxiv commented Aug 23, 2019

I understand, I made a mistake that should not be made.
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants