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", "

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") dumpall() log.Println("[+] Done dumping all TLEs") } // get Handler returns the specified TLE func getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) // define where the file containing the spoecified categories lies categoryPath := fmt.Sprintf("data/%s.txt", vars["station"]) // this will only download the category if the file does not exist or if it // is to old downloadCategory(vars["station"]) // 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") // iterate over all lines searching for the satellite var j int for i, line := range lines { if strings.Contains(line, vars["name"]) { j = i break } } RawTLE := strings.Join(lines[j:j+3], "\n") w.Header().Set("Content-Type", "application/json") 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)) } 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, "]") }