-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainServer.go
More file actions
90 lines (80 loc) · 2.9 KB
/
trainServer.go
File metadata and controls
90 lines (80 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"os"
"net/http"
"html"
"database/sql"
"strconv"
//"encoding/json"
"encoding/json"
)
var db *sql.DB = nil
func handleGetNext(rw http.ResponseWriter, request *http.Request) {
fmt.Fprint(rw, "GetNext, %q", html.EscapeString(request.URL.Path))
}
func handlePostPosition(rw http.ResponseWriter, request *http.Request) {
fmt.Fprint(rw, "PostPosition , %q", html.EscapeString(request.URL.Path))
}
func handleGetWhereami(rw http.ResponseWriter, request *http.Request) {
//fmt.Printf("GetWhereami, %q", html.EscapeString(request.URL.Path))
getParameters := request.URL.Query()
latitude, _ := strconv.ParseFloat(getParameters.Get("latitude"), 64)
longitude, _ := strconv.ParseFloat(getParameters.Get("longitude"), 64)
fmt.Printf("Request whereami: %f %f\n", latitude, longitude)
location, _ := getClosestLocationTo(latitude, longitude)
station1, station2 := getStationsSurrounding(location)
//fmt.Fprint(rw, "Stations are " + station1.name + " and " + station2.name + "\n")
type WhereamiResponse struct {
Station1 string
Station2 string
}
jsonResponse, err := json.Marshal(WhereamiResponse {
Station1: station1.name,
Station2: station2.name,
})
if err != nil {
fmt.Println("error:", err)
return
}
//fmt.Fprint(rw, string(jsonResponse))
rw.Header().Set("Content-Type", "application/json")
rw.Write(jsonResponse)
}
func whereAmI(latitude float64, longitude float64) {
// 1. find closest location in Location
// 2. find the route(s) that you're on
// 3. find the stations around this location
location, distance := getClosestLocationTo(latitude, longitude)
fmt.Println("The closest location is: ", location.id, "and distance is: ", distance)
if location.locationType == LocationTypeStation {
fmt.Println("You are at station: ", location.name)
} else {
station1, station2 := getStationsSurrounding(location)
fmt.Println("You are closest to these stations: ", station1.name, " and ", station2.name)
}
}
func main() {
if db == nil {
openDatabase()
defer db.Close()
}
request := os.Args[1]
if request == "server" {
http.HandleFunc("/next", handleGetNext)
http.HandleFunc("/position", handlePostPosition)
http.HandleFunc("/whereami", handleGetWhereami)
http.ListenAndServe(":8083", nil)
} else if request == "route" {
subRequest := os.Args[2]
handleRequest(subRequest)
} else if request == "whereami" {
latitude, _ := strconv.ParseFloat(os.Args[2], 64)
longitude, _ := strconv.ParseFloat(os.Args[3], 64)
//stationComingFrom, stationGoingTo := whereAmI(latitude, longitude)
whereAmI(latitude, longitude)
//fmt.Println("You are coming from: ", getLocationAsString(&stationComingFrom), " and you are going to: ", getLocationAsString(&stationGoingTo));
} else {
fmt.Println("Expected some parameter ...")
}
}