Added ability to disable file serving if it is handled by some other service (e.g. NGiNX).
This commit is contained in:
parent
799d0b0cb1
commit
3b324a9733
@ -16,6 +16,7 @@ import (
|
||||
|
||||
var listenAddress *string = flag.String("listen", "/var/run/dwelling-files/f.sock", "listen address (ip:port|unix_path)")
|
||||
var directoryPath *string = flag.String("path", "/srv/ftp", "path to file share")
|
||||
var disableFileHandler *bool = flag.Bool("no-file-handling", false, "disable file handling if it is handled by something else (e.g. NGiNX)")
|
||||
var showVersion *bool = flag.Bool("v", false, "show version")
|
||||
|
||||
func main() {
|
||||
@ -27,7 +28,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
hand := handlers.New(directoryPath, web.Assets())
|
||||
hand := handlers.New(directoryPath, web.Assets(), *disableFileHandler)
|
||||
srv := server.NewHttpServer()
|
||||
|
||||
srv.GET("/*filepath", hand.Index)
|
||||
|
@ -25,13 +25,21 @@ type FilesHandlers struct {
|
||||
directoryPath string
|
||||
assetsServer http.Handler
|
||||
fileServer http.Handler
|
||||
noFileHandling bool
|
||||
}
|
||||
|
||||
func New(directoryPath *string, assetsFS http.FileSystem) *FilesHandlers {
|
||||
func New(directoryPath *string, assetsFS http.FileSystem, noFileHandling bool) *FilesHandlers {
|
||||
var fSrv http.Handler
|
||||
if noFileHandling {
|
||||
fSrv = nil
|
||||
} else {
|
||||
fSrv = http.FileServer(http.Dir(*directoryPath))
|
||||
}
|
||||
return &FilesHandlers{
|
||||
directoryPath: *directoryPath,
|
||||
assetsServer: http.FileServer(assetsFS),
|
||||
fileServer: http.FileServer(http.Dir(*directoryPath))}
|
||||
fileServer: fSrv,
|
||||
noFileHandling: noFileHandling}
|
||||
}
|
||||
|
||||
func (FilesHandlers) AssetsFS() http.FileSystem {
|
||||
@ -52,6 +60,10 @@ func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if strings.HasPrefix(path, "/file") {
|
||||
if h.noFileHandling {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
r.URL.Path = path[5:]
|
||||
h.fileServer.ServeHTTP(w, r)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user