From b265293bfcd98b021a7fc14bd8900f8dbaa4df89 Mon Sep 17 00:00:00 2001 From: abeltay Date: Fri, 2 Feb 2018 18:23:45 +0800 Subject: [PATCH] Add function to maintain backwards compatibility (#10) --- web/fileserver.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/web/fileserver.go b/web/fileserver.go index aeb2f04..d7415da 100644 --- a/web/fileserver.go +++ b/web/fileserver.go @@ -6,14 +6,22 @@ import ( "strings" ) -// FileServer creates a file server that serves files from from a "Root" folder. +// FileServerHandler creates a file server that serves files from from a "Root" folder. // It will call "NotFound" HandlerFunc if the path contains '..' or if the file cannot be found on the system. -type FileServer struct { +type FileServerHandler struct { Root string NotFound http.HandlerFunc } -func (f FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { +// FileServer creates a FileServer that returns http.NotFound if the file cannot be found on the system +func FileServer(root http.Dir) http.Handler { + return FileServerHandler{ + Root: string(root), + NotFound: http.NotFound, + } +} + +func (f FileServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if containsDotDot(r.URL.Path) { f.NotFound(w, r) return