-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
223 lines (202 loc) · 6.62 KB
/
main.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"log"
"math/rand"
"michikusa_back/types"
"net/http"
"os"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geo"
)
func main() {
// 環境変数の読み込み
odptAPIKey := os.Getenv("ODPT_API_KEY")
if odptAPIKey == "" {
panic("Error loading ODPT_API_KEY")
}
yahooAPIKey := os.Getenv("YAHOO_API_KEY")
if yahooAPIKey == "" {
panic("Error loading YAHOO_API_KEY")
}
e := echo.New()
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://michikusa-front.pages.dev", "http://localhost:5173"},
AllowMethods: []string{http.MethodGet},
}))
// 「現在地」の情報を受け取って、最寄駅や行き先駅、施設の情報を返すAPI
e.GET("/", func(c echo.Context) error {
var req types.InitialRequestData
if err := c.Bind(&req); err != nil {
// todo: fix エラーハンドリング
log.Println(err)
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Bad Request",
})
}
log.Println("GET / with", req)
nearestStation, err := GetNearestStation(req.Longitude, req.Latitude, odptAPIKey)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getNearestStation)",
})
}
stationList, err := GetStationList(nearestStation, odptAPIKey)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getStationList)",
})
}
railwayInfo, err := GetRailwayInfo(nearestStation.Railway, odptAPIKey)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getRailwayInfo)",
})
}
var filteredStationList []types.OdptStation
if req.Price != 0 {
railwayFares, err := GetRailwayFare(nearestStation, odptAPIKey)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getRailwayFare)",
})
}
fareMap := make(map[string]int)
for _, railwayFare := range railwayFares {
fareMap[railwayFare.ToStation] = railwayFare.TicketFare
}
for _, station := range stationList {
if fare, ok := fareMap[station.SameAs]; ok && fare <= req.Price {
filteredStationList = append(filteredStationList, station)
}
}
} else {
filteredStationList = stationList
}
if len(filteredStationList) == 0 {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "No station is available for travel at the specified fare",
})
}
// 乱数で行き先駅を選択
destinationStation := filteredStationList[rand.Intn(len(filteredStationList))]
// neaest->destinationまでの駅数
var neaestStationIndex int
var destinationStationIndex int
for i, station := range railwayInfo.StationOrder {
if station.Station == nearestStation.SameAs {
neaestStationIndex = i
}
if station.Station == destinationStation.SameAs {
destinationStationIndex = i
}
}
var numberOfStations int
if neaestStationIndex < destinationStationIndex {
numberOfStations = destinationStationIndex - neaestStationIndex
} else {
numberOfStations = neaestStationIndex - destinationStationIndex
}
facilityList, err := GetFacility(types.LocationsRequestData{
Latitude: destinationStation.Lat,
Longitude: destinationStation.Long,
}, yahooAPIKey)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getFacility)",
})
}
var facilities []types.Facility
for _, facility := range facilityList {
lat, _ := strconv.ParseFloat(strings.Split(facility.Geometry.Coordinates, ",")[1], 64)
long, _ := strconv.ParseFloat(strings.Split(facility.Geometry.Coordinates, ",")[0], 64)
var genre string
if facility.Property.Genre == nil || len(facility.Property.Genre) == 0 {
genre = "その他"
} else {
genre = facility.Property.Genre[0].Name
}
facilities = append(facilities, types.Facility{
Name: facility.Name,
Distance: int(geo.Distance(orb.Point{destinationStation.Lat, destinationStation.Long}, orb.Point{lat, long})),
Genre: genre,
Latitude: lat,
Longitude: long,
MapURL: "https://map.yahoo.co.jp/v2/place/" + facility.Gid,
})
}
var res types.InitialResponseData
res.NearestStation = types.Station{
Name: nearestStation.Title,
Latitude: nearestStation.Lat,
Longitude: nearestStation.Long,
StationCode: nearestStation.StationCode,
}
res.DestinationStation = types.Station{
Name: destinationStation.Title,
Latitude: destinationStation.Lat,
Longitude: destinationStation.Long,
StationCode: destinationStation.StationCode,
}
res.RailwayName = railwayInfo.Title
res.RailwayColor = railwayInfo.Color
res.Facilities = facilities
res.NumerOfStations = numberOfStations
return c.JSON(http.StatusOK, res)
})
// 「行き先駅」の情報を受け取って、付近の施設の情報を返すAPI
e.GET("/location-list", func(c echo.Context) error {
var req types.LocationsRequestData
if err := c.Bind(&req); err != nil {
// todo: fix エラーハンドリング
log.Println(err)
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Bad Request",
})
}
log.Println("GET /location-list with", req)
facilityList, err := GetFacility(req, yahooAPIKey)
if err != nil {
log.Println(err)
if err.Error() == "no facilities found" {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "No facilities found",
})
}
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal Server Error (getFacility)",
})
}
var facilities []types.Facility
for _, facility := range facilityList {
coords := strings.Split(facility.Geometry.Coordinates, ",")
lat, _ := strconv.ParseFloat(coords[1], 64)
long, _ := strconv.ParseFloat(coords[0], 64)
facilities = append(facilities, types.Facility{
Name: facility.Name,
Distance: int(geo.Distance(orb.Point{req.Latitude, req.Longitude}, orb.Point{lat, long})),
Genre: facility.Property.Genre[0].Name,
Latitude: lat,
Longitude: long,
MapURL: "https://map.yahoo.co.jp/v2/place/" + facility.Gid,
})
}
return c.JSON(http.StatusOK, types.LocationsResponseData{
Facilities: facilities,
})
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
e.Logger.Fatal(e.Start(":" + port))
}