Skip to content

Commit

Permalink
ref: move ws request validation outside handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Dec 30, 2021
1 parent e6aa152 commit 9a2fdce
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
3 changes: 3 additions & 0 deletions database/Database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"github.com/SharkEzz/sattrack/database/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand All @@ -13,5 +14,7 @@ func Init(path string) *gorm.DB {
panic("failed to connect to the database")
}

db.AutoMigrate(&models.TLE{})

return db
}
40 changes: 11 additions & 29 deletions handlers/TrackingHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package handlers

import (
"net/http"
"strconv"
"time"

"github.com/SharkEzz/sattrack/dto"
"github.com/SharkEzz/sattrack/services"
"github.com/SharkEzz/sattrack/validation"
"github.com/SharkEzz/sgp4"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -54,42 +54,22 @@ func HandlePostTracking(c *fiber.Ctx, db *gorm.DB, validator *validator.Validate

func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {
defer c.Close()
var (
qCatNbr = c.Query("catNbr")
qLat = c.Query("lat")
qLng = c.Query("lng")
qAlt = c.Query("alt")
)
catNbr, err := strconv.ParseInt(qCatNbr, 10, 64)
if err != nil {
handleError(c, "Invalid CatNbr")
return
}
lat, err := strconv.ParseFloat(qLat, 64)
if err != nil {
handleError(c, "Invalid Latitude")
return
}
lng, err := strconv.ParseFloat(qLng, 64)
if err != nil {
handleError(c, "Invalid Longitude")
return
}
alt, err := strconv.ParseFloat(qAlt, 64)
catNbr, lat, lng, alt, err := validation.ValidateWebsocketQuery(c)

if err != nil {
handleError(c, "Invalid Altitude")
handleError(c, http.StatusBadRequest, err.Error())
return
}

tle, err := services.GetTLEFromDatabase(int(catNbr), db)
if err != nil {
handleError(c, "TLE not found")
handleError(c, http.StatusNotFound, "TLE not found")
return
}

sgp4, err := sgp4.NewSGP4(tle)
if err != nil {
handleError(c, "Error while initializing SGP4, try again later")
handleError(c, http.StatusInternalServerError, "Error while initializing SGP4, try again later")
return
}

Expand All @@ -103,7 +83,9 @@ func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {
}
}

func handleError(c *websocket.Conn, errStr string) {
c.WriteMessage(1, []byte(errStr))
c.Close()
func handleError(c *websocket.Conn, status uint, message string) {
c.WriteJSON(dto.Error{
Status: status,
Message: message,
})
}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"flag"

"github.com/SharkEzz/sattrack/database"
"github.com/SharkEzz/sattrack/database/models"
"github.com/SharkEzz/sattrack/handlers"
"github.com/SharkEzz/sattrack/services"

"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
Expand All @@ -19,7 +19,6 @@ var (
func main() {
flag.Parse()
db := database.Init("database/local.db")
db.AutoMigrate(&models.TLE{})

validator := validator.New()

Expand Down
36 changes: 36 additions & 0 deletions validation/Tracking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package validation

import (
"errors"
"strconv"

"github.com/gofiber/websocket/v2"
)

func ValidateWebsocketQuery(c *websocket.Conn) (int, float64, float64, float64, error) {
var (
qCatNbr = c.Query("catNbr")
qLat = c.Query("lat")
qLng = c.Query("lng")
qAlt = c.Query("alt")
)

catNbr, err := strconv.ParseInt(qCatNbr, 10, 64)
if err != nil {
return 0, 0, 0, 0, errors.New("invalid catnbr")
}
lat, err := strconv.ParseFloat(qLat, 64)
if err != nil {
return 0, 0, 0, 0, errors.New("invalid latitude")
}
lng, err := strconv.ParseFloat(qLng, 64)
if err != nil {
return 0, 0, 0, 0, errors.New("invalid longitude")
}
alt, err := strconv.ParseFloat(qAlt, 64)
if err != nil {
return 0, 0, 0, 0, errors.New("invalid altitude")
}

return int(catNbr), lat, lng, alt, nil
}

0 comments on commit 9a2fdce

Please sign in to comment.