Moved to my httpr router.
This commit is contained in:
parent
ed62b37dbc
commit
15af164462
@ -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)
|
||||
}
|
||||
|
2
go.mod
2
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
|
||||
|
4
go.sum
4
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=
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user