Skip to content

Commit 4ccb68a

Browse files
committed
style: extract repeated string literals into constants
1 parent f665679 commit 4ccb68a

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

lfs/batch.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (h *Handler) processDownload(r *http.Request, obj Object, baseURL string) O
5454
resp.Error = &ObjectError{Code: 404, Message: "object not found"}
5555
} else {
5656
log.Error().Err(err).Str("oid", obj.OID).Msg("reading object size")
57-
resp.Error = &ObjectError{Code: 500, Message: "internal error"}
57+
resp.Error = &ObjectError{Code: 500, Message: msgInternalError}
5858
}
5959
return resp
6060
}
@@ -72,13 +72,13 @@ func (h *Handler) processDownload(r *http.Request, obj Object, baseURL string) O
7272
href, err := p.PresignGet(r.Context(), h.endpoint.Path, obj.OID, h.signer.Expiry())
7373
if err != nil {
7474
log.Error().Err(err).Str("oid", obj.OID).Msg("presigning download URL")
75-
resp.Error = &ObjectError{Code: 500, Message: "internal error"}
75+
resp.Error = &ObjectError{Code: 500, Message: msgInternalError}
7676
return resp
7777
}
7878
resp.Authenticated = new(bool)
7979
*resp.Authenticated = true
8080
resp.Actions = map[string]Action{
81-
"download": {Href: href, ExpiresIn: int64(h.signer.Expiry().Seconds())},
81+
opDownload: {Href: href, ExpiresIn: int64(h.signer.Expiry().Seconds())},
8282
}
8383
return resp
8484
}
@@ -88,7 +88,7 @@ func (h *Handler) processDownload(r *http.Request, obj Object, baseURL string) O
8888
resp.Authenticated = new(bool)
8989
*resp.Authenticated = true
9090
resp.Actions = map[string]Action{
91-
"download": {
91+
opDownload: {
9292
Href: signed.Href,
9393
ExpiresIn: signed.ExpiresIn,
9494
ExpiresAt: signed.ExpiresAt,
@@ -112,7 +112,7 @@ func (h *Handler) processUpload(r *http.Request, obj Object, baseURL string) Obj
112112
exists, err := h.store.Exists(r.Context(), h.endpoint.Path, obj.OID)
113113
if err != nil {
114114
log.Error().Err(err).Str("oid", obj.OID).Msg("checking object existence")
115-
resp.Error = &ObjectError{Code: 500, Message: "internal error"}
115+
resp.Error = &ObjectError{Code: 500, Message: msgInternalError}
116116
return resp
117117
}
118118
if exists {
@@ -125,15 +125,15 @@ func (h *Handler) processUpload(r *http.Request, obj Object, baseURL string) Obj
125125
href, err := p.PresignPut(r.Context(), h.endpoint.Path, obj.OID, h.signer.Expiry())
126126
if err != nil {
127127
log.Error().Err(err).Str("oid", obj.OID).Msg("presigning upload URL")
128-
resp.Error = &ObjectError{Code: 500, Message: "internal error"}
128+
resp.Error = &ObjectError{Code: 500, Message: msgInternalError}
129129
return resp
130130
}
131131
verifyPath := fmt.Sprintf("%s/objects/verify", h.endpoint.Path)
132132
signedVerify := h.signer.Sign(baseURL, verifyPath)
133133
resp.Authenticated = new(bool)
134134
*resp.Authenticated = true
135135
resp.Actions = map[string]Action{
136-
"upload": {Href: href, ExpiresIn: int64(h.signer.Expiry().Seconds())},
136+
opUpload: {Href: href, ExpiresIn: int64(h.signer.Expiry().Seconds())},
137137
"verify": {
138138
Href: signedVerify.Href,
139139
ExpiresIn: signedVerify.ExpiresIn,
@@ -150,7 +150,7 @@ func (h *Handler) processUpload(r *http.Request, obj Object, baseURL string) Obj
150150
resp.Authenticated = new(bool)
151151
*resp.Authenticated = true
152152
resp.Actions = map[string]Action{
153-
"upload": {
153+
opUpload: {
154154
Href: signedUpload.Href,
155155
ExpiresIn: signedUpload.ExpiresIn,
156156
ExpiresAt: signedUpload.ExpiresAt,

lfs/handler.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func getRequestID(ctx context.Context) string {
5151

5252
const lfsMediaType = "application/vnd.git-lfs+json"
5353

54+
// msgInternalError is the generic message returned to clients for unexpected server-side failures.
55+
const msgInternalError = "internal error"
56+
57+
// URL schemes used when constructing external base URLs.
58+
const (
59+
schemeHTTP = "http"
60+
schemeHTTPS = "https"
61+
)
62+
5463
// LFS operation names used in batch requests.
5564
const (
5665
opDownload = "download"
@@ -213,7 +222,7 @@ func (h *Handler) DownloadHandler(w http.ResponseWriter, r *http.Request) {
213222
writeError(w, r, http.StatusNotFound, "object not found")
214223
} else {
215224
log.Error().Err(err).Str("oid", oid).Msg("reading object")
216-
writeError(w, r, http.StatusInternalServerError, "internal error")
225+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
217226
}
218227
return
219228
}
@@ -275,7 +284,7 @@ func (h *Handler) UploadHandler(w http.ResponseWriter, r *http.Request) {
275284
exists, err := h.store.Exists(r.Context(), h.endpoint.Path, oid)
276285
if err != nil {
277286
log.Error().Err(err).Str("oid", oid).Msg("checking object existence")
278-
writeError(w, r, http.StatusInternalServerError, "internal error")
287+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
279288
return
280289
}
281290
if exists {
@@ -296,7 +305,7 @@ func (h *Handler) UploadHandler(w http.ResponseWriter, r *http.Request) {
296305
return
297306
}
298307
log.Error().Err(err).Str("oid", oid).Msg("storing object")
299-
writeError(w, r, http.StatusInternalServerError, "internal error")
308+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
300309
return
301310
}
302311

@@ -342,7 +351,7 @@ func (h *Handler) VerifyHandler(w http.ResponseWriter, r *http.Request) {
342351
writeError(w, r, http.StatusNotFound, "object not found")
343352
} else {
344353
log.Error().Err(err).Str("oid", req.OID).Msg("checking object size")
345-
writeError(w, r, http.StatusInternalServerError, "internal error")
354+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
346355
}
347356
return
348357
}
@@ -391,7 +400,7 @@ func (h *Handler) CreateLockHandler(w http.ResponseWriter, r *http.Request) {
391400
return
392401
}
393402
log.Error().Err(err).Str("endpoint", h.endpoint.Name).Msg("creating lock")
394-
writeError(w, r, http.StatusInternalServerError, "internal error")
403+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
395404
return
396405
}
397406

@@ -433,7 +442,7 @@ func (h *Handler) ListLocksHandler(w http.ResponseWriter, r *http.Request) {
433442
})
434443
if err != nil {
435444
log.Error().Err(err).Str("endpoint", h.endpoint.Name).Msg("listing locks")
436-
writeError(w, r, http.StatusInternalServerError, "internal error")
445+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
437446
return
438447
}
439448

@@ -470,7 +479,7 @@ func (h *Handler) VerifyLocksHandler(w http.ResponseWriter, r *http.Request) {
470479
})
471480
if err != nil {
472481
log.Error().Err(err).Str("endpoint", h.endpoint.Name).Msg("verifying locks")
473-
writeError(w, r, http.StatusInternalServerError, "internal error")
482+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
474483
return
475484
}
476485

@@ -528,7 +537,7 @@ func (h *Handler) UnlockHandler(w http.ResponseWriter, r *http.Request) {
528537
return
529538
}
530539
log.Error().Err(err).Str("endpoint", h.endpoint.Name).Msg("unlocking")
531-
writeError(w, r, http.StatusInternalServerError, "internal error")
540+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
532541
return
533542
}
534543

@@ -558,7 +567,7 @@ func (h *Handler) requireAuthResult(w http.ResponseWriter, r *http.Request, op a
558567
result, err := h.authenticate(r, op)
559568
if err != nil {
560569
log.Error().Err(err).Str("endpoint", h.endpoint.Name).Msg("authentication error")
561-
writeError(w, r, http.StatusInternalServerError, "internal error")
570+
writeError(w, r, http.StatusInternalServerError, msgInternalError)
562571
return auth.Result{}, false
563572
}
564573
if !result.Authenticated {
@@ -631,11 +640,11 @@ func (h *Handler) requestBaseURL(r *http.Request) string {
631640
if h.baseURL != "" {
632641
return h.baseURL
633642
}
634-
scheme := "http"
643+
scheme := schemeHTTP
635644
if r.TLS != nil {
636-
scheme = "https"
645+
scheme = schemeHTTPS
637646
}
638-
if proto := r.Header.Get("X-Forwarded-Proto"); proto == "http" || proto == "https" {
647+
if proto := r.Header.Get("X-Forwarded-Proto"); proto == schemeHTTP || proto == schemeHTTPS {
639648
scheme = proto
640649
}
641650
host := r.Host
@@ -689,7 +698,7 @@ func writeJSON(w http.ResponseWriter, status int, v any) {
689698
data, err := json.Marshal(v)
690699
if err != nil {
691700
log.Error().Err(err).Msg("marshalling JSON response")
692-
http.Error(w, "internal error", http.StatusInternalServerError)
701+
http.Error(w, msgInternalError, http.StatusInternalServerError)
693702
return
694703
}
695704
w.Header().Set("Content-Type", lfsMediaType)

0 commit comments

Comments
 (0)