Moved to my httpr router.
This commit is contained in:
parent
ed62b37dbc
commit
15af164462
@ -1,16 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"dwelling-files/internal/http"
|
dwhttp "dwelling-files/internal/http"
|
||||||
"dwelling-files/web"
|
"dwelling-files/web"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.arav.su/Arav/httpr"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version string
|
var version string
|
||||||
@ -46,11 +49,19 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hand := http.New(directoryPath, web.Assets(), !*enableFileHandler)
|
hand := dwhttp.New(directoryPath, !*enableFileHandler)
|
||||||
srv := http.NewHttpServer()
|
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 {
|
if err := srv.Start(network, *listenAddress); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module dwelling-files
|
|||||||
|
|
||||||
go 1.16
|
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=
|
git.arav.su/Arav/httpr v0.2.0 h1:rtwUVl4ZDfvMf9DeLktxvups5GDY0ARVcUuUHR7Eb9E=
|
||||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
git.arav.su/Arav/httpr v0.2.0/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
|
||||||
|
@ -6,19 +6,17 @@ import (
|
|||||||
"dwelling-files/web"
|
"dwelling-files/web"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"git.arav.su/Arav/httpr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FilesHandlers struct {
|
type FilesHandlers struct {
|
||||||
directoryPath string
|
directoryPath string
|
||||||
assetsServer http.Handler
|
|
||||||
fileServer http.Handler
|
fileServer http.Handler
|
||||||
noFileHandling bool
|
noFileHandling bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(directoryPath *string, assetsFS http.FileSystem, noFileHandling bool) *FilesHandlers {
|
func New(directoryPath *string, noFileHandling bool) *FilesHandlers {
|
||||||
var fSrv http.Handler
|
var fSrv http.Handler
|
||||||
if noFileHandling {
|
if noFileHandling {
|
||||||
fSrv = nil
|
fSrv = nil
|
||||||
@ -27,38 +25,12 @@ func New(directoryPath *string, assetsFS http.FileSystem, noFileHandling bool) *
|
|||||||
}
|
}
|
||||||
return &FilesHandlers{
|
return &FilesHandlers{
|
||||||
directoryPath: *directoryPath,
|
directoryPath: *directoryPath,
|
||||||
assetsServer: http.FileServer(assetsFS),
|
|
||||||
fileServer: fSrv,
|
fileServer: fSrv,
|
||||||
noFileHandling: noFileHandling}
|
noFileHandling: noFileHandling}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (FilesHandlers) AssetsFS() http.FileSystem {
|
|
||||||
return web.Assets()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||||
path := httprouter.CleanPath(GetURLParam(r, "filepath"))
|
path := "/" + httpr.Param(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
|
|
||||||
}
|
|
||||||
|
|
||||||
currentPath := files.CurrentPath(path)
|
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)
|
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"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HttpServer struct {
|
type HttpServer struct {
|
||||||
server *http.Server
|
s http.Server
|
||||||
router *httprouter.Router
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHttpServer() *HttpServer {
|
func NewHttpServer(r http.Handler) *HttpServer {
|
||||||
r := httprouter.New()
|
return &HttpServer{s: http.Server{
|
||||||
return &HttpServer{
|
ReadTimeout: 3 * time.Second,
|
||||||
server: &http.Server{
|
WriteTimeout: 3 * time.Second,
|
||||||
ReadTimeout: 3 * time.Second,
|
Handler: r}}
|
||||||
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 (s *HttpServer) Start(network, address string) error {
|
func (s *HttpServer) Start(network, address string) error {
|
||||||
@ -48,7 +31,7 @@ func (s *HttpServer) Start(network, address string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
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)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -60,7 +43,7 @@ func (s *HttpServer) Stop() error {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := s.server.Shutdown(ctx); err != nil {
|
if err := s.s.Shutdown(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,7 +12,8 @@ import (
|
|||||||
var assetsDir embed.FS
|
var assetsDir embed.FS
|
||||||
|
|
||||||
func Assets() http.FileSystem {
|
func Assets() http.FileSystem {
|
||||||
return http.FS(assetsDir)
|
f, _ := fs.Sub(assetsDir, "assets")
|
||||||
|
return http.FS(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssetsGetFile(path string) ([]byte, error) {
|
func AssetsGetFile(path string) ([]byte, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user