aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-06-13 09:14:09 -0400
committerXe Iaso <me@xeiaso.net>2023-06-13 09:14:09 -0400
commit5a77317341d549d3ee41e2ac4371c931fbf704e0 (patch)
tree5765d928514d14830b77e22196a7996297ffd5bb
parentdc43b4c55db696304a4045705cb772409f0a67a6 (diff)
downloadx-5a77317341d549d3ee41e2ac4371c931fbf704e0.tar.xz
x-5a77317341d549d3ee41e2ac4371c931fbf704e0.zip
web/bsky: start a small experiment here
Signed-off-by: Xe Iaso <me@xeiaso.net>
-rw-r--r--web/bsky/com/atproto/identity/resolveHandle/lexicon.json35
-rw-r--r--web/bsky/com/atproto/identity/resolveHandle/package.go69
-rw-r--r--web/bsky/package.go23
3 files changed, 127 insertions, 0 deletions
diff --git a/web/bsky/com/atproto/identity/resolveHandle/lexicon.json b/web/bsky/com/atproto/identity/resolveHandle/lexicon.json
new file mode 100644
index 0000000..b3aed33
--- /dev/null
+++ b/web/bsky/com/atproto/identity/resolveHandle/lexicon.json
@@ -0,0 +1,35 @@
+{
+ "lexicon": 1,
+ "id": "com.atproto.identity.resolveHandle",
+ "defs": {
+ "main": {
+ "type": "query",
+ "description": "Provides the DID of a repo.",
+ "parameters": {
+ "type": "params",
+ "properties": {
+ "handle": {
+ "type": "string",
+ "format": "handle",
+ "description": "The handle to resolve. If not supplied, will resolve the host's own handle."
+ }
+ }
+ },
+ "output": {
+ "encoding": "application/json",
+ "schema": {
+ "type": "object",
+ "required": [
+ "did"
+ ],
+ "properties": {
+ "did": {
+ "type": "string",
+ "format": "did"
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/web/bsky/com/atproto/identity/resolveHandle/package.go b/web/bsky/com/atproto/identity/resolveHandle/package.go
new file mode 100644
index 0000000..398c0ec
--- /dev/null
+++ b/web/bsky/com/atproto/identity/resolveHandle/package.go
@@ -0,0 +1,69 @@
+package resolveHandle
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+
+ "github.com/pasztorpisti/qs"
+ "within.website/ln"
+ "within.website/x/web/bsky"
+)
+
+type Query struct {
+ Handle *string `json:"handle"`
+}
+
+func (q Query) XRPCType() string {
+ return "params"
+}
+
+type Output struct {
+ DID string `json:"did"`
+}
+
+func (o Output) XRPCType() string {
+ return "object"
+}
+
+type Handler interface {
+ IdentityResolveHandle(context.Context, *Query) (*Output, error)
+}
+
+func ServeHTTP(h Handler) http.HandlerFunc {
+ return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+ q := &Query{}
+
+ if err := qs.Unmarshal(q, req.URL.RawQuery); err != nil {
+ ln.Error(req.Context(), err, ln.Action("parsing request query parameters"))
+ rw.Header().Set("Content-Type", "application/json")
+ rw.WriteHeader(http.StatusBadRequest)
+ json.NewEncoder(rw).Encode(bsky.Error{
+ ErrorKind: "InvalidRequestError",
+ Message: "Your request cannot be parsed. Try again.",
+ })
+ return
+ }
+
+ output, err := h.IdentityResolveHandle(req.Context(), q)
+ if err != nil {
+ ln.Error(req.Context(), err, ln.Action("doing handler logic"))
+ switch err.(type) {
+ case *bsky.Error:
+ rw.Header().Set("Content-Type", "application/json")
+ rw.WriteHeader(http.StatusBadRequest)
+ json.NewEncoder(rw).Encode(err)
+ default:
+ rw.Header().Set("Content-Type", "application/json")
+ rw.WriteHeader(http.StatusInternalServerError)
+ json.NewEncoder(rw).Encode(bsky.Error{
+ ErrorKind: "InternalServerError",
+ Message: "There was an internal server error. No further information is available.",
+ })
+ }
+ return
+ }
+
+ json.NewEncoder(rw).Encode(output)
+ })
+}
diff --git a/web/bsky/package.go b/web/bsky/package.go
new file mode 100644
index 0000000..88be5e1
--- /dev/null
+++ b/web/bsky/package.go
@@ -0,0 +1,23 @@
+package bsky
+
+import (
+ "fmt"
+
+ "within.website/ln"
+)
+
+type Error struct {
+ ErrorKind string `json:"error"`
+ Message string `json:"message"`
+}
+
+func (e Error) Error() string {
+ return fmt.Sprintf("bsky: %s: %s", e.ErrorKind, e.Message)
+}
+
+func (e Error) F() ln.F {
+ return ln.F{
+ "error": e.ErrorKind,
+ "message": e.Message,
+ }
+}