Skip to content

Commit

Permalink
Refactor: Remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
moebiusband73 committed Aug 15, 2024
1 parent 561fd41 commit 9b6db46
Showing 1 changed file with 13 additions and 36 deletions.
49 changes: 13 additions & 36 deletions cmd/cc-backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ var (
apiHandle *api.RestApi
)

func onFailureResponse(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(rw).Encode(map[string]string{
"status": http.StatusText(http.StatusUnauthorized),
"error": err.Error(),
})
}

func serverInit() {
// Setup the http.Handler/Router used by the server
graph.Init()
Expand Down Expand Up @@ -166,64 +175,32 @@ func serverInit() {
return authHandle.AuthApi(
// On success;
next,

// On failure: JSON Response
func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(rw).Encode(map[string]string{
"status": http.StatusText(http.StatusUnauthorized),
"error": err.Error(),
})
})
onFailureResponse)
})

userapi.Use(func(next http.Handler) http.Handler {
return authHandle.AuthUserApi(
// On success;
next,

// On failure: JSON Response
func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(rw).Encode(map[string]string{
"status": http.StatusText(http.StatusUnauthorized),
"error": err.Error(),
})
})
onFailureResponse)
})

configapi.Use(func(next http.Handler) http.Handler {
return authHandle.AuthConfigApi(
// On success;
next,

// On failure: JSON Response
func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(rw).Encode(map[string]string{
"status": http.StatusText(http.StatusUnauthorized),
"error": err.Error(),
})
})
onFailureResponse)
})

frontendapi.Use(func(next http.Handler) http.Handler {
return authHandle.AuthFrontendApi(
// On success;
next,

// On failure: JSON Response
func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(rw).Encode(map[string]string{
"status": http.StatusText(http.StatusUnauthorized),
"error": err.Error(),
})
})
onFailureResponse)
})
}

Expand Down

0 comments on commit 9b6db46

Please sign in to comment.