Skip to content

Commit

Permalink
Implementing ServeFiles function
Browse files Browse the repository at this point in the history
Adding a handler that serves static files from an http.FileSystem, utilizing http.FileServer. Returns a 404 error if the requested file is not found.
  • Loading branch information
gabrielluizsf committed Sep 17, 2024
1 parent c97aefe commit 4a1d76b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func (r *Response) SetHeader(key, value string) {
r.res.Header().Set(key, value)
}

// ServeFiles returns a Handler that serves static files from the specified http.FileSystem.
// It uses http.FileServer to handle requests for static files and returns a 404 error if the file is not found.
//
// Parameters:
// - path: An http.FileSystem representing the root directory from which static files will be served.
//
// Returns:
// - A Handler function that takes a Request and Response, serving the files
func ServeFiles(path http.FileSystem) Handler {
staticFileSystem := http.FileServer(path)
return func(req *Request, res *Response) error {
staticFileSystem.ServeHTTP(res.res, req.req)
return nil
}
}

const defaultStatusCode = http.StatusOK

// Writes the response with the provided byte slice as the body,
Expand Down

0 comments on commit 4a1d76b

Please sign in to comment.