From 9e821ef30bb8f45e70dd75ba857659baedb79091 Mon Sep 17 00:00:00 2001 From: Emile Date: Thu, 5 Sep 2019 20:52:19 +0200 Subject: split up the package into multiple files --- funcs.go | 41 +++++++++++++++++++++++++ http.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 102 -------------------------------------------------------------- 3 files changed, 120 insertions(+), 102 deletions(-) create mode 100644 funcs.go create mode 100644 http.go diff --git a/funcs.go b/funcs.go new file mode 100644 index 0000000..5ab913c --- /dev/null +++ b/funcs.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "os" +) + +// downloadCategory downloads the given category from celestrack +func downloadCategory(categoryName string) { + + // define where to download the TLEs from + base := "https://celestrak.com/NORAD/elements/" + downloadURL := fmt.Sprintf("%s%s.txt", base, categoryName) + + // define the HTTP GET request and make it + resp, err := http.Get(downloadURL) + if err != nil { + log.Printf("Could not download %s", categoryName) + } + defer resp.Body.Close() + + // read the respose from the GET request + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Printf("Could not read the category %s", categoryName) + } + + // write the response to the corresponding file + filename := fmt.Sprintf("data/%s.txt", categoryName) + f, err := os.Create(filename) // creating the file + if err != nil { + fmt.Printf("Could not create file data/%s.txt", categoryName) + } + _, err = f.WriteString(string(body)) // writing the TLE to the file + if err != nil { + fmt.Printf("Could not write to file data/%s.txt", categoryName) + } +} diff --git a/http.go b/http.go new file mode 100644 index 0000000..ca3c413 --- /dev/null +++ b/http.go @@ -0,0 +1,79 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" + + TLE "git.darknebu.la/Satellite/TLE" + "github.com/gorilla/mux" +) + +// indexHandler displays an index page +func indexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%s", "

Hello World

") +} + +// 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", "could not read categories.txt file!") + } + lines := strings.Split(string(content), "\n") + + for _, line := range lines { + categories = append(categories, line) + } + + // download all the categories to the data/ directory + for _, category := range categories { + downloadCategory(category) + } + + log.Println("[+] Done dumping all TLEs") +} + +// get Handler returns the specified TLE +func getHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + // read the TLEs from the categories file + //content, err := ioutil.ReadFile("categories.txt") + //if err != nil { + // log.Println("Could note read categories.txt") + //} + + // handle the response according to the query parameter "format" + // if ?format=json is given, return + if r.URL.Query().Get("format") == "json" { + w.Header().Set("Content-Type", "application/json") + + RawTLE := `ISS (ZARYA) +1 25544U 98067A 19229.39083552 .00000228 00000-0 11917-4 0 9993 +2 25544 51.6447 57.6210 0007373 294.0868 138.8050 15.50381554184754` + + 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) + } + fmt.Fprintf(w, string(b)) + //fmt.Fprintf(w, "{\"response\": \"Getting the TLE for %s/%s as json\"}", vars["station"], vars["name"]) + } else { + fmt.Fprintf(w, "Getting the TLE for %s/%s raw", vars["station"], vars["name"]) + } + +} diff --git a/server.go b/server.go index b1faa8b..69d528b 100644 --- a/server.go +++ b/server.go @@ -1,17 +1,12 @@ package main import ( - "encoding/json" "flag" "fmt" - "io/ioutil" "log" - "os" - "strings" "net/http" - TLE "git.darknebu.la/Satellite/TLE" "github.com/gorilla/mux" ) @@ -35,100 +30,3 @@ func initFlags() { flag.IntVar(&port, "p", 8080, "the port the server should listen on") flag.Parse() } - -func indexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "%s", "

Hello World

") -} - -// 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", "could not read categories.txt file!") - } - lines := strings.Split(string(content), "\n") - - for _, line := range lines { - categories = append(categories, line) - } - - // download all the categories to the data/ directory - for _, category := range categories { - downloadCategory(category) - } - - log.Println("[+] Done dumping all TLEs") -} - -// downloadCategory downloads the given category from celestrack -func downloadCategory(categoryName string) { - - // define where to download the TLEs from - base := "https://celestrak.com/NORAD/elements/" - downloadURL := fmt.Sprintf("%s%s.txt", base, categoryName) - - // define the HTTP GET request and make it - resp, err := http.Get(downloadURL) - if err != nil { - log.Printf("Could not download %s", categoryName) - } - defer resp.Body.Close() - - // read the respose from the GET request - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Printf("Could not read the category %s", categoryName) - } - - // write the response to the corresponding file - filename := fmt.Sprintf("data/%s.txt", categoryName) - f, err := os.Create(filename) // creating the file - if err != nil { - fmt.Printf("Could not create file data/%s.txt", categoryName) - } - _, err = f.WriteString(string(body)) // writing the TLE to the file - if err != nil { - fmt.Printf("Could not write to file data/%s.txt", categoryName) - } -} - -// get Handler returns the specified TLE -func getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - - // read the TLEs from the categories file - //content, err := ioutil.ReadFile("categories.txt") - //if err != nil { - // log.Println("Could note read categories.txt") - //} - - // handle the response according to the query parameter "format" - // if ?format=json is given, return - if r.URL.Query().Get("format") == "json" { - w.Header().Set("Content-Type", "application/json") - - RawTLE := `ISS (ZARYA) -1 25544U 98067A 19229.39083552 .00000228 00000-0 11917-4 0 9993 -2 25544 51.6447 57.6210 0007373 294.0868 138.8050 15.50381554184754` - - 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) - } - fmt.Fprintf(w, string(b)) - //fmt.Fprintf(w, "{\"response\": \"Getting the TLE for %s/%s as json\"}", vars["station"], vars["name"]) - } else { - fmt.Fprintf(w, "Getting the TLE for %s/%s raw", vars["station"], vars["name"]) - } - -} -- cgit 1.4.1