From 205c92b576475a3224d88bfef7aa927ca29e08c1 Mon Sep 17 00:00:00 2001 From: Emile Date: Mon, 23 Sep 2019 12:39:30 +0200 Subject: moved code moved the code from the dumpallHandler into the dumpall function located in the funcs.go file --- funcs.go | 26 ++++++++++++++++++++++++++ http.go | 27 +++------------------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/funcs.go b/funcs.go index 5ab913c..fbcd62d 100644 --- a/funcs.go +++ b/funcs.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "strings" ) // downloadCategory downloads the given category from celestrack @@ -39,3 +40,28 @@ func downloadCategory(categoryName string) { fmt.Printf("Could not write to file data/%s.txt", categoryName) } } + +// dumpall dumps all the categories into the data folder +func dumpall() { + // 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) + } + } + + // download all the categories to the data/ directory + for _, category := range categories { + fmt.Fprintf(w, "downloading %s... ", category) + log.Printf("downloading %s... ", category) + downloadCategory(category) + fmt.Fprintf(w, "done.\n") + } +} diff --git a/http.go b/http.go index ece4fde..d1cfb48 100644 --- a/http.go +++ b/http.go @@ -14,35 +14,14 @@ import ( // indexHandler displays an index page func indexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "%s", "

Hello World

") + fmt.Fprintf(w, "%s", "

TLE2JSON api

") + fmt.Fprintf(w, "%s", "Usage: Make a get request to /{station}/{name}") } // dumpallHandler dumps all categories func dumpallHandler(w http.ResponseWriter, r *http.Request) { log.Println("[ ] Dumping all TLEs") - - // 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) - } - } - - // download all the categories to the data/ directory - for _, category := range categories { - fmt.Fprintf(w, "downloading %s... ", category) - log.Printf("downloading %s... ", category) - downloadCategory(category) - fmt.Fprintf(w, "done.\n") - } - + dumpall() log.Println("[+] Done dumping all TLEs") } -- cgit 1.4.1