Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver: simplify json response boilerplate

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

authored by

oppiliappan and committed by
Tangled
fe72fb7c 96159595

+23 -136
+1 -10
knotserver/xrpc/list_keys.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "strconv" 7 6 ··· 45 46 response.Cursor = &nextCursor 46 47 } 47 48 48 - w.Header().Set("Content-Type", "application/json") 49 - if err := json.NewEncoder(w).Encode(response); err != nil { 50 - x.Logger.Error("failed to encode response", "error", err) 51 - writeError(w, xrpcerr.NewXrpcError( 52 - xrpcerr.WithTag("InternalServerError"), 53 - xrpcerr.WithMessage("failed to encode response"), 54 - ), http.StatusInternalServerError) 55 - return 56 - } 49 + writeJson(w, response) 57 50 }
+1 -10
knotserver/xrpc/owner.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 7 6 "tangled.sh/tangled.sh/core/api/tangled" ··· 18 19 Owner: owner, 19 20 } 20 21 21 - w.Header().Set("Content-Type", "application/json") 22 - if err := json.NewEncoder(w).Encode(response); err != nil { 23 - x.Logger.Error("failed to encode response", "error", err) 24 - writeError(w, xrpcerr.NewXrpcError( 25 - xrpcerr.WithTag("InternalServerError"), 26 - xrpcerr.WithMessage("failed to encode response"), 27 - ), http.StatusInternalServerError) 28 - return 29 - } 22 + writeJson(w, response) 30 23 }
+1 -10
knotserver/xrpc/repo_blob.go
··· 3 3 import ( 4 4 "crypto/sha256" 5 5 "encoding/base64" 6 - "encoding/json" 7 6 "fmt" 8 7 "net/http" 9 8 "path/filepath" ··· 123 124 response.MimeType = &mimeType 124 125 } 125 126 126 - w.Header().Set("Content-Type", "application/json") 127 - if err := json.NewEncoder(w).Encode(response); err != nil { 128 - x.Logger.Error("failed to encode response", "error", err) 129 - writeError(w, xrpcerr.NewXrpcError( 130 - xrpcerr.WithTag("InternalServerError"), 131 - xrpcerr.WithMessage("failed to encode response"), 132 - ), http.StatusInternalServerError) 133 - return 134 - } 127 + writeJson(w, response) 135 128 } 136 129 137 130 // isTextualMimeType returns true if the MIME type represents textual content
+1 -10
knotserver/xrpc/repo_branch.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "net/url" 7 6 "time" ··· 81 82 When: commit.Author.When.Format(time.RFC3339), 82 83 } 83 84 84 - w.Header().Set("Content-Type", "application/json") 85 - if err := json.NewEncoder(w).Encode(response); err != nil { 86 - x.Logger.Error("failed to encode response", "error", err) 87 - writeError(w, xrpcerr.NewXrpcError( 88 - xrpcerr.WithTag("InternalServerError"), 89 - xrpcerr.WithMessage("failed to encode response"), 90 - ), http.StatusInternalServerError) 91 - return 92 - } 85 + writeJson(w, response) 93 86 }
+1 -11
knotserver/xrpc/repo_branches.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "strconv" 7 6 ··· 52 53 Branches: paginatedBranches, 53 54 } 54 55 55 - // Write JSON response directly 56 - w.Header().Set("Content-Type", "application/json") 57 - if err := json.NewEncoder(w).Encode(response); err != nil { 58 - x.Logger.Error("failed to encode response", "error", err) 59 - writeError(w, xrpcerr.NewXrpcError( 60 - xrpcerr.WithTag("InternalServerError"), 61 - xrpcerr.WithMessage("failed to encode response"), 62 - ), http.StatusInternalServerError) 63 - return 64 - } 56 + writeJson(w, response) 65 57 }
+2 -11
knotserver/xrpc/repo_compare.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "fmt" 6 5 "net/http" 7 6 ··· 71 72 return 72 73 } 73 74 74 - resp := types.RepoFormatPatchResponse{ 75 + response := types.RepoFormatPatchResponse{ 75 76 Rev1: commit1.Hash.String(), 76 77 Rev2: commit2.Hash.String(), 77 78 FormatPatch: formatPatch, 78 79 Patch: rawPatch, 79 80 } 80 81 81 - w.Header().Set("Content-Type", "application/json") 82 - if err := json.NewEncoder(w).Encode(resp); err != nil { 83 - x.Logger.Error("failed to encode response", "error", err) 84 - writeError(w, xrpcerr.NewXrpcError( 85 - xrpcerr.WithTag("InternalServerError"), 86 - xrpcerr.WithMessage("failed to encode response"), 87 - ), http.StatusInternalServerError) 88 - return 89 - } 82 + writeJson(w, response) 90 83 }
+2 -11
knotserver/xrpc/repo_diff.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 7 6 "tangled.sh/tangled.sh/core/knotserver/git" ··· 32 33 return 33 34 } 34 35 35 - resp := types.RepoCommitResponse{ 36 + response := types.RepoCommitResponse{ 36 37 Ref: ref, 37 38 Diff: diff, 38 39 } 39 40 40 - w.Header().Set("Content-Type", "application/json") 41 - if err := json.NewEncoder(w).Encode(resp); err != nil { 42 - x.Logger.Error("failed to encode response", "error", err) 43 - writeError(w, xrpcerr.NewXrpcError( 44 - xrpcerr.WithTag("InternalServerError"), 45 - xrpcerr.WithMessage("failed to encode response"), 46 - ), http.StatusInternalServerError) 47 - return 48 - } 41 + writeJson(w, response) 49 42 }
+1 -10
knotserver/xrpc/repo_get_default_branch.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "time" 7 6 ··· 35 36 When: time.UnixMicro(0).Format(time.RFC3339), 36 37 } 37 38 38 - w.Header().Set("Content-Type", "application/json") 39 - if err := json.NewEncoder(w).Encode(response); err != nil { 40 - x.Logger.Error("failed to encode response", "error", err) 41 - writeError(w, xrpcerr.NewXrpcError( 42 - xrpcerr.WithTag("InternalServerError"), 43 - xrpcerr.WithMessage("failed to encode response"), 44 - ), http.StatusInternalServerError) 45 - return 46 - } 39 + writeJson(w, response) 47 40 }
+1 -10
knotserver/xrpc/repo_languages.go
··· 2 2 3 3 import ( 4 4 "context" 5 - "encoding/json" 6 5 "math" 7 6 "net/http" 8 7 "time" ··· 72 73 response.TotalFiles = &totalFiles 73 74 } 74 75 75 - w.Header().Set("Content-Type", "application/json") 76 - if err := json.NewEncoder(w).Encode(response); err != nil { 77 - x.Logger.Error("failed to encode response", "error", err) 78 - writeError(w, xrpcerr.NewXrpcError( 79 - xrpcerr.WithTag("InternalServerError"), 80 - xrpcerr.WithMessage("failed to encode response"), 81 - ), http.StatusInternalServerError) 82 - return 83 - } 76 + writeJson(w, response) 84 77 }
+1 -11
knotserver/xrpc/repo_log.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "strconv" 7 6 ··· 77 78 78 79 response.Log = true 79 80 80 - // Write JSON response directly 81 - w.Header().Set("Content-Type", "application/json") 82 - if err := json.NewEncoder(w).Encode(response); err != nil { 83 - x.Logger.Error("failed to encode response", "error", err) 84 - writeError(w, xrpcerr.NewXrpcError( 85 - xrpcerr.WithTag("InternalServerError"), 86 - xrpcerr.WithMessage("failed to encode response"), 87 - ), http.StatusInternalServerError) 88 - return 89 - } 81 + writeJson(w, response) 90 82 }
+1 -11
knotserver/xrpc/repo_tags.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "strconv" 7 6 ··· 82 83 Tags: paginatedTags, 83 84 } 84 85 85 - // Write JSON response directly 86 - w.Header().Set("Content-Type", "application/json") 87 - if err := json.NewEncoder(w).Encode(response); err != nil { 88 - x.Logger.Error("failed to encode response", "error", err) 89 - writeError(w, xrpcerr.NewXrpcError( 90 - xrpcerr.WithTag("InternalServerError"), 91 - xrpcerr.WithMessage("failed to encode response"), 92 - ), http.StatusInternalServerError) 93 - return 94 - } 86 + writeJson(w, response) 95 87 }
+1 -10
knotserver/xrpc/repo_tree.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "net/http" 6 5 "path/filepath" 7 6 "time" ··· 85 86 Files: treeEntries, 86 87 } 87 88 88 - w.Header().Set("Content-Type", "application/json") 89 - if err := json.NewEncoder(w).Encode(response); err != nil { 90 - x.Logger.Error("failed to encode response", "error", err) 91 - writeError(w, xrpcerr.NewXrpcError( 92 - xrpcerr.WithTag("InternalServerError"), 93 - xrpcerr.WithMessage("failed to encode response"), 94 - ), http.StatusInternalServerError) 95 - return 96 - } 89 + writeJson(w, response) 97 90 }
+1 -11
knotserver/xrpc/version.go
··· 1 1 package xrpc 2 2 3 3 import ( 4 - "encoding/json" 5 4 "fmt" 6 5 "net/http" 7 6 "runtime/debug" 8 7 9 8 "tangled.sh/tangled.sh/core/api/tangled" 10 - xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors" 11 9 ) 12 10 13 11 // version is set during build time. ··· 56 58 Version: version, 57 59 } 58 60 59 - w.Header().Set("Content-Type", "application/json") 60 - if err := json.NewEncoder(w).Encode(response); err != nil { 61 - x.Logger.Error("failed to encode response", "error", err) 62 - writeError(w, xrpcerr.NewXrpcError( 63 - xrpcerr.WithTag("InternalServerError"), 64 - xrpcerr.WithMessage("failed to encode response"), 65 - ), http.StatusInternalServerError) 66 - return 67 - } 61 + writeJson(w, response) 68 62 }
+8
knotserver/xrpc/xrpc.go
··· 117 117 w.WriteHeader(status) 118 118 json.NewEncoder(w).Encode(e) 119 119 } 120 + 121 + func writeJson(w http.ResponseWriter, response any) { 122 + w.Header().Set("Content-Type", "application/json") 123 + if err := json.NewEncoder(w).Encode(response); err != nil { 124 + writeError(w, xrpcerr.GenericError(err), http.StatusInternalServerError) 125 + return 126 + } 127 + }