tweaks
This commit is contained in:
@@ -42,6 +42,17 @@ func throwInternalServiceError(
|
||||
throwError(s, w, r, http.StatusInternalServerError, msg, err, "error")
|
||||
}
|
||||
|
||||
// throwServiceUnavailable handles 503 errors
|
||||
func throwServiceUnavailable(
|
||||
s *hws.Server,
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
msg string,
|
||||
err error,
|
||||
) {
|
||||
throwError(s, w, r, http.StatusServiceUnavailable, msg, err, "error")
|
||||
}
|
||||
|
||||
// throwBadRequest handles 400 errors (malformed requests)
|
||||
func throwBadRequest(
|
||||
s *hws.Server,
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"net/http"
|
||||
|
||||
"git.haelnorr.com/h/golib/cookies"
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/config"
|
||||
"git.haelnorr.com/h/oslstats/internal/discord"
|
||||
@@ -13,16 +15,34 @@ import (
|
||||
"git.haelnorr.com/h/oslstats/pkg/oauth"
|
||||
)
|
||||
|
||||
func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *discord.APIClient) http.Handler {
|
||||
func Login(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
cfg *config.Config,
|
||||
st *store.Store,
|
||||
discordAPI *discord.APIClient,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: check DB is connected
|
||||
// check discord API is working
|
||||
errDB := conn.Ping()
|
||||
_, errDisc := discordAPI.Ping()
|
||||
err := stderrors.Join(errors.Wrap(errDB, "conn.Ping"), errors.Wrap(errDisc, "discordAPI.Ping"))
|
||||
err = errors.Wrap(err, "login error")
|
||||
|
||||
if r.Method == "POST" {
|
||||
// if either fail, notify the client that login is unavailable right now
|
||||
// otherwise proceed redirect to GET method
|
||||
if err != nil {
|
||||
notifyServiceUnavailable(s, r, "Login currently unavailable", err)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
throwServiceUnavailable(s, w, r, "Login currently unavailable", err)
|
||||
return
|
||||
}
|
||||
// if either fail and method is GET, show service not available page
|
||||
cookies.SetPageFrom(w, r, cfg.HWSAuth.TrustedHost)
|
||||
attempts, exceeded, track := st.TrackRedirect(r, "/login", 5)
|
||||
|
||||
@@ -39,7 +59,7 @@ func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *
|
||||
st.ClearRedirectTrack(r, "/login")
|
||||
|
||||
throwError(
|
||||
server,
|
||||
s,
|
||||
w,
|
||||
r,
|
||||
http.StatusBadRequest,
|
||||
@@ -52,14 +72,14 @@ func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *
|
||||
|
||||
state, uak, err := oauth.GenerateState(cfg.OAuth, "login")
|
||||
if err != nil {
|
||||
throwInternalServiceError(server, w, r, "Failed to generate state token", err)
|
||||
throwInternalServiceError(s, w, r, "Failed to generate state token", err)
|
||||
return
|
||||
}
|
||||
oauth.SetStateCookie(w, uak, cfg.HWSAuth.SSL)
|
||||
|
||||
link, err := discordAPI.GetOAuthLink(state)
|
||||
if err != nil {
|
||||
throwInternalServiceError(server, w, r, "An error occurred trying to generate the login link", err)
|
||||
throwInternalServiceError(s, w, r, "An error occurred trying to generate the login link", err)
|
||||
return
|
||||
}
|
||||
st.ClearRedirectTrack(r, "/login")
|
||||
|
||||
@@ -37,6 +37,11 @@ func notifyInternalServiceError(s *hws.Server, r *http.Request, msg string, err
|
||||
SerializeErrorDetails(http.StatusInternalServerError, err), nil)
|
||||
}
|
||||
|
||||
func notifyServiceUnavailable(s *hws.Server, r *http.Request, msg string, err error) error {
|
||||
return notifyClient(s, r, notify.LevelError, "Service Unavailable", msg,
|
||||
SerializeErrorDetails(http.StatusServiceUnavailable, err), nil)
|
||||
}
|
||||
|
||||
func notifyWarn(s *hws.Server, r *http.Request, title, msg string, action any) error {
|
||||
return notifyClient(s, r, notify.LevelWarn, title, msg, "", action)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user