Skip to content

Commit

Permalink
feat: unify response props to be upcase first letter
Browse files Browse the repository at this point in the history
  • Loading branch information
CrescentKohana committed Apr 29, 2024
1 parent 1c5a5f3 commit 7f7c8dc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,38 @@ func returnStatistics(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Fprintf(w, `{ "message": "statistics not implemented" }`)
fmt.Fprintf(w, `{ "Message": "statistics not implemented" }`)
}

// Returns the root path as JSON.
func returnRoot(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, `{ "message": "Mangatsu API available at /api" }`)
fmt.Fprintf(w, `{ "Message": "Mangatsu API available at /api" }`)
}

// Handles errors. Argument msg is only used with 400 and 500.
func errorHandler(w http.ResponseWriter, status int, msg string, endpoint string) {
switch status {
case http.StatusNotFound:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "not found" }`, status)
fmt.Fprintf(w, `{ "Code": %d, "Message": "not found" }`, status)
case http.StatusBadRequest:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "%s" }`, status, msg)
fmt.Fprintf(w, `{ "Code": %d, "Message": "%s" }`, status, msg)
case http.StatusForbidden:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "forbidden" }`, status)
fmt.Fprintf(w, `{ "Code": %d, "Message": "forbidden" }`, status)
case http.StatusUnauthorized:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "unauthorized" }`, status)
fmt.Fprintf(w, `{ "Code": %d, "Message": "unauthorized" }`, status)
case http.StatusGone:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "gone" }`, status)
fmt.Fprintf(w, `{ "Code": %d, "Message": "gone" }`, status)
case http.StatusConflict:
w.WriteHeader(status)
fmt.Fprintf(w, `{ "code": %d, "message": "%s" }`, status, msg)
fmt.Fprintf(w, `{ "Code": %d, "Message": "%s" }`, status, msg)
default:
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{ "status": %d, "message": "internal server error" }`, http.StatusInternalServerError)
fmt.Fprintf(w, `{ "status": %d, "Message": "internal server error" }`, http.StatusInternalServerError)
log.Z.Error(msg, zap.Int("status", status))
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,5 @@ func updateGallery(w http.ResponseWriter, r *http.Request) {
errorHandler(w, http.StatusInternalServerError, err.Error(), r.URL.Path)
return
}
fmt.Fprintf(w, `{ "message": "gallery updated" }`)
fmt.Fprintf(w, `{ "Message": "gallery updated" }`)
}
6 changes: 3 additions & 3 deletions pkg/api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func scanLibraries(w http.ResponseWriter, r *http.Request) {
go library.ScanArchives()

w.Header().Set("Content-Type", "application/json;charset=UTF-8")
fmt.Fprintf(w, `{ "message": "started scanning for new archives." }`)
fmt.Fprintf(w, `{ "Message": "started scanning for new archives." }`)
}

func returnProcessingStatus(w http.ResponseWriter, r *http.Request) {
Expand All @@ -43,7 +43,7 @@ func generateThumbnails(w http.ResponseWriter, r *http.Request) {
go library.GenerateThumbnails(pages == "true", force == "true")

w.Header().Set("Content-Type", "application/json;charset=UTF-8")
fmt.Fprintf(w, `{ "message": "started generateting thumbnails. Prioritizing covers." }`)
fmt.Fprintf(w, `{ "Message": "started generateting thumbnails. Prioritizing covers." }`)
}

func findMetadata(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -71,7 +71,7 @@ func findMetadata(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json;charset=UTF-8")
if metaTypes[metadata.XMeta] || metaTypes[metadata.EHDLMeta] || metaTypes[metadata.HathMeta] || title == "true" {
fmt.Fprintf(w, `{ "message": "started parsing given sources" }`)
fmt.Fprintf(w, `{ "Message": "started parsing given sources" }`)
return
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func register(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Fprintf(w, `{ "message": "successfully registered" }`)
fmt.Fprintf(w, `{ "Message": "successfully registered" }`)
}

func login(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -181,7 +181,7 @@ func logout(w http.ResponseWriter, r *http.Request) {
}
http.SetCookie(w, &cookie)

fmt.Fprint(w, `{ "message": "successfully logged out" }`)
fmt.Fprint(w, `{ "Message": "successfully logged out" }`)
}

// updateUser can be used to update role, password or username of users. Role can only be changed by admins.
Expand Down Expand Up @@ -220,7 +220,7 @@ func updateUser(w http.ResponseWriter, r *http.Request) {
return
}

fmt.Fprint(w, `{ "message": "successfully updated user" }`)
fmt.Fprint(w, `{ "Message": "successfully updated user" }`)
}

// returnUsers returns all users in the database. Only for admins. Never returns the hashed password.
Expand Down Expand Up @@ -351,7 +351,7 @@ func setFavorite(w http.ResponseWriter, r *http.Request) {
errorHandler(w, http.StatusInternalServerError, err.Error(), r.URL.Path)
return
}
fmt.Fprintf(w, `{ "message": "favorite group updated" }`)
fmt.Fprintf(w, `{ "Message": "favorite group updated" }`)
}

func updateProgress(w http.ResponseWriter, r *http.Request) {
Expand All @@ -371,5 +371,5 @@ func updateProgress(w http.ResponseWriter, r *http.Request) {
errorHandler(w, http.StatusInternalServerError, err.Error(), r.URL.Path)
return
}
fmt.Fprintf(w, `{ "message": "progress updated" }`)
fmt.Fprintf(w, `{ "Message": "progress updated" }`)
}

0 comments on commit 7f7c8dc

Please sign in to comment.