aboutsummaryrefslogtreecommitdiffstats
path: root/http/http.go
blob: 09a3064a47217621a168ac537512a34a4c50ff11 (plain) (blame)
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
package http

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"path/filepath"
	"strconv"
	"time"

	"github.com/mpolden/atb/cache"
	"github.com/mpolden/atb/entur"
)

const (
	inbound  = "inbound"
	outbound = "outbound"
)

// Server represents an Server server.
type Server struct {
	Entur *entur.Client
	CORS  bool
	cache *cache.Cache
	ttl
}

type ttl struct {
	departures time.Duration
	stops      time.Duration
}

func urlPrefix(r *http.Request) string {
	host := r.Host
	if host == "" {
		host = r.RemoteAddr
	}
	scheme := "http"
	if r.TLS != nil {
		scheme = "https"
	}
	forwardedProto := r.Header["X-Forwarded-Proto"]
	if len(forwardedProto) > 0 {
		scheme = forwardedProto[0]
	}
	url := url.URL{Scheme: scheme, Host: host}
	return url.String()
}

func filterDepartures(departures []Departure, direction string) []Departure {
	switch direction {
	case inbound, outbound:
		copy := make([]Departure, 0, len(departures))
		for _, d := range departures {
			towardsCentrum := *d.TowardsCentrum
			if direction == inbound && !towardsCentrum {
				continue
			}
			if direction == outbound && towardsCentrum {
				continue
			}
			copy = append(copy, d)
		}
		return copy
	}
	return departures
}

func (s *Server) enturDepartures(urlPrefix string, stopID int, direction string) (Departures, bool, error) {
	cacheKey := strconv.Itoa(stopID)
	cached, hit := s.cache.Get(cacheKey)
	var departures Departures
	if hit {
		departures = cached.(Departures)
	} else {
		enturDepartures, err := s.Entur.Departures(25, stopID)
		if err != nil {
			return Departures{}, hit, err
		}
		departures = convertDepartures(enturDepartures)
		departures.URL = fmt.Sprintf("%s/api/v2/departures/%d", urlPrefix, stopID)
		s.cache.Set(cacheKey, departures, s.ttl.departures)
	}
	departures.Departures = filterDepartures(departures.Departures, direction)
	return departures, hit, nil
}

func (s *Server) setCacheHeader(w http.ResponseWriter, hit bool) {
	v := "MISS"
	if hit {
		v = "HIT"
	}
	w.Header().Set("X-Cache", v)
}

// DepartureHandlerV2 is a handler which retrieves departures for a given bus stop through Entur.
func (s *Server) DepartureHandlerV2(w http.ResponseWriter, r *http.Request) (interface{}, *Error) {
	stopID, err := strconv.Atoi(filepath.Base(r.URL.Path))
	if err != nil {
		return nil, &Error{
			err:     err,
			Status:  http.StatusBadRequest,
			Message: "Invalid stop ID. Use https://stoppested.entur.org/ to find stop IDs.",
		}
	}
	direction := r.URL.Query().Get("direction")
	departures, hit, err := s.enturDepartures(urlPrefix(r), stopID, direction)
	if err != nil {
		return nil, &Error{
			err:     err,
			Status:  http.StatusInternalServerError,
			Message: "Failed to get departures from Entur",
		}
	}
	s.setCacheHeader(w, hit)
	return departures, nil
}

// DefaultHandler lists known URLs.
func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) (interface{}, *Error) {
	if r.URL.Path != "/" {
		return nil, &Error{Status: http.StatusNotFound, Message: "Resource not found"}
	}
	prefix := urlPrefix(r)
	departuresV2URL := fmt.Sprintf("%s/api/v2/departures", prefix)
	return struct {
		URLs []string `json:"urls"`
	}{
		[]string{departuresV2URL},
	}, nil
}

// New returns a new Server using given clients to communicate with AtB and Entur. stopTTL and departureTTL control the
// cache TTL bus stops and departures.
func New(entur *entur.Client, stopTTL, departureTTL time.Duration, cors bool) *Server {
	cache := cache.New(time.Minute)
	return &Server{
		Entur: entur,
		CORS:  cors,
		cache: cache,
		ttl: ttl{
			stops:      stopTTL,
			departures: departureTTL,
		},
	}
}

type appHandler func(http.ResponseWriter, *http.Request) (interface{}, *Error)

func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	data, e := fn(w, r)
	if e != nil { // e is *Error, not os.Error.
		if e.err != nil {
			log.Print(e.err)
		}
		out, err := json.Marshal(e)
		if err != nil {
			// Should never happen
			panic(err)
		}
		w.WriteHeader(e.Status)
		w.Write(out)
	} else {
		out, err := json.Marshal(data)
		if err != nil {
			panic(err)
		}
		w.Write(out)
	}
}

func requestFilter(next http.Handler, cors bool) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		if cors {
			w.Header().Set("Access-Control-Allow-Methods", "GET")
			w.Header().Set("Access-Control-Allow-Origin", "*")
		}
		next.ServeHTTP(w, r)
	})
}

// Handler returns a root handler for the API.
func (s *Server) Handler() http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/api/v2/departures", appHandler(s.DepartureHandlerV2))
	mux.Handle("/api/v2/departures/", appHandler(s.DepartureHandlerV2))
	mux.Handle("/", appHandler(s.DefaultHandler))
	return requestFilter(mux, s.CORS)
}

// ListenAndServe listens on the TCP network address addr and serves the API.
func (s *Server) ListenAndServe(addr string) error {
	return http.ListenAndServe(addr, s.Handler())
}