Start refactoring

This commit is contained in:
Aurélie Delhaie
2023-05-29 17:44:50 +02:00
parent 55ac50f3be
commit c06843cd28
31 changed files with 1125 additions and 1267 deletions

View File

@@ -15,15 +15,9 @@ type httpError struct {
Path string `json:"path"`
}
type successMessage struct {
Status int `json:"status"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
}
func internalServerError(w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: 500,
Status: http.StatusInternalServerError,
Error: "Internal Server Error",
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
Path: r.RequestURI,
@@ -35,7 +29,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
@@ -44,7 +38,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
func notFound(message string, w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: 404,
Status: http.StatusNotFound,
Error: "Not Found",
Message: message,
Path: r.RequestURI,
@@ -56,7 +50,28 @@ func notFound(message string, w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(404)
w.WriteHeader(http.StatusNotFound)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
}
}
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: http.StatusMethodNotAllowed,
Error: "Method Not Allowed",
Message: "The server knows the request method, but the target resource doesn't support this method",
Path: r.RequestURI,
Timestamp: time.Now(),
}
payload, err := json.Marshal(e)
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
@@ -65,7 +80,7 @@ func notFound(message string, w http.ResponseWriter, r *http.Request) {
func unauthorized(w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: 401,
Status: http.StatusUnauthorized,
Error: "Unauthorized",
Message: "The request has not been completed because it lacks valid authentication credentials for the requested resource.",
Path: r.RequestURI,
@@ -77,8 +92,8 @@ func unauthorized(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.Header().Add("WWW-Authenticate", "Custom realm=\"Login via /api/login\"")
w.WriteHeader(401)
w.Header().Add("WWW-Authenticate", "Custom realm=\"loginUserHandler via /api/login\"")
w.WriteHeader(http.StatusUnauthorized)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
@@ -87,8 +102,8 @@ func unauthorized(w http.ResponseWriter, r *http.Request) {
func forbidden(w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: 403,
Error: "Unauthorized",
Status: http.StatusForbidden,
Error: "Forbidden",
Message: "The access is permanently forbidden and tied to the application logic, such as insufficient rights to a resource.",
Path: r.RequestURI,
Timestamp: time.Now(),
@@ -99,14 +114,14 @@ func forbidden(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(403)
w.WriteHeader(http.StatusForbidden)
_, err = w.Write(payload)
if err != nil {
log.Println(err)
}
}
func ok(obj interface{}, w http.ResponseWriter, _ *http.Request) {
func ok(obj interface{}, w http.ResponseWriter) {
payload, err := json.Marshal(obj)
if err != nil {
log.Println(err)
@@ -120,7 +135,7 @@ func ok(obj interface{}, w http.ResponseWriter, _ *http.Request) {
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
e := httpError{
Status: 400,
Status: http.StatusBadRequest,
Error: "Bad Request",
Message: message,
Path: r.RequestURI,
@@ -132,7 +147,7 @@ func badRequest(message string, w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(400)
w.WriteHeader(http.StatusBadRequest)
_, err = w.Write(payload)
if err != nil {
log.Println(err)