Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2

appview/repo: use slog logger in artifact handlers

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 48a5e6a6 ca47e44a

verified
+39 -30
+39 -30
appview/repo/artifact.go
··· 5 5 "encoding/json" 6 6 "fmt" 7 7 "io" 8 - "log" 9 8 "net/http" 10 9 "net/url" 11 10 "time" ··· 30 31 31 32 // TODO: proper statuses here on early exit 32 33 func (rp *Repo) AttachArtifact(w http.ResponseWriter, r *http.Request) { 34 + l := rp.logger.With("handler", "AttachArtifact") 35 + 33 36 user := rp.oauth.GetMultiAccountUser(r) 34 37 tagParam := chi.URLParam(r, "tag") 35 38 f, err := rp.repoResolver.Resolve(r) 36 39 if err != nil { 37 - log.Println("failed to get repo and knot", err) 40 + l.Error("failed to get repo and knot", "err", err) 38 41 rp.pages.Notice(w, "upload", "failed to upload artifact, error in repo resolution") 39 42 return 40 43 } 41 44 42 45 tag, err := rp.resolveTag(r.Context(), f, tagParam) 43 46 if err != nil { 44 - log.Println("failed to resolve tag", err) 47 + l.Error("failed to resolve tag", "err", err) 45 48 rp.pages.Notice(w, "upload", "failed to upload artifact, error in tag resolution") 46 49 return 47 50 } 48 51 49 52 file, header, err := r.FormFile("artifact") 50 53 if err != nil { 51 - log.Println("failed to upload artifact", err) 54 + l.Error("failed to upload artifact", "err", err) 52 55 rp.pages.Notice(w, "upload", "failed to upload artifact") 53 56 return 54 57 } ··· 58 57 59 58 client, err := rp.oauth.AuthorizedClient(r) 60 59 if err != nil { 61 - log.Println("failed to get authorized client", err) 60 + l.Error("failed to get authorized client", "err", err) 62 61 rp.pages.Notice(w, "upload", "failed to get authorized client") 63 62 return 64 63 } 65 64 66 65 uploadBlobResp, err := xrpc.RepoUploadBlob(r.Context(), client, file, header.Header.Get("Content-Type")) 67 66 if err != nil { 68 - log.Println("failed to upload blob", err) 67 + l.Error("failed to upload blob", "err", err) 69 68 rp.pages.Notice(w, "upload", "Failed to upload blob to your PDS. Try again later.") 70 69 return 71 70 } 72 71 73 - log.Println("uploaded blob", humanize.Bytes(uint64(uploadBlobResp.Blob.Size)), uploadBlobResp.Blob.Ref.String()) 72 + l.Info("uploaded blob", "size", humanize.Bytes(uint64(uploadBlobResp.Blob.Size)), "blobRef", uploadBlobResp.Blob.Ref.String()) 74 73 75 74 rkey := tid.TID() 76 75 createdAt := time.Now() ··· 90 89 }, 91 90 }) 92 91 if err != nil { 93 - log.Println("failed to create record", err) 92 + l.Error("failed to create record", "err", err) 94 93 rp.pages.Notice(w, "upload", "Failed to create artifact record. Try again later.") 95 94 return 96 95 } 97 96 98 - log.Println(putRecordResp.Uri) 97 + l.Debug("created record for blob", "aturi", putRecordResp.Uri) 99 98 100 99 tx, err := rp.db.BeginTx(r.Context(), nil) 101 100 if err != nil { 102 - log.Println("failed to start tx") 101 + l.Error("failed to start tx") 103 102 rp.pages.Notice(w, "upload", "Failed to create artifact. Try again later.") 104 103 return 105 104 } ··· 119 118 120 119 err = db.AddArtifact(tx, artifact) 121 120 if err != nil { 122 - log.Println("failed to add artifact record to db", err) 121 + l.Error("failed to add artifact record to db", "err", err) 123 122 rp.pages.Notice(w, "upload", "Failed to create artifact. Try again later.") 124 123 return 125 124 } 126 125 127 126 err = tx.Commit() 128 127 if err != nil { 129 - log.Println("failed to add artifact record to db") 128 + l.Error("failed to add artifact record to db") 130 129 rp.pages.Notice(w, "upload", "Failed to create artifact. Try again later.") 131 130 return 132 131 } ··· 139 138 } 140 139 141 140 func (rp *Repo) DownloadArtifact(w http.ResponseWriter, r *http.Request) { 141 + l := rp.logger.With("handler", "DownloadArtifact") 142 + 142 143 f, err := rp.repoResolver.Resolve(r) 143 144 if err != nil { 144 - log.Println("failed to get repo and knot", err) 145 + l.Error("failed to get repo and knot", "err", err) 145 146 http.Error(w, "failed to resolve repo", http.StatusInternalServerError) 146 147 return 147 148 } ··· 153 150 154 151 tag, err := rp.resolveTag(r.Context(), f, tagParam) 155 152 if err != nil { 156 - log.Println("failed to resolve tag", err) 153 + l.Error("failed to resolve tag", "err", err) 157 154 rp.pages.Notice(w, "upload", "failed to upload artifact, error in tag resolution") 158 155 return 159 156 } ··· 165 162 orm.FilterEq("name", filename), 166 163 ) 167 164 if err != nil { 168 - log.Println("failed to get artifacts", err) 165 + l.Error("failed to get artifacts", "err", err) 169 166 http.Error(w, "failed to get artifact", http.StatusInternalServerError) 170 167 return 171 168 } 172 169 173 170 if len(artifacts) != 1 { 174 - log.Printf("too many or too few artifacts found") 171 + l.Error("too many or too few artifacts found") 175 172 http.Error(w, "artifact not found", http.StatusNotFound) 176 173 return 177 174 } ··· 180 177 181 178 ownerId, err := rp.idResolver.ResolveIdent(r.Context(), f.Did) 182 179 if err != nil { 183 - log.Println("failed to resolve repo owner did", f.Did, err) 180 + l.Error("failed to resolve repo owner did", f.Did, "err", err) 184 181 http.Error(w, "repository owner not found", http.StatusNotFound) 185 182 return 186 183 } ··· 194 191 195 192 req, err := http.NewRequest(http.MethodGet, url.String(), nil) 196 193 if err != nil { 197 - log.Println("failed to create request", err) 194 + l.Error("failed to create request", "err", err) 198 195 http.Error(w, "failed to create request", http.StatusInternalServerError) 199 196 return 200 197 } ··· 202 199 203 200 resp, err := http.DefaultClient.Do(req) 204 201 if err != nil { 205 - log.Println("failed to make request", err) 202 + l.Error("failed to make request", "err", err) 206 203 http.Error(w, "failed to make request to PDS", http.StatusInternalServerError) 207 204 return 208 205 } ··· 218 215 219 216 // stream the body directly to the client 220 217 if _, err := io.Copy(w, resp.Body); err != nil { 221 - log.Println("error streaming response to client:", err) 218 + l.Error("error streaming response to client:", "err", err) 222 219 } 223 220 } 224 221 225 222 // TODO: proper statuses here on early exit 226 223 func (rp *Repo) DeleteArtifact(w http.ResponseWriter, r *http.Request) { 224 + l := rp.logger.With("handler", "DeleteArtifact") 225 + 227 226 user := rp.oauth.GetMultiAccountUser(r) 228 227 tagParam := chi.URLParam(r, "tag") 229 228 filename := chi.URLParam(r, "file") 230 229 f, err := rp.repoResolver.Resolve(r) 231 230 if err != nil { 232 - log.Println("failed to get repo and knot", err) 231 + l.Error("failed to get repo and knot", "err", err) 233 232 return 234 233 } 235 234 ··· 246 241 orm.FilterEq("name", filename), 247 242 ) 248 243 if err != nil { 249 - log.Println("failed to get artifacts", err) 244 + l.Error("failed to get artifacts", "err", err) 250 245 rp.pages.Notice(w, "remove", "Failed to delete artifact. Try again later.") 251 246 return 252 247 } ··· 258 253 artifact := artifacts[0] 259 254 260 255 if user.Active.Did != artifact.Did { 261 - log.Println("user not authorized to delete artifact", err) 256 + l.Error("user not authorized to delete artifact", "err", err) 262 257 rp.pages.Notice(w, "remove", "Unauthorized deletion of artifact.") 263 258 return 264 259 } ··· 269 264 Rkey: artifact.Rkey, 270 265 }) 271 266 if err != nil { 272 - log.Println("failed to get blob from pds", err) 267 + l.Error("failed to get blob from pds", "err", err) 273 268 rp.pages.Notice(w, "remove", "Failed to remove blob from PDS.") 274 269 return 275 270 } 276 271 277 272 tx, err := rp.db.BeginTx(r.Context(), nil) 278 273 if err != nil { 279 - log.Println("failed to start tx") 274 + l.Error("failed to start tx") 280 275 rp.pages.Notice(w, "remove", "Failed to delete artifact. Try again later.") 281 276 return 282 277 } ··· 288 283 orm.FilterEq("name", filename), 289 284 ) 290 285 if err != nil { 291 - log.Println("failed to remove artifact record from db", err) 286 + l.Error("failed to remove artifact record from db", "err", err) 292 287 rp.pages.Notice(w, "remove", "Failed to delete artifact. Try again later.") 293 288 return 294 289 } 295 290 296 291 err = tx.Commit() 297 292 if err != nil { 298 - log.Println("failed to remove artifact record from db") 293 + l.Error("failed to remove artifact record from db") 299 294 rp.pages.Notice(w, "remove", "Failed to delete artifact. Try again later.") 300 295 return 301 296 } 297 + 298 + l.Info("successfully deleted artifact", "tag", tagParam, "file", filename) 302 299 303 300 w.Write([]byte{}) 304 301 } 305 302 306 303 func (rp *Repo) resolveTag(ctx context.Context, f *models.Repo, tagParam string) (*types.TagReference, error) { 304 + l := rp.logger.With("handler", "resolveTag") 305 + 307 306 tagParam, err := url.QueryUnescape(tagParam) 308 307 if err != nil { 309 308 return nil, err ··· 326 317 xrpcBytes, err := tangled.RepoTags(ctx, xrpcc, "", 0, repo) 327 318 if err != nil { 328 319 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 329 - log.Println("failed to call XRPC repo.tags", xrpcerr) 320 + l.Error("failed to call XRPC repo.tags", "err", xrpcerr) 330 321 return nil, xrpcerr 331 322 } 332 - log.Println("failed to reach knotserver", err) 323 + l.Error("failed to reach knotserver", "err", err) 333 324 return nil, err 334 325 } 335 326 336 327 var result types.RepoTagsResponse 337 328 if err := json.Unmarshal(xrpcBytes, &result); err != nil { 338 - log.Println("failed to decode XRPC tags response", err) 329 + l.Error("failed to decode XRPC tags response", "err", err) 339 330 return nil, err 340 331 } 341 332