From b5047f2607cea16ee2808d4abb05c26ea55bbcaa Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 28 Apr 2022 22:22:23 -0400 Subject: [PATCH] Create basic ActivityPub inspector utility Prints AP information for a given URL. --- go.mod | 3 +++ main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c9d6f9a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module code.as/matt/ap-inspect + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..14a86e7 --- /dev/null +++ b/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" +) + +func main() { + if len(os.Args) < 2 { + log.Fatalln("usage: ap-inspect BLOG-URL") + } + blogURL := os.Args[1] + + // Set up request + req, err := http.NewRequest("GET", blogURL, nil) + if err != nil { + log.Fatalln(err) + } + req.Header.Set("Accept", "application/activity+json") + + // Send request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Fatalln(err) + } + if resp != nil && resp.Body != nil { + defer resp.Body.Close() + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + fmt.Printf("Status : %s\n", resp.Status) + + // Pretty-print the response + var fmttd bytes.Buffer + err = json.Indent(&fmttd, body, "", " ") + if err != nil { + log.Fatalln(err) + } + fmt.Printf("Response:\n%s\n", fmttd.String()) +}