Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Create custom http filesystem (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
kochman authored and garoller committed Oct 10, 2018
1 parent 421137f commit ea7775f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api.go
Expand Up @@ -120,7 +120,7 @@ func New(cfg Config, ms shuttletracker.ModelService, msg shuttletracker.MessageS

// Static files
r.Get("/", IndexHandler)
r.Method("GET", "/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
r.Method("GET", "/static/*", http.StripPrefix("/static/", http.FileServer(staticFileSystem{http.Dir("static/")})))

// iTRAK data feed endpoint
r.Get("/datafeed", api.DataFeedHandler)
Expand Down
28 changes: 28 additions & 0 deletions api/filesystem.go
@@ -0,0 +1,28 @@
package api

import (
"net/http"
"os"
)

type staticFileSystem struct {
fs http.FileSystem
}

// Open will only return Files that are not directories.
func (sfs staticFileSystem) Open(path string) (http.File, error) {
f, err := sfs.fs.Open(path)
if err != nil {
return nil, err
}

s, err := f.Stat()
if err != nil {
return nil, err
}
if s.IsDir() {
return nil, os.ErrNotExist
}

return f, nil
}

0 comments on commit ea7775f

Please sign in to comment.