Skip to content

Commit

Permalink
fix: replace fmt by log & dynamic TLE list url
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Dec 30, 2021
1 parent 4b26163 commit c2229d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@ import (
)

const (
AppName = "SatTrack"
appName string = "SatTrack"
)

var (
shouldUpdate = flag.Bool("update", false, "Use this flag to update all the TLE")
Version = "no_version"
tleListURL = flag.String("tleUrl", "https://celestrak.com/NORAD/elements/active.txt", "default URL to fetch TLEs")
version = "devel"
)

func main() {
flag.Parse()

db := database.Init("database/local.db")

validator := validator.New()

if *shouldUpdate {
services.UpdateDatabase(db)
services.UpdateDatabase(*tleListURL, db)
}

validator := validator.New()

app := fiber.New(fiber.Config{
DisableStartupMessage: true,
AppName: appName,
})

app.Use("/ws", func(c *fiber.Ctx) error {
Expand All @@ -46,7 +48,7 @@ func main() {
})

app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(AppName)
return c.SendString(appName + "🛰️")
})

app.Post("/tracking", func(c *fiber.Ctx) error {
Expand All @@ -57,6 +59,6 @@ func main() {
handlers.HandleWsTracking(c, db)
}))

log.Println("Started", AppName, Version)
log.Println("Started", appName, version)
app.Listen(":8000")
}
21 changes: 8 additions & 13 deletions services/TLEService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package services

import (
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
Expand All @@ -13,16 +13,13 @@ import (
"gorm.io/gorm"
)

// CelesTrack active satellites list
const activeListUrl string = "https://celestrak.com/NORAD/elements/active.txt"

// Fetch all active satellite from CelesTrack and update the satellites table
func UpdateDatabase(db *gorm.DB) error {
fmt.Println("Updating TLE database")
fmt.Println("Deleting record from `tles` table")
// Fetch all active satellite a defined URL and update the tles table
func UpdateDatabase(url string, db *gorm.DB) error {
start := time.Now()
log.Println("Starting TLE updater")
db.Exec("DELETE FROM tles")
fmt.Println("Querying new TLE list")
res, err := http.Get(activeListUrl)
log.Println("Querying new TLE list")
res, err := http.Get(url)
if err != nil {
return err
}
Expand All @@ -35,8 +32,6 @@ func UpdateDatabase(db *gorm.DB) error {
return errors.New("invalid TLE count")
}

fmt.Println("Inserting new records")
start := time.Now()
satellites := []models.TLE{}
for i := 0; i < len(contentArr); i += 3 {
tle, err := sgp4.NewTLE(contentArr[i][:24], contentArr[i+1][:69], contentArr[i+2][:69])
Expand All @@ -53,7 +48,7 @@ func UpdateDatabase(db *gorm.DB) error {
}
db.Create(&satellites)

fmt.Printf("Inserted %v TLE in %s\n", len(contentArr)/3, time.Since(start))
log.Default().Printf("Updated %v TLE in %s\n", len(contentArr)/3, time.Since(start).Round(time.Millisecond))

return nil
}
Expand Down

0 comments on commit c2229d1

Please sign in to comment.