router.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package server
  5. import (
  6. "apiote.xyz/p/szczanieckiej/api"
  7. "apiote.xyz/p/szczanieckiej/config"
  8. "apiote.xyz/p/szczanieckiej/traffic"
  9. traffic_errors "apiote.xyz/p/szczanieckiej/traffic/errors"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "log"
  14. "log/slog"
  15. "net/http"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "golang.org/x/text/language"
  20. "git.sr.ht/~sircmpwn/go-bare"
  21. "github.com/google/uuid"
  22. )
  23. type ServerError struct {
  24. code int
  25. field string
  26. value string
  27. err error
  28. }
  29. func (e ServerError) Error() string {
  30. message := ""
  31. switch e.code {
  32. case http.StatusBadRequest:
  33. message = e.value + " not valid as " + e.field
  34. case http.StatusNotFound:
  35. message = e.value + " not found as " + e.field
  36. default:
  37. message = fmt.Sprintf("error %d", e.code)
  38. if e.field != "" {
  39. message += " in field " + e.field
  40. }
  41. if e.value != "" {
  42. message += " with value " + e.value
  43. }
  44. if e.err != nil {
  45. message += ": " + e.err.Error()
  46. }
  47. }
  48. return message
  49. }
  50. func handleFeed(w http.ResponseWriter, r *http.Request, feedID string, cfg config.Config, t *traffic.Traffic, accept map[uint]struct{}) error {
  51. if _, ok := accept[4]; !ok {
  52. return ServerError{
  53. code: http.StatusNotAcceptable,
  54. }
  55. }
  56. timetable, err := traffic.GetTimetable(feedID, t, cfg)
  57. defer timetable.Close()
  58. if err != nil {
  59. return fmt.Errorf("while getting timetable: %w", err)
  60. }
  61. _, err = io.Copy(w, timetable)
  62. if err != nil {
  63. return fmt.Errorf("while writing: %w", err)
  64. }
  65. return nil
  66. }
  67. func handleLocatables(w http.ResponseWriter, r *http.Request, feedNames []string, cfg config.Config, t *traffic.Traffic, accept map[uint]struct{}) error {
  68. var locatablesSuccess api.LocatablesResponse
  69. if _, ok := accept[0]; ok {
  70. locatablesSuccess = api.LocatablesResponseDev{}
  71. } else if _, ok := accept[3]; ok {
  72. locatablesSuccess = api.LocatablesResponseV3{}
  73. } else if _, ok := accept[2]; ok {
  74. locatablesSuccess = api.LocatablesResponseV2{}
  75. } else if _, ok := accept[1]; ok {
  76. locatablesSuccess = api.LocatablesResponseV1{}
  77. } else {
  78. return ServerError{
  79. code: http.StatusNotAcceptable,
  80. }
  81. }
  82. err := r.ParseForm()
  83. if err != nil {
  84. return fmt.Errorf("while parsing form: %w", err)
  85. }
  86. dateString := r.Form.Get("date")
  87. lb, err := traffic.ParsePosition(r.Form.Get("lb"))
  88. if err != nil {
  89. return ServerError{
  90. code: http.StatusBadRequest,
  91. field: "lb",
  92. value: r.Form.Get("lb"),
  93. err: err,
  94. }
  95. }
  96. rt, err := traffic.ParsePosition(r.Form.Get("rt"))
  97. if err != nil {
  98. return ServerError{
  99. code: http.StatusBadRequest,
  100. field: "rt",
  101. value: r.Form.Get("rt"),
  102. err: err,
  103. }
  104. }
  105. for _, feedName := range feedNames {
  106. versionCode, _, err := parseDate(dateString, feedName, t)
  107. if err != nil {
  108. return fmt.Errorf("while parsing date: %w", err)
  109. }
  110. context := traffic.Context{
  111. DataHome: cfg.FeedsPath,
  112. FeedID: feedName,
  113. Version: versionCode,
  114. }
  115. stops, err := traffic.GetStopsIn(lb, rt, context, t)
  116. if err != nil {
  117. return fmt.Errorf("while getting stops in bounding box: %w", err)
  118. }
  119. vehicles, err := traffic.GetVehiclesIn(lb, rt, context, t)
  120. if err != nil {
  121. return fmt.Errorf("while getting vehicles in bounding box: %w", err)
  122. }
  123. locatables := []traffic.Locatable{}
  124. for _, stop := range stops {
  125. locatables = append(locatables, stop)
  126. }
  127. for _, vehicle := range vehicles {
  128. locatables = append(locatables, vehicle)
  129. }
  130. if _, ok := accept[0]; ok {
  131. locatablesSuccess, err = api.CreateSuccessLocatablesV3(locatables, context, t, locatablesSuccess)
  132. } else if _, ok := accept[3]; ok {
  133. locatablesSuccess, err = api.CreateSuccessLocatablesV3(locatables, context, t, locatablesSuccess)
  134. } else if _, ok := accept[2]; ok {
  135. locatablesSuccess, err = api.CreateSuccessLocatablesV2(locatables, context, t, locatablesSuccess)
  136. } else if _, ok := accept[1]; ok {
  137. locatablesSuccess, err = api.CreateSuccessLocatables(locatables, context, t, locatablesSuccess)
  138. } else {
  139. return ServerError{
  140. code: http.StatusNotAcceptable,
  141. }
  142. }
  143. if err != nil {
  144. return fmt.Errorf("while creating locatablesSuccess from near locatables: %w", err)
  145. }
  146. }
  147. bytes, err := bare.Marshal(&locatablesSuccess)
  148. if err != nil {
  149. return fmt.Errorf("while marshaling: %w", err)
  150. }
  151. _, err = w.Write(bytes)
  152. if err != nil {
  153. return fmt.Errorf("while writing: %w", err)
  154. }
  155. return nil
  156. }
  157. func handleQueryables(w http.ResponseWriter, r *http.Request, feedNames []string, cfg config.Config, t *traffic.Traffic, accept map[uint]struct{}) error {
  158. var queryablesSuccess api.QueryablesResponse
  159. if _, ok := accept[0]; ok {
  160. queryablesSuccess = api.QueryablesResponseDev{}
  161. } else if _, ok := accept[4]; ok {
  162. queryablesSuccess = api.QueryablesResponseV4{}
  163. } else if _, ok := accept[3]; ok {
  164. queryablesSuccess = api.QueryablesResponseV3{}
  165. } else if _, ok := accept[2]; ok {
  166. queryablesSuccess = api.QueryablesResponseV2{}
  167. } else if _, ok := accept[1]; ok {
  168. queryablesSuccess = api.QueryablesResponseV1{}
  169. } else {
  170. return ServerError{
  171. code: http.StatusNotAcceptable,
  172. }
  173. }
  174. err := r.ParseForm()
  175. if err != nil {
  176. return fmt.Errorf("while parsing form: %w", err)
  177. }
  178. query := r.Form.Get("q")
  179. near := r.Form.Get("near")
  180. dateString := r.Form.Get("date")
  181. limitString := r.Form.Get("limit")
  182. if limitString == "" {
  183. limitString = "12"
  184. }
  185. limit, err := strconv.ParseUint(limitString, 10, 0)
  186. if err != nil {
  187. return ServerError{
  188. code: http.StatusBadRequest,
  189. field: "limit",
  190. value: limitString,
  191. }
  192. }
  193. offsetString := r.Form.Get("offset")
  194. if offsetString == "" {
  195. offsetString = "0"
  196. }
  197. offset, err := strconv.ParseUint(offsetString, 10, 0)
  198. if err != nil {
  199. return ServerError{
  200. code: http.StatusBadRequest,
  201. field: "offset",
  202. value: offsetString,
  203. }
  204. }
  205. for _, feedName := range feedNames {
  206. versionCode, _, err := parseDate(dateString, feedName, t)
  207. if err != nil {
  208. return fmt.Errorf("while parsing date: %w", err)
  209. }
  210. context := traffic.Context{
  211. DataHome: cfg.FeedsPath,
  212. FeedID: feedName,
  213. Version: versionCode,
  214. }
  215. if near != "" {
  216. location, err := traffic.ParsePosition(near)
  217. if err != nil {
  218. return ServerError{
  219. code: http.StatusBadRequest,
  220. field: "near",
  221. value: near,
  222. err: err,
  223. }
  224. }
  225. stops, err := traffic.GetStopsNear(location, context, t)
  226. if err != nil {
  227. return fmt.Errorf("while getting near stops: %w", err)
  228. }
  229. items := []traffic.Queryable{}
  230. for _, stop := range stops {
  231. items = append(items, stop)
  232. }
  233. if _, ok := accept[0]; ok {
  234. queryablesSuccess, err = api.CreateSuccessQueryablesV4(near, items, context, t, queryablesSuccess, true)
  235. } else if _, ok := accept[4]; ok {
  236. queryablesSuccess, err = api.CreateSuccessQueryablesV4(near, items, context, t, queryablesSuccess, true)
  237. } else if _, ok := accept[3]; ok {
  238. queryablesSuccess, err = api.CreateSuccessQueryablesV3(near, items, context, t, queryablesSuccess, true)
  239. } else if _, ok := accept[2]; ok {
  240. queryablesSuccess, err = api.CreateSuccessQueryablesV2(near, items, context, t, queryablesSuccess, true)
  241. } else if _, ok := accept[1]; ok {
  242. queryablesSuccess, err = api.CreateSuccessQueryables(items, context, t, queryablesSuccess, true)
  243. } else {
  244. return ServerError{
  245. code: http.StatusNotAcceptable,
  246. }
  247. }
  248. if err != nil {
  249. return fmt.Errorf("while creating stopsSuccess from near stops: %w", err)
  250. }
  251. } else {
  252. ix := t.CodeIndexes[feedName][versionCode]
  253. code := query
  254. _, exists := ix[code]
  255. if exists {
  256. stop, err := traffic.GetStop(code, context, t)
  257. if err != nil {
  258. return fmt.Errorf("while getting stop: %w", err)
  259. }
  260. if _, ok := accept[0]; ok {
  261. queryablesSuccess, err = api.CreateSuccessQueryablesV4(query, []traffic.Queryable{stop}, context, t, queryablesSuccess, false)
  262. } else if _, ok := accept[4]; ok {
  263. queryablesSuccess, err = api.CreateSuccessQueryablesV4(query, []traffic.Queryable{stop}, context, t, queryablesSuccess, false)
  264. } else if _, ok := accept[3]; ok {
  265. queryablesSuccess, err = api.CreateSuccessQueryablesV3(query, []traffic.Queryable{stop}, context, t, queryablesSuccess, false)
  266. } else if _, ok := accept[2]; ok {
  267. queryablesSuccess, err = api.CreateSuccessQueryablesV2(query, []traffic.Queryable{stop}, context, t, queryablesSuccess, false)
  268. } else if _, ok := accept[1]; ok {
  269. queryablesSuccess, err = api.CreateSuccessQueryables([]traffic.Queryable{stop}, context, t, queryablesSuccess, false)
  270. } else {
  271. return ServerError{
  272. code: http.StatusNotAcceptable,
  273. }
  274. }
  275. if err != nil {
  276. return fmt.Errorf("while creating stopsSuccess from code: %w", err)
  277. }
  278. } else {
  279. query, err = traffic.CleanQuery(query, t.Feeds[feedName])
  280. if err != nil {
  281. return fmt.Errorf("while cleaning query: %w", err)
  282. }
  283. lines, err1 := traffic.QueryLines(query, cfg.FeedsPath, feedName, versionCode, t)
  284. stops, err2 := traffic.QueryStops(query, context, t)
  285. if err1 != nil && err2 != nil {
  286. return fmt.Errorf("while querying stops and lines: %w", errors.Join(err1, err2))
  287. }
  288. items := []traffic.Queryable{}
  289. for _, line := range lines {
  290. items = append(items, line)
  291. }
  292. for _, stop := range stops {
  293. items = append(items, stop)
  294. }
  295. if _, ok := accept[0]; ok {
  296. queryablesSuccess, err = api.CreateSuccessQueryablesV4(query, items, context, t, queryablesSuccess, false)
  297. } else if _, ok := accept[4]; ok {
  298. queryablesSuccess, err = api.CreateSuccessQueryablesV4(query, items, context, t, queryablesSuccess, false)
  299. } else if _, ok := accept[3]; ok {
  300. queryablesSuccess, err = api.CreateSuccessQueryablesV3(query, items, context, t, queryablesSuccess, false)
  301. } else if _, ok := accept[2]; ok {
  302. queryablesSuccess, err = api.CreateSuccessQueryablesV2(query, items, context, t, queryablesSuccess, false)
  303. } else if _, ok := accept[1]; ok {
  304. queryablesSuccess, err = api.CreateSuccessQueryables(items, context, t, queryablesSuccess, false)
  305. } else {
  306. return ServerError{
  307. code: http.StatusNotAcceptable,
  308. }
  309. }
  310. if err != nil {
  311. return fmt.Errorf("while creating stopsSuccess from lines and stops: %w", err)
  312. }
  313. }
  314. }
  315. }
  316. queryablesSuccess = api.LimitQueryables(queryablesSuccess, offset, limit)
  317. bytes, err := bare.Marshal(&queryablesSuccess)
  318. if err != nil {
  319. return fmt.Errorf("while marshaling: %w", err)
  320. }
  321. _, err = w.Write(bytes)
  322. if err != nil {
  323. return fmt.Errorf("while writing: %w", err)
  324. }
  325. return nil
  326. }
  327. func handleDepartures(w http.ResponseWriter, r *http.Request, feedName string, cfg config.Config, t *traffic.Traffic, accept map[uint]struct{}) error {
  328. err := r.ParseForm()
  329. if err != nil {
  330. return fmt.Errorf("while parsing form: %w", err)
  331. }
  332. code := r.Form.Get("code")
  333. if code == "" {
  334. return ServerError{
  335. code: http.StatusBadRequest,
  336. field: "code",
  337. value: "EMPTY",
  338. }
  339. }
  340. dateString := r.Form.Get("date")
  341. line := r.Form.Get("line")
  342. lineID := r.Form.Get("lineID")
  343. limitString := r.Form.Get("limit")
  344. if limitString == "" {
  345. limitString = "12"
  346. }
  347. limit, err := strconv.ParseUint(limitString, 10, 0)
  348. if err != nil {
  349. return ServerError{
  350. code: http.StatusBadRequest,
  351. field: "limit",
  352. value: limitString,
  353. }
  354. }
  355. offsetString := r.Form.Get("offset")
  356. if offsetString == "" {
  357. offsetString = "0"
  358. }
  359. offset, err := strconv.ParseUint(offsetString, 10, 0)
  360. if err != nil {
  361. return ServerError{
  362. code: http.StatusBadRequest,
  363. field: "offset",
  364. value: offsetString,
  365. }
  366. }
  367. departuresType := traffic.DEPARTURES_FULL
  368. if dateString == "" {
  369. departuresType = traffic.DEPARTURES_HYBRID
  370. }
  371. versionCode, date, err := parseDate(dateString, feedName, t)
  372. if err != nil {
  373. return err
  374. }
  375. context := traffic.Context{
  376. DataHome: cfg.FeedsPath,
  377. FeedID: feedName,
  378. Version: versionCode,
  379. }
  380. ix := t.CodeIndexes[feedName][versionCode]
  381. _, exists := ix[code]
  382. if !exists {
  383. return ServerError{
  384. code: http.StatusNotFound,
  385. field: "code",
  386. value: string(code),
  387. }
  388. }
  389. stop, err := traffic.GetStop(code, context, t)
  390. if err != nil {
  391. return fmt.Errorf("while getting stop: %w", err)
  392. }
  393. if lineID == "" {
  394. l, _ := traffic.GetLineOld(line, context, t)
  395. lineID = l.Id
  396. }
  397. acceptLanguage := r.Header.Get("Accept-Language")
  398. if acceptLanguage == "" {
  399. acceptLanguage, err = traffic.GetLanguage(context)
  400. if err != nil {
  401. log.Printf("while gettng default language: %v\n", err)
  402. acceptLanguage = "und"
  403. }
  404. }
  405. preferredLanguages, _, err := language.ParseAcceptLanguage(acceptLanguage)
  406. if err != nil {
  407. return ServerError{
  408. code: http.StatusBadRequest,
  409. field: "Accept-Language",
  410. value: acceptLanguage,
  411. err: err,
  412. }
  413. }
  414. departures, err := traffic.GetDepartures(code, lineID, context, t, date, departuresType, preferredLanguages)
  415. if err != nil {
  416. if _, ok := err.(traffic_errors.NoSchedule); ok {
  417. return ServerError{
  418. code: http.StatusNotFound,
  419. field: "date",
  420. value: dateString,
  421. }
  422. } else {
  423. return fmt.Errorf("while getting departures: %w", err)
  424. }
  425. }
  426. if departuresType == traffic.DEPARTURES_HYBRID {
  427. if int(offset) > len(departures) {
  428. departures = []traffic.DepartureRealtime{}
  429. } else if len(departures) < int(offset+limit) {
  430. departures = departures[offset:]
  431. } else {
  432. departures = departures[offset : offset+limit]
  433. }
  434. }
  435. stopAlerts := traffic.GetAlerts(stop.Id, stop.Code, -1, context, t, preferredLanguages)
  436. if err != nil {
  437. return fmt.Errorf("while getting alerts: %w", err)
  438. }
  439. var success api.DeparturesResponse
  440. if _, ok := accept[0]; ok {
  441. success, err = api.CreateSuccessDeparturesDev(stop, departures, date, t.Vehicles[feedName][versionCode], stopAlerts, context, t, accept, preferredLanguages)
  442. } else if _, ok := accept[4]; ok {
  443. success, err = api.CreateSuccessDeparturesV4(stop, departures, date, t.Vehicles[feedName][versionCode], stopAlerts, context, t, accept, preferredLanguages)
  444. } else if _, ok := accept[3]; ok {
  445. success, err = api.CreateSuccessDeparturesV3(stop, departures, date, t.Vehicles[feedName][versionCode], stopAlerts, context, t, accept, preferredLanguages)
  446. } else if _, ok := accept[2]; ok {
  447. success, err = api.CreateSuccessDeparturesV2(stop, departures, date, t.Vehicles[feedName][versionCode], stopAlerts, context, t, accept, preferredLanguages)
  448. } else if _, ok := accept[1]; ok {
  449. success, err = api.CreateSuccessDeparturesV1(stop, departures, date, t.Vehicles[feedName][versionCode], stopAlerts, context, t, accept, preferredLanguages)
  450. } else {
  451. return ServerError{
  452. code: http.StatusNotAcceptable,
  453. }
  454. }
  455. if err != nil {
  456. return fmt.Errorf("while creating departuresSuccess: %w", err)
  457. }
  458. bytes, err := bare.Marshal(&success)
  459. if err != nil {
  460. return fmt.Errorf("while marshaling: %w", err)
  461. }
  462. _, err = w.Write(bytes)
  463. if err != nil {
  464. return fmt.Errorf("while writing: %w", err)
  465. }
  466. return nil
  467. }
  468. func handleTrip(w http.ResponseWriter, r *http.Request, feedName string, cfg config.Config, t *traffic.Traffic, accept uint) error {
  469. path := strings.Split(r.URL.Path[1:], "/")
  470. if len(path) == 3 {
  471. dateString := r.Form.Get("date")
  472. versionCode, _, err := parseDate(dateString, feedName, t)
  473. if err != nil {
  474. return err
  475. }
  476. tripID := path[2]
  477. stopCode := r.Form.Get("stop")
  478. context := traffic.Context{
  479. DataHome: cfg.FeedsPath,
  480. FeedID: feedName,
  481. Version: versionCode,
  482. }
  483. trip, err := traffic.GetTripFromStop(tripID, stopCode, context, t)
  484. if err != nil {
  485. return fmt.Errorf("while getting line: %w", err)
  486. }
  487. if len(trip) == 0 {
  488. return ServerError{
  489. code: http.StatusNotFound,
  490. field: "line",
  491. value: tripID,
  492. }
  493. }
  494. success := struct{}{} //api.CreateSuccessTrip(trip)
  495. bytes, err := bare.Marshal(&success)
  496. if err != nil {
  497. return fmt.Errorf("while marshaling trip: %w", err)
  498. }
  499. _, err = w.Write(bytes)
  500. if err != nil {
  501. return fmt.Errorf("while writing: %w", err)
  502. }
  503. } else {
  504. return ServerError{
  505. code: http.StatusNotFound,
  506. field: "trip",
  507. value: "EMPTY",
  508. }
  509. }
  510. return nil
  511. }
  512. func sendError(w http.ResponseWriter, r *http.Request, err error) {
  513. var (
  514. se ServerError
  515. response api.ErrorResponse
  516. )
  517. if !errors.As(err, &se) {
  518. se = ServerError{
  519. code: http.StatusInternalServerError,
  520. err: err,
  521. }
  522. }
  523. response = api.ErrorResponse{
  524. Field: se.field,
  525. Message: se.Error(),
  526. }
  527. log.Println(err.Error())
  528. b, err := bare.Marshal(&response)
  529. if err != nil {
  530. w.WriteHeader(http.StatusInternalServerError)
  531. return
  532. }
  533. w.WriteHeader(se.code)
  534. w.Write(b)
  535. }
  536. func Route(cfg config.Config, traffic *traffic.Traffic, logger *slog.Logger) *http.Server {
  537. srv := &http.Server{Addr: cfg.ListenAddress}
  538. http.DefaultServeMux = &http.ServeMux{}
  539. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  540. skidderID := uuid.New().String()
  541. logger = logger.With(slog.Attr{Key: "skidderID", Value: slog.StringValue(skidderID)})
  542. log.Printf("%s %s?%s\n", r.Method, r.URL.RawPath, r.URL.RawQuery)
  543. accept, err := parseAccept(r.Header.Values("Accept"))
  544. if err != nil {
  545. sendError(w, r, fmt.Errorf("while parsing accept: %w", err))
  546. return
  547. }
  548. if r.URL.Path[1:] == "" {
  549. err = handleFeeds(w, r, cfg, traffic, accept)
  550. } else {
  551. path := strings.Split(r.URL.Path[1:], "/")
  552. feedNames := strings.Split(path[0], ",")
  553. for _, feedName := range feedNames {
  554. if traffic.Versions[feedName] == nil {
  555. sendError(w, r, ServerError{
  556. code: http.StatusNotFound,
  557. field: "feed",
  558. value: feedName,
  559. })
  560. return
  561. }
  562. }
  563. if len(path) == 1 {
  564. if len(feedNames) > 1 {
  565. err = ServerError{
  566. code: http.StatusBadRequest,
  567. field: "feed",
  568. value: path[0],
  569. }
  570. } else {
  571. err = handleFeed(w, r, feedNames[0], cfg, traffic, accept)
  572. }
  573. } else {
  574. resource := path[1]
  575. switch resource {
  576. case "queryables":
  577. err = handleQueryables(w, r, feedNames, cfg, traffic, accept)
  578. case "locatables":
  579. err = handleLocatables(w, r, feedNames, cfg, traffic, accept)
  580. case "departures":
  581. if len(feedNames) > 1 {
  582. err = ServerError{
  583. code: http.StatusBadRequest,
  584. field: "feed",
  585. value: path[0],
  586. }
  587. break
  588. }
  589. err = handleDepartures(w, r, feedNames[0], cfg, traffic, accept)
  590. case "lines":
  591. if len(feedNames) > 1 {
  592. err = ServerError{
  593. code: http.StatusBadRequest,
  594. field: "feed",
  595. value: path[0],
  596. }
  597. break
  598. }
  599. err = handleLine(w, r, feedNames[0], cfg, traffic, accept)
  600. /*case "trips":
  601. if len(feedNames) > 1 {
  602. err = ServerError{
  603. code: http.StatusBadRequest,
  604. field: "feed",
  605. value: path[0],
  606. }
  607. break
  608. }
  609. err = handleTrip(w, r, feedNames[0], cfg, traffic)*/
  610. // todo(BAF21, BAF11): "shape" (line_id/trip_id)
  611. default:
  612. err = ServerError{
  613. code: http.StatusNotFound,
  614. field: "resource",
  615. value: resource,
  616. }
  617. }
  618. }
  619. }
  620. if err != nil {
  621. sendError(w, r, err)
  622. }
  623. })
  624. go func() {
  625. if err := srv.ListenAndServe(); err != http.ErrServerClosed {
  626. log.Printf("ListenAndServe(): %v\n", err)
  627. os.Exit(1)
  628. }
  629. }()
  630. return srv
  631. }