Clean up + add webserver
This commit is contained in:
parent
2eed16376f
commit
67e2e4d306
7 changed files with 216 additions and 68 deletions
82
main.go
82
main.go
|
|
@ -1,75 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"log"
|
||||
|
||||
"github.com/golang-io/requests"
|
||||
"github.com/tkrajina/gpxgo/gpx"
|
||||
"git.readonly.ch/bouzoure/gpx-downloader/routes"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/helmet/v2"
|
||||
)
|
||||
|
||||
type SuisseMobileGeometry struct {
|
||||
Segments [][][]float64 `json:"coordinates"`
|
||||
}
|
||||
|
||||
type SuisseMobileProperties struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type SuisseMobileResponse struct {
|
||||
Properties SuisseMobileProperties `json:"properties"`
|
||||
Geometry SuisseMobileGeometry `json:"geometry"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
sess := requests.New(requests.URL("https://schweizmobil.ch"))
|
||||
resp, _ := sess.DoRequest(context.Background(),
|
||||
requests.Path("/api/6/tracks/1856337628"),
|
||||
)
|
||||
// Create fiber config
|
||||
appConfig := fiber.Config{}
|
||||
|
||||
var response SuisseMobileResponse
|
||||
err := json.Unmarshal(resp.Content.Bytes(), &response)
|
||||
// Init fiber webapp with config
|
||||
app := fiber.New(appConfig)
|
||||
|
||||
// Apply middlewares
|
||||
app.Use(helmet.New())
|
||||
|
||||
// Routes
|
||||
app.Get("/", routes.Index)
|
||||
app.Post("/fetch", routes.Fetch)
|
||||
|
||||
err := app.Listen("127.0.0.1:3000")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var gpxFile gpx.GPX
|
||||
var gpxTrack gpx.GPXTrack
|
||||
|
||||
gpxFile.Name = response.Properties.Name
|
||||
|
||||
for _, segments := range response.Geometry.Segments {
|
||||
var gpxTrackSegment gpx.GPXTrackSegment
|
||||
|
||||
for _, point := range segments {
|
||||
fmt.Println(point)
|
||||
var gpxPoint gpx.GPXPoint
|
||||
|
||||
gpxPoint.Longitude = point[0]
|
||||
gpxPoint.Latitude = point[1]
|
||||
gpxPoint.Elevation = *gpx.NewNullableFloat64(point[2])
|
||||
|
||||
gpxTrackSegment.AppendPoint(&gpxPoint)
|
||||
}
|
||||
|
||||
gpxTrack.AppendSegment(&gpxTrackSegment)
|
||||
}
|
||||
|
||||
gpxFile.AppendTrack(&gpxTrack)
|
||||
xml, err := gpxFile.ToXml(gpx.ToXmlParams{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
file, err := os.Create("1856337628.gpx")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = file.Write(xml)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue