1
0
dwelling-files/internal/http/handlers.go

59 lines
1.2 KiB
Go

package http
import (
"dwelling-files/pkg/files"
"dwelling-files/pkg/utils"
"dwelling-files/web"
"log"
"net/http"
"git.arav.su/Arav/httpr"
)
type FilesHandlers struct {
directoryPath string
fileServer http.Handler
noFileHandling bool
}
func New(directoryPath *string, noFileHandling bool) *FilesHandlers {
var fSrv http.Handler
if noFileHandling {
fSrv = nil
} else {
fSrv = http.FileServer(http.Dir(*directoryPath))
}
return &FilesHandlers{
directoryPath: *directoryPath,
fileServer: fSrv,
noFileHandling: noFileHandling}
}
func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
path := "/" + httpr.Param(r, "filepath") + "/"
currentPath := files.CurrentPath(path)
entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)
if err != nil {
log.Println(err)
return
}
web.Index(utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
}
func (h *FilesHandlers) File(w http.ResponseWriter, r *http.Request) {
if h.noFileHandling {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
r.URL.Path = httpr.Param(r, "filepath")
h.fileServer.ServeHTTP(w, r)
}
func RobotsTxt(w http.ResponseWriter, r *http.Request) {
fc, _ := web.AssetsGetFile("robots.txt")
w.Write(fc)
}