From cbf7738367d78b44063928ea6c7bfb023c170d5f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 8 Dec 2024 04:09:12 +0400 Subject: [PATCH] Handlers were moved to main.go. --- cmd/dwelling-files/main.go | 69 ++++++++++++++++++++++++++++++++++---- internal/http/handlers.go | 68 ------------------------------------- 2 files changed, 63 insertions(+), 74 deletions(-) delete mode 100644 internal/http/handlers.go diff --git a/cmd/dwelling-files/main.go b/cmd/dwelling-files/main.go index 857f576..7df7572 100644 --- a/cmd/dwelling-files/main.go +++ b/cmd/dwelling-files/main.go @@ -2,7 +2,9 @@ package main import ( dwhttp "dwelling-files/internal/http" + "dwelling-files/pkg/files" "dwelling-files/web" + "dwelling-files/web/locales" "flag" "fmt" "log" @@ -12,6 +14,7 @@ import ( "syscall" "git.arav.su/Arav/httpr" + "github.com/invopop/ctxi18n" ) var version string @@ -26,19 +29,35 @@ func main() { log.SetFlags(0) if *showVersion { - fmt.Println("dwelling-files ver.", version, "\nCopyright (c) 2023 Alexander \"Arav\" Andreev ") + fmt.Println("dwelling-files ver.", version, "\nCopyright (c) 2023,2024 Alexander \"Arav\" Andreev ") return } - hand := dwhttp.New(directoryPath, !*enableFileHandler) + if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil { + log.Fatalln(err) + } + r := httpr.New() + r.Handler(http.MethodGet, "/", Index) + r.Handler(http.MethodGet, "/*filepath", Index) r.ServeStatic("/assets/*filepath", web.Assets()) - r.Handler(http.MethodGet, "/file/*filepath", hand.File) - r.Handler(http.MethodGet, "/*filepath", hand.Index) - r.Handler(http.MethodGet, "/", hand.Index) - srv := dwhttp.NewHttpServer(r) + var fileServer http.Handler + if *enableFileHandler { + fileServer = http.FileServer(http.Dir(*directoryPath)) + } + r.Handler(http.MethodGet, "/file/*filepath", func(w http.ResponseWriter, r *http.Request) { + if !*enableFileHandler { + http.Error(w, "File handling is turned off.", http.StatusServiceUnavailable) + return + } + + r.URL.Path = httpr.Param(r, "filepath") + fileServer.ServeHTTP(w, r) + }) + + srv := dwhttp.NewHttpServer(I18nMiddleware(r)) if err := srv.Start(*listenAddress); err != nil { log.Fatalln(err) } @@ -52,3 +71,41 @@ func main() { log.Fatalln(err) } } + +func I18nMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + lang := "en" + + if lq := r.URL.Query().Get("lang"); lq != "" { + lc := http.Cookie{Name: "lang", Value: lq, HttpOnly: false, MaxAge: 0} + http.SetCookie(w, &lc) + lang = lq + } else if l, err := r.Cookie("lang"); err == nil { + lang = l.Value + } else if al := r.Header.Get("Accept-Language"); al != "" { + lang = r.Header.Get("Accept-Language") + } + + ctx, err := ctxi18n.WithLocale(r.Context(), lang) + if err != nil { + log.Println("i18nmw:", err) + } + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +func Index(w http.ResponseWriter, r *http.Request) { + path := "/" + httpr.Param(r, "filepath") + if path[len(path)-1] != '/' { + path += "/" + } + + entries, stats, err := files.ScanDirectory(*directoryPath+path, path) + if err != nil { + log.Println("Error directory scan:", err) + http.Error(w, "Not found", http.StatusNotFound) + return + } + + web.Index(files.CurrentPath(path), version, &stats, &entries, r).Render(r.Context(), w) +} diff --git a/internal/http/handlers.go b/internal/http/handlers.go deleted file mode 100644 index 6171a0c..0000000 --- a/internal/http/handlers.go +++ /dev/null @@ -1,68 +0,0 @@ -package http - -import ( - "dwelling-files/pkg/files" - "dwelling-files/pkg/utils" - "dwelling-files/web" - "log" - "net/http" - "strings" - - "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") - if !strings.HasSuffix(path, "/") { - path += "/" - } - - currentPath := files.CurrentPath(path) - - entries, stats, err := files.ScanDirectory(h.directoryPath+path, path) - if err != nil { - log.Println(err) - Error(w, http.StatusNotFound, "", "", r.Referer()) - return - } - - web.Index("Files", utils.MainSite(r.Host), currentPath, &stats, &entries, r, w) -} - -func (h *FilesHandlers) File(w http.ResponseWriter, r *http.Request) { - if h.noFileHandling { - Error(w, http.StatusServiceUnavailable, "File handling is turned off.", "", r.Referer()) - 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) -} - -func Error(w http.ResponseWriter, code int, reason, message, referer string) { - w.WriteHeader(code) - web.ErrorXXX("/ "+http.StatusText(code), reason, message, referer, code, w) -}