Disable directory listing for Hugo file server

Fix #1400
This commit is contained in:
Arun Sori 2015-10-23 21:51:37 +05:30 committed by Anthony Fok
parent 50c819cede
commit 83e46555dd
1 changed files with 22 additions and 1 deletions

View File

@ -49,6 +49,26 @@ serve them up.`,
//Run: server,
}
type filesOnlyFs struct {
fs http.FileSystem
}
type noDirFile struct {
http.File
}
func (fs filesOnlyFs) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return noDirFile{f}, nil
}
func (f noDirFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
func init() {
serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen")
serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
@ -125,7 +145,8 @@ func serve(port int) {
jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
fileserver := http.FileServer(httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))
fs := filesOnlyFs{httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))}
fileserver := http.FileServer(fs)
// We're only interested in the path
u, err := url.Parse(viper.GetString("BaseURL"))