Skip to content

Commit

Permalink
Use a http function to serve assets
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed May 29, 2021
1 parent d175dba commit 0fba106
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,28 @@ type Options struct {
}

// AssetsHandler implements the static handler for serving custom or original assets.
func AssetsHandler(opts *Options) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
var custPath = filepath.Join(setting.CustomPath, "public")
if !filepath.IsAbs(custPath) {
custPath = filepath.Join(setting.AppWorkPath, custPath)
}
func AssetsHandler(opts *Options) func(resp http.ResponseWriter, req *http.Request) {
var custPath = filepath.Join(setting.CustomPath, "public")
if !filepath.IsAbs(custPath) {
custPath = filepath.Join(setting.AppWorkPath, custPath)
}

if !filepath.IsAbs(opts.Directory) {
opts.Directory = filepath.Join(setting.AppWorkPath, opts.Directory)
}
if !filepath.IsAbs(opts.Directory) {
opts.Directory = filepath.Join(setting.AppWorkPath, opts.Directory)
}

return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
// custom files
if opts.handle(resp, req, http.Dir(custPath), opts.Prefix) {
return
}
return func(resp http.ResponseWriter, req *http.Request) {
// custom files
if opts.handle(resp, req, http.Dir(custPath), opts.Prefix) {
return
}

// internal files
if opts.handle(resp, req, fileSystem(opts.Directory), opts.Prefix) {
return
}
// internal files
if opts.handle(resp, req, fileSystem(opts.Directory), opts.Prefix) {
return
}

resp.WriteHeader(404)
})
resp.WriteHeader(404)
}
}

Expand Down

0 comments on commit 0fba106

Please sign in to comment.