246 lines
4.7 KiB
Go
246 lines
4.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/template/html/v2"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type DummyRecord struct {
|
|
Ts string `json:"ts"`
|
|
}
|
|
|
|
const host = "https://huxley2.azurewebsites.net"
|
|
|
|
func main() {
|
|
|
|
log.Println("Traintimeserver")
|
|
|
|
engine := html.New("./dist", ".html")
|
|
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
})
|
|
|
|
app.Get("/", indexHandler)
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8100"
|
|
}
|
|
|
|
app.Static("/", "./dist")
|
|
|
|
app.Get("/gettrains", func(c *fiber.Ctx) error {
|
|
return getTrains(c)
|
|
})
|
|
|
|
app.Get("/getnexttraintimes", func(c *fiber.Ctx) error {
|
|
return GetNextTrainTimes(c)
|
|
})
|
|
|
|
app.Get("/getroute", func(c *fiber.Ctx) error {
|
|
return NotImplemented(c, "getroute")
|
|
})
|
|
|
|
app.Get("/getnews", func(c *fiber.Ctx) error {
|
|
return NotImplemented(c, "getnews")
|
|
})
|
|
|
|
app.Get("/getservice", func(c *fiber.Ctx) error {
|
|
return NotImplemented(c, "getservice")
|
|
})
|
|
|
|
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
|
|
}
|
|
|
|
func indexHandler(c *fiber.Ctx) error {
|
|
|
|
return c.Render("index", nil)
|
|
}
|
|
|
|
func NotImplemented(c *fiber.Ctx, from string) error {
|
|
|
|
params := c.Queries()
|
|
|
|
fmt.Printf("NotImplemented from: %s\n", from)
|
|
fmt.Printf("-- %+v\n", params)
|
|
|
|
return c.SendString("{}")
|
|
|
|
}
|
|
|
|
func getTrains(c *fiber.Ctx) error {
|
|
|
|
params := c.Queries()
|
|
|
|
log.Println("$$:getTrains")
|
|
log.Printf("-- %+v\n", params)
|
|
|
|
From := c.Query("from", "")
|
|
To := c.Query("to", "")
|
|
|
|
ts := time.Now().String()
|
|
log.Printf("-- %+v\n", ts)
|
|
|
|
log.Printf("-- From: %+v\n", From)
|
|
log.Printf("-- To: %+v\n", To)
|
|
|
|
dummy := DummyRecord{Ts: ts}
|
|
|
|
jsonStr, _ := json.Marshal(dummy)
|
|
|
|
// const url = `/all/${ request.query['from'] }/to/${ request.query['to'] }/10?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8`;
|
|
|
|
if From != "" && To != "" {
|
|
log.Println("-- Got something to search for")
|
|
|
|
// `/all/${ req.query.from }/to/${ req.query.to }/10?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8`;
|
|
url := "/all/" + From + "/to/" + To + "/10?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8"
|
|
|
|
log.Printf("-- url: %+v\n", url)
|
|
|
|
tr := &http.Transport{
|
|
MaxIdleConns: 10,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
DisableCompression: true,
|
|
}
|
|
client := &http.Client{Transport: tr}
|
|
|
|
log.Printf("-- full url: %+v\n", host+url)
|
|
resp, err := client.Get(host + url)
|
|
|
|
if err != nil {
|
|
// handle error
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// log.Printf("-- body: %+v\n", string(body))
|
|
|
|
return c.SendString(string(body))
|
|
}
|
|
|
|
return c.SendString(string(jsonStr))
|
|
}
|
|
|
|
func GetNextTrainTimes(c *fiber.Ctx) error {
|
|
|
|
params := c.Queries()
|
|
|
|
log.Println("$$:GetNextTrainTimes")
|
|
log.Printf("-- %+v\n", params)
|
|
|
|
From := c.Query("from", "")
|
|
To := c.Query("to", "")
|
|
|
|
ts := time.Now().String()
|
|
log.Printf("-- %+v\n", ts)
|
|
|
|
log.Printf("-- From: %+v\n", From)
|
|
log.Printf("-- To: %+v\n", To)
|
|
|
|
dummy := DummyRecord{Ts: ts}
|
|
|
|
jsonStr, _ := json.Marshal(dummy)
|
|
|
|
if From != "" && To != "" {
|
|
log.Println("-- Got something to search for")
|
|
|
|
// `/all/${ req.query.from }/to/${ req.query.to }/10?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8`;
|
|
url := "/next/" + From + "/to/" + To + "?accessToken=215b99fe-b237-4a01-aadc-cf315d6756d8"
|
|
|
|
log.Printf("-- url: %+v\n", url)
|
|
|
|
tr := &http.Transport{
|
|
MaxIdleConns: 10,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
DisableCompression: true,
|
|
}
|
|
client := &http.Client{Transport: tr}
|
|
|
|
log.Printf("-- full url: %+v\n", host+url)
|
|
resp, err := client.Get(host + url)
|
|
|
|
if err != nil {
|
|
// handle error
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var services AllDepartures
|
|
unerr := json.Unmarshal(body, &services)
|
|
|
|
if unerr != nil {
|
|
|
|
log.Println("Failed to Unmarshal json")
|
|
panic(unerr)
|
|
|
|
}
|
|
|
|
departure := reduceNextTrainTimes(services)
|
|
|
|
departJson, derr := json.Marshal(departure)
|
|
|
|
if derr != nil {
|
|
|
|
log.Println("Failed to marshal json")
|
|
panic(derr)
|
|
|
|
}
|
|
|
|
// log.Println("-- body: %+v\n", string(body))
|
|
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
|
|
|
|
return c.SendString(string(departJson))
|
|
// return c.Send(body)
|
|
}
|
|
|
|
return c.SendString(string(jsonStr))
|
|
}
|
|
|
|
func reduceNextTrainTimes(departData AllDepartures) NextTrain {
|
|
|
|
// log.Printf("-- obj?: %+v\n", departData)
|
|
|
|
depart := departData.Departures[0].Service
|
|
// log.Printf("-- depart: %+v\n", depart)
|
|
var output NextTrain
|
|
|
|
if depart.Origin != nil {
|
|
if depart.Sta != "" {
|
|
output.Sta = depart.Sta
|
|
} else {
|
|
output.Sta = depart.Std
|
|
}
|
|
|
|
if depart.Eta != "" {
|
|
output.Eta = depart.Eta
|
|
} else {
|
|
output.Eta = depart.Etd
|
|
}
|
|
|
|
} else {
|
|
output.Eta = "No Service"
|
|
output.Sta = "No Service"
|
|
}
|
|
|
|
// log.Printf("-- output: %+v\n", depart)
|
|
|
|
return output
|
|
}
|