Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add immutable option #126

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ type ScannedFile struct {
etag string
}

func createImmutableServer(s *Plugin) FileServer {
func createImmutableServer(s *Plugin) (FileServer, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing plugin pointer is a bad idea. Consider approach without that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like that?

var files map[string]ScannedFile

var scanner func(path string, info os.FileInfo, err error) error
scanner = func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
return err
}

if info.IsDir() {
Expand All @@ -121,7 +121,7 @@ func createImmutableServer(s *Plugin) FileServer {
file, openError := s.root.Open(path)

if openError != nil {
panic(openError)
return openError
}

var etag string
Expand All @@ -143,7 +143,7 @@ func createImmutableServer(s *Plugin) FileServer {
err := filepath.Walk(s.cfg.Dir, scanner)

if err != nil {
panic(err)
return nil, err
}

return func(s *Plugin, next http.Handler, w http.ResponseWriter, r *http.Request, fp string) {
Expand Down Expand Up @@ -175,7 +175,7 @@ func createImmutableServer(s *Plugin) FileServer {

// we passed all checks - serve the file
http.ServeContent(w, r, file.name, file.modTime, file.file)
}
}, nil
}

// Plugin serves static files. Potentially convert into middleware?
Expand Down Expand Up @@ -263,7 +263,12 @@ func (s *Plugin) Middleware(next http.Handler) http.Handler {
var server FileServer = server

if s.cfg.Immutable {
server = createImmutableServer(s)
immutableServer, err := createImmutableServer(s)
if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panic in Go is not the same as exception in PHP, for example, and should be avoided as much as possible. All data should be initialized (since you know the list of files to serve on the Init stage) on the Init stage. Middleware, on error should delete the buggy file from the list of the served files (or try to re-fetch).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. This way should be ok then?

}

server = immutableServer
}

// Define the http.HandlerFunc
Expand Down