diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 83776f6..7741eb0 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -7,6 +7,7 @@ import ( "dwelling-files/web" "log" "net/http" + "strings" "github.com/julienschmidt/httprouter" ) @@ -22,11 +23,15 @@ type IndexData struct { type FilesHandlers struct { directoryPath string + assetsServer http.Handler + fileServer http.Handler } -func New(directoryPath *string) *FilesHandlers { +func New(directoryPath *string, assetsFS http.FileSystem) *FilesHandlers { return &FilesHandlers{ - directoryPath: *directoryPath} + directoryPath: *directoryPath, + assetsServer: http.FileServer(assetsFS), + fileServer: http.FileServer(http.Dir(*directoryPath))} } func (FilesHandlers) AssetsFS() http.FileSystem { @@ -41,6 +46,17 @@ func (FilesHandlers) Robots(w http.ResponseWriter, r *http.Request) { func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) { path := httprouter.CleanPath(server.GetURLParam(r, "filepath")) + if strings.HasPrefix(path, "/assets") { + h.assetsServer.ServeHTTP(w, r) + return + } + + if strings.HasPrefix(path, "/file") { + r.URL.Path = path[5:] + h.fileServer.ServeHTTP(w, r) + return + } + currentPath := files.CurrentPath(path) entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)