Skip to content

Commit

Permalink
Feature/typescript migration (#5)
Browse files Browse the repository at this point in the history
* feat: js -> ts

* fix: eslint

* ref: better types naming, some simplifications

* ref: generic APIResponse
  • Loading branch information
SharkEzz authored Jan 3, 2022
1 parent d9b9e17 commit ebf260a
Show file tree
Hide file tree
Showing 25 changed files with 1,186 additions and 146 deletions.
7 changes: 7 additions & 0 deletions dto/Dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dto

type Response struct {
Status int
Message string
Data interface{}
}
6 changes: 0 additions & 6 deletions dto/Error.go

This file was deleted.

6 changes: 3 additions & 3 deletions dto/Tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type TrackingResponse struct {
}

type ObservationWsResponse struct {
SatelliteName string
Visible bool
GeneratedAt time.Time
SatName string
Visible bool
GeneratedAt time.Time
sgp4.Observation
}
42 changes: 27 additions & 15 deletions handlers/TrackingHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func HandlePostTracking(c *fiber.Ctx, db *gorm.DB, validator *validator.Validate
err := validator.Struct(requestData)
if err != nil {
c.Response().SetStatusCode(http.StatusBadRequest)
return c.JSON(dto.Error{
return c.JSON(dto.Response{
Status: http.StatusBadRequest,
Message: err.Error(),
})
Expand All @@ -30,7 +30,7 @@ func HandlePostTracking(c *fiber.Ctx, db *gorm.DB, validator *validator.Validate
tle, err := services.GetTLEFromDatabase(requestData.CatNBR, db)
if err != nil {
c.Response().SetStatusCode(http.StatusNotFound)
return c.JSON(dto.Error{
return c.JSON(dto.Response{
Status: http.StatusNotFound,
Message: "Satellite not found",
})
Expand All @@ -39,18 +39,26 @@ func HandlePostTracking(c *fiber.Ctx, db *gorm.DB, validator *validator.Validate
sgp4, err := sgp4.NewSGP4(tle)
if err != nil {
c.Response().SetStatusCode(http.StatusInternalServerError)
return c.JSON(dto.Error{
return c.JSON(dto.Response{
Status: http.StatusInternalServerError,
Message: "Error while initializing SGP4, please try again later",
})
}

observation := sgp4.ObservationFromLocation(requestData.Lat, requestData.Lng, requestData.Alt)

return c.JSON(dto.TrackingResponse{
trackingResponse := dto.TrackingResponse{
Observation: observation,
GeneratedAt: time.Now(),
})
}

response := dto.Response{
Status: http.StatusOK,
Message: "",
Data: trackingResponse,
}

return c.JSON(response)
}

func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {
Expand All @@ -62,7 +70,7 @@ func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {
return
}

tle, err := services.GetTLEFromDatabase(int(catNbr), db)
tle, err := services.GetTLEFromDatabase(catNbr, db)
if err != nil {
handleError(c, http.StatusNotFound, "TLE not found")
return
Expand All @@ -76,24 +84,28 @@ func HandleWsTracking(c *websocket.Conn, db *gorm.DB) {

for {
observation := sgp4.ObservationFromLocation(lat, lng, alt)
responseDto := dto.ObservationWsResponse{
SatelliteName: strings.TrimSpace(tle.Name()),
Visible: observation.Elevation > 0,
GeneratedAt: time.Now().UTC(),
Observation: observation,
observationResponseDto := dto.ObservationWsResponse{
SatName: strings.TrimSpace(tle.Name()),
Visible: observation.Elevation > 0,
GeneratedAt: time.Now().UTC(),
Observation: observation,
}
response := dto.Response{
Status: http.StatusOK,
Data: observationResponseDto,
}
if err := c.WriteJSON(responseDto); err != nil {
if err := c.WriteJSON(response); err != nil {
return
}
if _, _, err = c.ReadMessage(); err != nil {
if _, _, err := c.ReadMessage(); err != nil {
return
}
time.Sleep(time.Second)
}
}

func handleError(c *websocket.Conn, status uint, message string) {
c.WriteJSON(dto.Error{
func handleError(c *websocket.Conn, status int, message string) {
c.WriteJSON(dto.Response{
Status: status,
Message: message,
})
Expand Down
1 change: 1 addition & 0 deletions js/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vite.config.ts
12 changes: 9 additions & 3 deletions js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"es2021": true
},
"extends": [
"airbnb",
"airbnb-typescript",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:react-hooks/recommended",
Expand All @@ -15,11 +19,13 @@
"jsx": true
},
"ecmaVersion": 12,
"sourceType": "module"
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["react", "react-hooks", "prettier"],
"plugins": ["react", "react-hooks", "prettier", "@typescript-eslint"],
"rules": {
"prettier/prettier": "error",
"react/jsx-props-no-spreading": 0
"react/jsx-props-no-spreading": 0,
"no-restricted-syntax": ["error", "FunctionExpression", "WithStatement"]
}
}
2 changes: 1 addition & 1 deletion js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
<script type="module" src="/src/main.tsx"></script>
</body>

</html>
Loading

0 comments on commit ebf260a

Please sign in to comment.