Connor McCutcheon
/ Skykit
controller.go
go
package skykit
import "net/http"
type Handler interface {
	Setup(app *Application)
	Handle(r *http.Request) Handler
}
type Controller struct {
	*Application
	*http.Request
}
func (c *Controller) Setup(app *Application) {
	c.Application = app
}
func (c *Controller) Use(name string) Handler {
	other := c.Application.Use(name)
	if other == nil {
		return nil
	}
	return other.Handle(c.Request)
}
func (c *Controller) IsHTMX(r *http.Request) bool {
	return r.Header.Get("HX-Request") == "true"
}
func (c *Controller) Refresh(w http.ResponseWriter, r *http.Request) {
	if c.IsHTMX(r) {
		w.Header().Set("HX-Refresh", "true")
		w.WriteHeader(http.StatusNoContent)
		return
	}
	http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
}
func (c *Controller) Redirect(w http.ResponseWriter, r *http.Request, path string) {
	if c.IsHTMX(r) {
		w.Header().Set("HX-Location", path)
		w.WriteHeader(http.StatusNoContent)
		return
	}
	http.Redirect(w, r, path, http.StatusSeeOther)
}
No comments yet.