Disable directory listing for Hugo file server - hugo - [fork] hugo port for 9front
(HTM) git clone git@git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
(DIR) commit 83e46555dd15aa6082b17f9ebbf9f12eb1cf64d8
(DIR) parent 50c819cedee34c09d21f46def8a16d435d7271d1
(HTM) Author: Arun Sori <arunsori94@gmail.com>
Date: Fri, 23 Oct 2015 21:51:37 +0530
Disable directory listing for Hugo file server
Fix #1400
Diffstat:
M commands/server.go | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/commands/server.go b/commands/server.go
@@ -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"))