about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-09-25 17:39:07 +0200
committerEmile <hanemile@protonmail.com>2019-09-25 17:39:07 +0200
commitb171b43fd8040de3997961e223fda8209fc256ee (patch)
treed756540fdf3e5b12703a0eb7eb8b00e0839cc8e1
parent75ed2286f21faea346e3005671e1abc2a4cc94a7 (diff)
implemented the printAll endpoint
-rw-r--r--http.go62
-rw-r--r--server.go1
2 files changed, 63 insertions, 0 deletions
diff --git a/http.go b/http.go
index 4c77fcb..9808f07 100644
--- a/http.go
+++ b/http.go
@@ -68,5 +68,67 @@ func getHandler(w http.ResponseWriter, r *http.Request) {
 		fmt.Println("error: ", err)
 	}
 	fmt.Fprintf(w, string(b))
+}
+
+func printAllHandler(w http.ResponseWriter, r *http.Request) {
+	r.ParseForm()
+	dumpall()
+	w.Header().Set("Content-Type", "application/json")
+
+	// read the name of the categories from the categories.txt file and write
+	// the result to the categories slice
+	content, err := ioutil.ReadFile("categories.txt")
+	if err != nil {
+		log.Printf("%s\n", "could not read categories.txt file!")
+	}
+	lines := strings.Split(string(content), "\n")
+
+	for _, line := range lines {
+		if line != "" {
+			categories = append(categories, line)
+		}
+	}
+
+	fmt.Fprintf(w, "[")
 
+	// read the content of all files
+	for j, category := range categories {
+
+		// define where the file containing the spoecified categories lies
+		categoryPath := fmt.Sprintf("data/%s.txt", category)
+
+		// this will only download the category if the file does not exist or if it
+		// is to old
+		downloadCategory(category)
+
+		// open the file containing the data for the individual station
+		content, err := ioutil.ReadFile(categoryPath)
+		if err != nil {
+			log.Printf("Could not open %s", categoryPath)
+		}
+
+		lines := strings.Split(string(content), "\n")
+
+		for i := 0; i < len(lines)-3; i += 3 {
+			RawTLE := strings.Join(lines[i:i+3], "\n")
+
+			JSONTLE, err := tle.NewTLE(RawTLE)
+			if err != nil {
+				fmt.Println(err)
+			}
+
+			// convert the TLE to json
+			b, err := json.MarshalIndent(JSONTLE, "", " ")
+			if err != nil {
+				fmt.Println("error: ", err)
+			}
+
+			if i == len(lines)-4 && j == len(categories)-1 {
+				fmt.Fprintf(w, "%s", string(b))
+			} else {
+				fmt.Fprintf(w, "%s,", string(b))
+			}
+		}
+	}
+	fmt.Fprintf(w, "]")
 }
diff --git a/server.go b/server.go
index 928ca83..86df6a5 100644
--- a/server.go
+++ b/server.go
@@ -31,6 +31,7 @@ func main() {
 
 	r.HandleFunc("/", indexHandler)
 	r.HandleFunc("/dumpall", dumpallHandler)
+	r.HandleFunc("/all", printAllHandler)
 	r.HandleFunc("/{station}/{name}", getHandler)
 
 	// Start the http server using the port provided using command line arguments