Most of the web is static files, so today you add a handler that reads a file off disk and returns its contents, turning your server into a real file server.
Serve the contents of a file under a root directory as the response body.
Static files are the bread and butter of an HTTP server. A file handler maps
the request path onto a file under a root directory, reads its bytes, and returns
them as the body. The path /index.html becomes root/index.html on disk.
Join the path onto the root carefully — cleaning it first collapses any ..
segments so a request cannot escape the root and read arbitrary files (path
traversal is a classic hole). Read the bytes and hand them back as a 200;
picking the right Content-Type and handling a missing file are the next two
steps.
full := filepath.Join(root, filepath.Clean("/"+r.Path))data, err := os.ReadFile(full)if err == nil {return Response{Status: 200, Body: data}}