diff options
-rw-r--r-- | funcs.go | 76 | ||||
-rw-r--r-- | http.go | 14 | ||||
-rw-r--r-- | server.go | 11 |
3 files changed, 61 insertions, 40 deletions
diff --git a/funcs.go b/funcs.go index afd2d06..256fcda 100644 --- a/funcs.go +++ b/funcs.go @@ -7,37 +7,69 @@ import ( "net/http" "os" "strings" + "time" ) // 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) + timeNow := time.Now().Unix() - // 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() + // path where the category is stored + categoryPath := fmt.Sprintf("data/%s.txt", categoryName) - // 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) - } + // conditions used to define if a new category should be downloaded or not + var nonexistant bool + var tooOld = false - // 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) + // find out if the category all ready exists + if _, err := os.Stat(categoryPath); os.IsNotExist(err) { + nonexistant = true + } else { + + // find out how old it is + categoryFileStat, _ := os.Stat(categoryPath) + categoryFileModTime := categoryFileStat.ModTime() + categoryFileModTimeUnix := categoryFileModTime.Unix() + if int(timeNow-categoryFileModTimeUnix) > categoryAgeThreshold { + tooOld = true + } } - _, 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) + + // if the local cache is too old or nonexistant, redownload + if tooOld || nonexistant { + + log.Println("[c] cache to old, redownloading...") + + // 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) + } + + log.Println("[c] Done.") } } diff --git a/http.go b/http.go index c1f1e6f..4c77fcb 100644 --- a/http.go +++ b/http.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "log" "net/http" - "os" "strings" tle "git.darknebu.la/Satellite/tle" @@ -30,19 +29,12 @@ func dumpallHandler(w http.ResponseWriter, r *http.Request) { func getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - // read the TLEs from the categories file - // TODO: Handle caching - // - dump all if not present - // - if old, fetch from celestrack - // define where the file containing the spoecified categories lies categoryPath := fmt.Sprintf("data/%s.txt", vars["station"]) - // find out if the file exists or not - if _, err := os.Stat(categoryPath); os.IsNotExist(err) { - // if it does not exist, download the category - downloadCategory(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) diff --git a/server.go b/server.go index 49dfb2b..76085d1 100644 --- a/server.go +++ b/server.go @@ -14,13 +14,9 @@ var ( // Store a list of categories categories []string - // Map a category to it's age on disk - // If a category is downloaded, it is added to this map with it's Unix epoch - // timestamp. - // If the category is too old, re-download it - // TODO: Define what "to old" acutually means and what default value it - // might get - categoryAge = make(map[string]int) + // Threshold defining how old a category must be before it is considered "to + // old" (in seconds) + categoryAgeThreshold int // Define the port the server should listen on port int @@ -44,5 +40,6 @@ func main() { func initFlags() { flag.IntVar(&port, "p", 8080, "the port the server should listen on") + flag.IntVar(&categoryAgeThreshold, "a", 10, "max category age in seconds") flag.Parse() } |