update to new webserver module
This commit is contained in:
@@ -57,7 +57,14 @@ func ChangeUsername(
|
||||
// Start the transaction
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating username", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Message: "Error updating username",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
r.ParseForm()
|
||||
@@ -65,7 +72,14 @@ func ChangeUsername(
|
||||
unique, err := models.CheckUsernameUnique(tx, newUsername)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Error updating username",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !unique {
|
||||
@@ -78,7 +92,14 @@ func ChangeUsername(
|
||||
err = user.ChangeUsername(tx, newUsername)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating username", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Error updating username",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
@@ -101,7 +122,14 @@ func ChangeBio(
|
||||
// Start the transaction
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating bio", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Message: "Error updating bio",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
r.ParseForm()
|
||||
@@ -117,7 +145,14 @@ func ChangeBio(
|
||||
err = user.ChangeBio(tx, newBio)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating bio", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Error updating bio",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
@@ -154,7 +189,14 @@ func ChangePassword(
|
||||
// Start the transaction
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Error updating password", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Message: "Error updating password",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
newPass, err := validateChangePassword(r)
|
||||
@@ -167,7 +209,14 @@ func ChangePassword(
|
||||
err = user.SetPassword(tx, newPass)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Error updating password", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Error updating password",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
@@ -3,14 +3,16 @@ package handler
|
||||
import (
|
||||
"net/http"
|
||||
"projectreshoot/internal/view/page"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func ErrorPage(
|
||||
errorCode int,
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
message := map[int]string{
|
||||
) (hws.ErrorPage, error) {
|
||||
messages := map[int]string{
|
||||
400: "The request you made was malformed or unexpected.",
|
||||
401: "You need to login to view this page.",
|
||||
403: "You do not have permission to view this page.",
|
||||
404: "The page or resource you have requested does not exist.",
|
||||
@@ -18,25 +20,9 @@ func ErrorPage(
|
||||
continues to happen contact an administrator.`,
|
||||
503: "The server is currently down for maintenance and should be back soon. =)",
|
||||
}
|
||||
w.WriteHeader(errorCode)
|
||||
page.Error(errorCode, http.StatusText(errorCode), message[errorCode]).
|
||||
Render(r.Context(), w)
|
||||
}
|
||||
|
||||
func NewErrorPage(
|
||||
errorCode int,
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) error {
|
||||
message := map[int]string{
|
||||
401: "You need to login to view this page.",
|
||||
403: "You do not have permission to view this page.",
|
||||
404: "The page or resource you have requested does not exist.",
|
||||
500: `An error occured on the server. Please try again, and if this
|
||||
continues to happen contact an administrator.`,
|
||||
503: "The server is currently down for maintenance and should be back soon. =)",
|
||||
msg, exists := messages[errorCode]
|
||||
if !exists {
|
||||
return nil, errors.New("No valid message for the given code")
|
||||
}
|
||||
w.WriteHeader(errorCode)
|
||||
return page.Error(errorCode, http.StatusText(errorCode), message[errorCode]).
|
||||
Render(r.Context(), w)
|
||||
return page.Error(errorCode, http.StatusText(errorCode), msg), nil
|
||||
}
|
||||
|
||||
@@ -12,7 +12,14 @@ func Root() http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
ErrorPage(http.StatusNotFound, w, r)
|
||||
page, err := ErrorPage(http.StatusNotFound)
|
||||
if err != nil {
|
||||
// TODO: add logger for this
|
||||
}
|
||||
err = page.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
// TODO: add logger for this
|
||||
}
|
||||
return
|
||||
}
|
||||
page.Index().Render(r.Context(), w)
|
||||
|
||||
@@ -66,7 +66,14 @@ func LoginRequest(
|
||||
// Start the transaction
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusServiceUnavailable, "Login failed", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Message: "Login failed",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
r.ParseForm()
|
||||
@@ -74,9 +81,16 @@ func LoginRequest(
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
if err.Error() != "Username or password incorrect" {
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Login failed", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Login failed",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
} else {
|
||||
form.LoginForm(err.Error()).Render(r.Context(), w)
|
||||
form.LoginForm("Username or password incorrect").Render(r.Context(), w)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -85,7 +99,14 @@ func LoginRequest(
|
||||
err = auth.Login(w, r, user, rememberMe)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
server.ThrowWarn(w, hws.NewError(http.StatusInternalServerError, "Login failed", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Login failed",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -23,14 +23,28 @@ func Logout(
|
||||
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Logout failed", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Logout failed",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
err = auth.Logout(tx, w, r)
|
||||
if err != nil {
|
||||
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Logout failed", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Logout failed",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
@@ -6,36 +6,53 @@ import (
|
||||
"projectreshoot/internal/view/page"
|
||||
"strconv"
|
||||
|
||||
"git.haelnorr.com/h/golib/hlog"
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/golib/tmdb"
|
||||
)
|
||||
|
||||
func Movie(
|
||||
server *hws.Server,
|
||||
config *config.Config,
|
||||
logger *hlog.Logger,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("movie_id")
|
||||
movie_id, err := strconv.ParseInt(id, 10, 32)
|
||||
if err != nil {
|
||||
ErrorPage(http.StatusNotFound, w, r)
|
||||
logger.Error().Err(err).Str("movie_id", id).
|
||||
Msg("Error occured getting the movie")
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Message: "Movie ID provided is not valid",
|
||||
Error: err,
|
||||
Level: hws.ErrorDEBUG,
|
||||
RenderErrorPage: true,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
movie, err := tmdb.GetMovie(int32(movie_id), config.TMDBToken)
|
||||
if err != nil {
|
||||
ErrorPage(http.StatusInternalServerError, w, r)
|
||||
logger.Error().Err(err).Int32("movie_id", int32(movie_id)).
|
||||
Msg("Error occured getting the movie")
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "An error occured when trying to retrieve the requested movie",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
credits, err := tmdb.GetCredits(int32(movie_id), config.TMDBToken)
|
||||
if err != nil {
|
||||
ErrorPage(http.StatusInternalServerError, w, r)
|
||||
logger.Error().Err(err).Int32("movie_id", int32(movie_id)).
|
||||
Msg("Error occured getting the movie credits")
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "An error occured when trying to retrieve the credits for the requested movie",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
page.Movie(movie, credits, &config.TMDBConfig.Image).Render(r.Context(), w)
|
||||
|
||||
@@ -44,7 +44,14 @@ func Reauthenticate(
|
||||
// Start the transaction
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Failed to start transaction", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Failed to start transcation",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
@@ -56,7 +63,14 @@ func Reauthenticate(
|
||||
}
|
||||
err = auth.RefreshAuthTokens(tx, w, r)
|
||||
if err != nil {
|
||||
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Failed to refresh user tokens", err))
|
||||
err := server.ThrowError(w, r, hws.HWSError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "Failed to refresh user tokens",
|
||||
Error: err,
|
||||
})
|
||||
if err != nil {
|
||||
server.ThrowFatal(w, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
Reference in New Issue
Block a user