1
0
Fork 0

Made use of my own httpr router in main.

This commit is contained in:
Alexander Andreev 2023-05-26 04:30:29 +04:00
parent b18338e4e1
commit 38e6813d11
Signed by: Arav
GPG Key ID: D22A817D95815393
1 changed files with 17 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import (
"dwelling-upload/internal/http"
"dwelling-upload/pkg/utils"
"dwelling-upload/pkg/watcher"
"dwelling-upload/web"
"flag"
"fmt"
"log"
@ -14,6 +15,8 @@ import (
"path"
"strings"
"syscall"
"git.arav.su/Arav/httpr"
)
var listenAddress *string = flag.String("listen", "/var/run/dwelling-upload/sock", "listen address (ip:port|unix_path)")
@ -88,21 +91,24 @@ func main() {
hand := http.NewUploadHandlers(logFile, *uploadDir, &uploadDirSize, string(hashSalt),
*keepFileForHours, *limitStorage, *limitFileSize)
srv := http.NewHttpServer()
srv.SetNotFoundHandler(func(w nethttp.ResponseWriter, r *nethttp.Request) {
r := httpr.New()
r.NotFoundHandler = func(w nethttp.ResponseWriter, r *nethttp.Request) {
http.Error(w, r, nethttp.StatusNotFound, "")
})
}
srv.ServeStatic("/assets/*filepath", http.AssetsFS())
srv.GET("/robots.txt", http.RobotsTxt)
srv.GET("/favicon.svg", http.Favicon)
r.Handler(nethttp.MethodGet, "/", hand.Index)
r.Handler(nethttp.MethodPost, "/", hand.Upload)
r.Handler(nethttp.MethodGet, "/:hash/:name", hand.Download)
r.Handler(nethttp.MethodPost, "/delete", hand.Delete)
r.Handler(nethttp.MethodDelete, "/:hash", hand.Delete)
srv.GET("/", hand.Index)
srv.POST("/", hand.Upload)
srv.POST("/delete", hand.Delete)
srv.GET("/f/:hash/:name", hand.Download)
srv.DELETE("/:hash", hand.Delete)
r.ServeStatic("/assets/*filepath", web.Assets())
r.Handler(nethttp.MethodGet, "/robots.txt", http.RobotsTxt)
r.Handler(nethttp.MethodGet, "/favicon.svg", http.Favicon)
srv := http.NewHttpServer(r)
if err := srv.Start(network, *listenAddress); err != nil {
log.Fatalln("failed to start a server:", err)