1
0

Added ability to disable file serving if it is handled by some other service (e.g. NGiNX).

This commit is contained in:
Alexander Andreev 2022-06-28 01:07:46 +04:00
parent 799d0b0cb1
commit 3b324a9733
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
2 changed files with 21 additions and 8 deletions

View File

@ -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)

View File

@ -22,16 +22,24 @@ type IndexData struct {
}
type FilesHandlers struct {
directoryPath string
assetsServer http.Handler
fileServer http.Handler
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))}
directoryPath: *directoryPath,
assetsServer: http.FileServer(assetsFS),
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