2023-08-14 18:14:08 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.fifitido.net/ytdl-web/app"
|
|
|
|
"go.fifitido.net/ytdl-web/pkg/htmx"
|
|
|
|
"go.fifitido.net/ytdl-web/pkg/server"
|
|
|
|
"go.fifitido.net/ytdl-web/pkg/view"
|
2023-08-14 18:16:42 -04:00
|
|
|
"go.fifitido.net/ytdl-web/pkg/ytdl"
|
2023-08-14 18:14:08 -04:00
|
|
|
"go.fifitido.net/ytdl-web/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HomeController struct {
|
|
|
|
ytdl ytdl.Ytdl
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ server.Controller = (*HomeController)(nil)
|
|
|
|
|
|
|
|
func NewHomeController(ytdl ytdl.Ytdl) *HomeController {
|
|
|
|
return &HomeController{
|
|
|
|
ytdl: ytdl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HomeController) Router(r chi.Router) {
|
|
|
|
r.Get("/", c.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HomeController) Index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
hx := htmx.New(w, r)
|
2023-08-16 15:17:22 -04:00
|
|
|
isSecure := r.URL.Scheme == "https" || r.Header.Get("X-Forwarded-Proto") == "https"
|
2023-08-14 18:14:08 -04:00
|
|
|
|
|
|
|
if hx.IsHtmxRequest() {
|
|
|
|
hx.PushUrl("/")
|
|
|
|
|
|
|
|
app.Views.Render(w, "index", view.Data{
|
|
|
|
"BasePath": viper.GetString("base_path"),
|
|
|
|
"Version": version.Version,
|
|
|
|
"Build": version.Build,
|
|
|
|
"BinaryVersion": c.ytdl.Version(),
|
2023-08-16 11:33:15 -04:00
|
|
|
"IsSecure": isSecure,
|
2023-08-14 18:14:08 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
app.Views.Render(w, "index", view.Data{
|
|
|
|
"BasePath": viper.GetString("base_path"),
|
|
|
|
"Version": version.Version,
|
|
|
|
"Build": version.Build,
|
|
|
|
"BinaryVersion": c.ytdl.Version(),
|
2023-08-16 11:33:15 -04:00
|
|
|
"IsSecure": isSecure,
|
2023-08-14 18:14:08 -04:00
|
|
|
}, "layouts/main")
|
|
|
|
}
|
|
|
|
}
|