streamline response handling and logging
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
+27
-20
@@ -23,7 +23,7 @@ func (h *ApiHandler) ServeLoginPost(writer http.ResponseWriter, request *http.Re
|
||||
body, err := io.ReadAll(bodyReader)
|
||||
_ = bodyReader.Close()
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "failed to read body", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -34,19 +34,19 @@ func (h *ApiHandler) ServeLoginPost(writer http.ResponseWriter, request *http.Re
|
||||
loginBody := LoginBody{}
|
||||
err = json.Unmarshal(body, &loginBody)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "failed to read body", err)
|
||||
return
|
||||
}
|
||||
|
||||
success, err := h.db.ValidateRootPassword(loginBody.Password)
|
||||
if err != nil {
|
||||
log.Println("Error logging in:", err)
|
||||
http.Error(writer, "failed to read database", http.StatusInternalServerError)
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to read database", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !success {
|
||||
http.Error(writer, "invalid password", http.StatusUnauthorized)
|
||||
log.Printf("failed login from '%s'", request.RemoteAddr)
|
||||
WriteError(writer, http.StatusUnauthorized, "invalid password", nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -62,14 +62,9 @@ func (h *ApiHandler) ServeLoginPost(writer http.ResponseWriter, request *http.Re
|
||||
cookie.HttpOnly = true
|
||||
http.SetCookie(writer, &cookie)
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(writer).Encode(map[string]interface{}{})
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to serialize results", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
WriteResponse(writer, http.StatusOK, map[string]interface{}{})
|
||||
|
||||
log.Printf("successful login from '%s'", request.RemoteAddr)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeLogoutPost(writer http.ResponseWriter, request *http.Request) {
|
||||
@@ -85,13 +80,9 @@ func (h *ApiHandler) ServeLogoutPost(writer http.ResponseWriter, request *http.R
|
||||
|
||||
h.authToken = nil
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
err := json.NewEncoder(writer).Encode(map[string]interface{}{})
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to serialize results", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
WriteResponse(writer, http.StatusOK, map[string]interface{}{})
|
||||
|
||||
log.Printf("successful logout from '%s'", request.RemoteAddr)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ProcessAuth(next http.Handler, required bool) http.Handler {
|
||||
@@ -103,7 +94,7 @@ func (h *ApiHandler) ProcessAuth(next http.Handler, required bool) http.Handler
|
||||
}
|
||||
|
||||
if !isAuthorized && required {
|
||||
http.Error(writer, "authentication required", http.StatusUnauthorized)
|
||||
WriteError(writer, http.StatusUnauthorized, "authentication required", nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -115,3 +106,19 @@ func IsAuthorized(request *http.Request) bool {
|
||||
value := request.Context().Value(isAuthorizedContextKey)
|
||||
return value != nil && value.(bool)
|
||||
}
|
||||
|
||||
func WriteResponse(writer http.ResponseWriter, code int, body any) {
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(code)
|
||||
_ = json.NewEncoder(writer).Encode(body)
|
||||
}
|
||||
|
||||
func WriteError(writer http.ResponseWriter, code int, message string, err error) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
WriteResponse(writer, code, map[string]interface{}{
|
||||
"message": message,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user