DEV Community

Isabella Liu
Isabella Liu

Posted on

Connecting to Telescope

Alt Text

Setup environment

I tried to set up the telescope environment on my Windows PC. I installed wsl2 and Ubuntu. I also followed the instruction
of the document. Some commands like systemclt start init.d failed.

Then I gave my old MacBook air a try. Docker runs well.
Alt Text

But when I did npm install, there were some warnings, and When I ran
npm start there was an error.

Alt Text

It was frustrating that I had to go back to wsl, and installed Redis and Elasticsearch as native applications. And I learned how to run a program as a background Damon.

Redis-server
ctrl-z
bg
Enter fullscreen mode Exit fullscreen mode

Change the code.

My linkChecker can check urls from a file, and the logic of my program is processing the file content into a string. So I had to convert the body of each post to a string.
I added one flag variable to check from the url http:localhost:3000, and extract all the posts to a string. Here is the change I have made.

diff --git a/helpers.go b/helpers.go
index 74ffce1..6c2a61e 100644
--- a/helpers.go
+++ b/helpers.go
@@ -17,6 +17,11 @@ import (
    "mvdan.cc/xurls/v2"
 )

+type Post struct {
+   ID  string
+   URL string
+}
+
 // extract all urls from the string and return them as a slice (array)
 func extractURL(str string) []string {
    rxStrict := xurls.Strict()
@@ -24,6 +29,37 @@ func extractURL(str string) []string {
    return foundUrls
 }

+func parseFromTelescope() []string {
+   var allPostBody []byte
+   resp, err := http.Get("http://localhost:3000/posts")
+   if err != nil {
+       panic(err)
+   }
+   defer resp.Body.Close()
+
+   body, err := ioutil.ReadAll(resp.Body)
+   if err != nil {
+       panic(err)
+   }
+   var posts []Post
+   if err := json.Unmarshal(body, &posts); err != nil {
+       panic(err)
+   }
+   for _, post := range posts {
+       resp, err := http.Get("http://localhost:3000" + post.URL)
+       if err != nil {
+           log.Fatal(err)
+       }
+       defer resp.Body.Close()
+       body, err := ioutil.ReadAll(resp.Body)
+       if err != nil {
+           log.Fatal(err)
+       }
+       allPostBody = append(allPostBody, body...)
+   }
+   return extractURL(string(allPostBody))
+}
+
 //Function to parse ignore URLs from provided ignore file path
 func parseIgnoreURL(ignoreFilePath string) []string {
    var ignoreURLs []string
diff --git a/urlChecker.go b/urlChecker.go
index 9c97457..41ab8c4 100644
--- a/urlChecker.go
+++ b/urlChecker.go
@@ -25,6 +25,8 @@ func main() {
    // ignore url flag
    ignoreFlag := flag.BoolP("ignore", "i", false, "ignore url patterns")

+   telescopeFlag := flag.BoolP("telescope", "t", false, "check from telescope")
+
    flag.Parse()
    //deal with non-file path, giving usage message
    if len(os.Args) == 1 {
@@ -113,11 +115,27 @@ func main() {
            return
        }

+       if *telescopeFlag {
+           var urls []string
+           urls = parseFromTelescope()
+           if *jflag {
+
+               checkURLJson(urls)
+           } else {
+
+               fmt.Println()
+               fmt.Println(">>  ***** UrlChecker is working now...... *****  <<")
+               fmt.Println("--------------------------------------------------------------------------------------------------")
+               checkURL(urls)
+           }
+       }
+
        //use for loop to deal with multiple file paths
        i := 1
        for i+1 <= len(os.Args) {

            var urls []string
+
            if os.Args[i][0] != '-' {

                //call functions to check the availability of each url

Enter fullscreen mode Exit fullscreen mode

Top comments (0)