Handlers were moved to main.go.
This commit is contained in:
parent
df3ea75ca9
commit
cbf7738367
@ -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 <me@arav.su>")
|
||||
fmt.Println("dwelling-files ver.", version, "\nCopyright (c) 2023,2024 Alexander \"Arav\" Andreev <me@arav.su>")
|
||||
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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user