diff --git a/cmd/dwelling-files/main.go b/cmd/dwelling-files/main.go index 01c0d3f..888c516 100644 --- a/cmd/dwelling-files/main.go +++ b/cmd/dwelling-files/main.go @@ -1,16 +1,19 @@ package main import ( - "dwelling-files/internal/http" + dwhttp "dwelling-files/internal/http" "dwelling-files/web" "flag" "fmt" "log" + "net/http" "net/netip" "os" "os/signal" "strings" "syscall" + + "git.arav.su/Arav/httpr" ) var version string @@ -46,11 +49,19 @@ func main() { } } - hand := http.New(directoryPath, web.Assets(), !*enableFileHandler) - srv := http.NewHttpServer() + hand := dwhttp.New(directoryPath, !*enableFileHandler) + r := httpr.New() - srv.GET("/*filepath", hand.Index) + r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "lololol") + } + 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) if err := srv.Start(network, *listenAddress); err != nil { log.Fatalln(err) } diff --git a/go.mod b/go.mod index ad98933..c037b26 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module dwelling-files go 1.16 -require github.com/julienschmidt/httprouter v1.3.0 +require git.arav.su/Arav/httpr v0.2.0 diff --git a/go.sum b/go.sum index 096c54e..8af06aa 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= -github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= +git.arav.su/Arav/httpr v0.2.0 h1:rtwUVl4ZDfvMf9DeLktxvups5GDY0ARVcUuUHR7Eb9E= +git.arav.su/Arav/httpr v0.2.0/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU= diff --git a/internal/http/handlers.go b/internal/http/handlers.go index d8ff164..36d0c54 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -6,19 +6,17 @@ import ( "dwelling-files/web" "log" "net/http" - "strings" - "github.com/julienschmidt/httprouter" + "git.arav.su/Arav/httpr" ) type FilesHandlers struct { directoryPath string - assetsServer http.Handler fileServer http.Handler noFileHandling bool } -func New(directoryPath *string, assetsFS http.FileSystem, noFileHandling bool) *FilesHandlers { +func New(directoryPath *string, noFileHandling bool) *FilesHandlers { var fSrv http.Handler if noFileHandling { fSrv = nil @@ -27,38 +25,12 @@ func New(directoryPath *string, assetsFS http.FileSystem, noFileHandling bool) * } return &FilesHandlers{ directoryPath: *directoryPath, - assetsServer: http.FileServer(assetsFS), fileServer: fSrv, noFileHandling: noFileHandling} } -func (FilesHandlers) AssetsFS() http.FileSystem { - return web.Assets() -} - func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) { - path := httprouter.CleanPath(GetURLParam(r, "filepath")) - - if strings.HasPrefix(path, "/assets") { - h.assetsServer.ServeHTTP(w, r) - return - } - - if strings.HasPrefix(path, "/robots.txt") { - fc, _ := web.AssetsGetFile("robots.txt") - w.Write(fc) - return - } - - if strings.HasPrefix(path, "/file") { - if h.noFileHandling { - w.WriteHeader(http.StatusServiceUnavailable) - return - } - r.URL.Path = path[5:] - h.fileServer.ServeHTTP(w, r) - return - } + path := "/" + httpr.Param(r, "filepath") + "/" currentPath := files.CurrentPath(path) @@ -70,3 +42,17 @@ func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) { 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) +} diff --git a/internal/http/http.go b/internal/http/http.go index 58c3af4..7a82e8b 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -7,34 +7,17 @@ import ( "net/http" "os" "time" - - "github.com/julienschmidt/httprouter" ) type HttpServer struct { - server *http.Server - router *httprouter.Router + s http.Server } -func NewHttpServer() *HttpServer { - r := httprouter.New() - return &HttpServer{ - server: &http.Server{ - ReadTimeout: 3 * time.Second, - WriteTimeout: 3 * time.Second, - Handler: r, - }, - router: r, - } -} - -func (s *HttpServer) GET(path string, handler http.HandlerFunc) { - s.router.Handler(http.MethodGet, path, handler) -} - -// GetURLParam wrapper around underlying router for getting URL parameters. -func GetURLParam(r *http.Request, param string) string { - return httprouter.ParamsFromContext(r.Context()).ByName(param) +func NewHttpServer(r http.Handler) *HttpServer { + return &HttpServer{s: http.Server{ + ReadTimeout: 3 * time.Second, + WriteTimeout: 3 * time.Second, + Handler: r}} } func (s *HttpServer) Start(network, address string) error { @@ -48,7 +31,7 @@ func (s *HttpServer) Start(network, address string) error { } go func() { - if err = s.server.Serve(listener); err != nil && err != http.ErrServerClosed { + if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed { log.Fatalln(err) } }() @@ -60,7 +43,7 @@ func (s *HttpServer) Stop() error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if err := s.server.Shutdown(ctx); err != nil { + if err := s.s.Shutdown(ctx); err != nil { return err } diff --git a/web/web.go b/web/web.go index 8c8ea55..c068d64 100644 --- a/web/web.go +++ b/web/web.go @@ -2,6 +2,7 @@ package web import ( "embed" + "io/fs" "net/http" ) @@ -11,7 +12,8 @@ import ( var assetsDir embed.FS func Assets() http.FileSystem { - return http.FS(assetsDir) + f, _ := fs.Sub(assetsDir, "assets") + return http.FS(f) } func AssetsGetFile(path string) ([]byte, error) {