35 lines
780 B
Go
35 lines
780 B
Go
package httpUtil
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
)
|
|
|
|
type HandlerWithError func(http.ResponseWriter, *http.Request) error
|
|
|
|
func HandleErrors(f HandlerWithError) http.HandlerFunc {
|
|
return func(rw http.ResponseWriter, rq *http.Request) {
|
|
if err := f(rw, rq); err != nil {
|
|
slog.Error("error in handler", "error", err, "path", rq.URL.Path)
|
|
}
|
|
}
|
|
}
|
|
|
|
const NextPageCookie = "backseat-next"
|
|
|
|
func RequestSpotifySignIn(rw http.ResponseWriter, rq *http.Request) {
|
|
http.SetCookie(rw, &http.Cookie{
|
|
Name: NextPageCookie,
|
|
Value: rq.URL.Path,
|
|
HttpOnly: true,
|
|
})
|
|
http.Redirect(rw, rq, "/spotify/oauth/outbound", http.StatusFound)
|
|
}
|
|
|
|
func GetNextURL(rq *http.Request) string {
|
|
next, err := rq.Cookie(NextPageCookie)
|
|
if err != nil {
|
|
return "/"
|
|
}
|
|
return next.Value
|
|
}
|