ytdl-web/web/serve.go

54 lines
1.2 KiB
Go
Raw Normal View History

2023-04-14 11:58:32 -04:00
package web
import (
2023-04-14 16:07:57 -04:00
"net/url"
"sort"
2023-04-14 11:58:32 -04:00
"github.com/gofiber/fiber/v2"
2023-04-14 16:07:57 -04:00
"github.com/samber/lo"
"go.fifitido.net/ytdl-web/ytdl"
"golang.org/x/exp/slog"
2023-04-14 11:58:32 -04:00
)
func Serve() error {
engine := ViewsEngine()
app := fiber.New(fiber.Config{Views: engine})
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("views/index", fiber.Map{}, "views/layouts/main")
})
app.Get("/download", func(c *fiber.Ctx) error {
2023-04-14 16:07:57 -04:00
urlBytes, err := url.QueryUnescape(c.Query("url"))
if err != nil {
slog.Error("Failed to decode url param", slog.String("error", err.Error()))
return fiber.ErrBadRequest
}
url := string(urlBytes)
meta, err := ytdl.GetMetadata(url)
if err != nil {
slog.Error("Failed to get metadata", slog.String("error", err.Error()))
return fiber.ErrInternalServerError
}
formats := lo.Filter(meta.Formats, func(item ytdl.Format, _ int) bool {
return item.ACodec != "none" && item.VCodec != "none"
})
sort.Slice(formats, func(i, j int) bool {
return formats[i].Width > formats[j].Width
})
2023-04-14 11:58:32 -04:00
return c.Render("views/download", fiber.Map{
2023-04-14 16:07:57 -04:00
"Url": url, "Meta": meta, "Formats": formats,
2023-04-14 11:58:32 -04:00
}, "views/layouts/main")
})
app.Get("/download/stream", func(c *fiber.Ctx) error {
return nil
})
return app.Listen(":8080")
}