/
handler.go
162 lines (138 loc) · 3.71 KB
/
handler.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
package hxxp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path/filepath"
"time"
"github.com/hiendv/geojson/internal/hxxp/api.v1"
"github.com/hiendv/geojson/internal/hxxp/ctxx"
"github.com/hiendv/geojson/internal/hxxp/middleware"
"github.com/hiendv/geojson/internal/osm"
"github.com/hiendv/geojson/pkg/util"
"github.com/julienschmidt/httprouter"
)
// Handler is the application HTTP handler.
type Handler struct {
ctx context.Context
router *httprouter.Router
}
// Listen opens and serves an HTTP handler.
func Listen(h *Handler) error {
if h == nil {
return errors.New("invalid handler")
}
log := ctxLog(h.ctx)
address, ok := ctxAddress(h.ctx)
if !ok {
return errors.New("missing serving address")
}
srv := &http.Server{
Addr: address,
Handler: h,
ReadTimeout: 10 * time.Second,
WriteTimeout: 40 * time.Second,
}
log.Infow("serving", "address", address)
return srv.ListenAndServe()
}
// New constructs a new Handler.
func New(ctx context.Context) (handler *Handler, err error) {
dir, ok := ctxOutDir(ctx)
if !ok {
return nil, errors.New("invalid output directory")
}
router := httprouter.New()
handler = &Handler{ctx: ctx, router: router}
osmContext, err := osm.NewContext(ctx, ctxLog(ctx), false, false, dir, false)
if err != nil {
return nil, err
}
v1SubAreas, err := v1.SubAreas(ctxLog(ctx), osmContext, handler)
if err != nil {
return nil, err
}
prefix, ok := ctxPrefix(ctx)
if !ok {
prefix = "/"
}
router.GET("/", func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
util.HTTPRespond(w, []byte(`Hello`))
})
router.ServeFiles(fmt.Sprintf("%s/%s/*filepath", prefix, filepath.Base(dir)), http.Dir(dir))
router.GET("/api/v1/subareas/:id", v1SubAreas.Query)
return
}
// ServeHTTP serves HTTP requests.
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// exclude the static serving from middleware
_, params, _ := h.router.Lookup(r.Method, r.URL.Path)
for _, param := range params {
if param.Key == "filepath" {
h.router.ServeHTTP(w, r)
return
}
}
origin, ok := ctxOrigin(h.ctx)
if !ok {
util.HTTPAbort(w, "missing origin", http.StatusInternalServerError)
return
}
rate, ok := ctxRate(h.ctx)
if !ok {
util.HTTPAbort(w, "missing rate-limiting configuration", http.StatusInternalServerError)
return
}
rateBurst, ok := ctxRateBurst(h.ctx)
if !ok {
util.HTTPAbort(w, "missing rate-limiting configuration (burst)", http.StatusInternalServerError)
return
}
rateTTL, ok := ctxRateTTL(h.ctx)
if !ok {
util.HTTPAbort(w, "missing rate-limiting configuration (TTL)", http.StatusInternalServerError)
return
}
middleware.IP(
middleware.RateLimit(
middleware.CORS(
h.router,
),
rate,
rateBurst,
rateTTL,
),
).ServeHTTP(
w,
r.WithContext( // request-scoped context
ctxx.SetOrigin(r.Context(), origin),
),
)
}
// JSON is a helper to encode data in JSON format.
func (h Handler) JSON(data io.ReadCloser, result interface{}) error {
return json.NewDecoder(data).Decode(&result)
}
// Abort is a helper to respond HTTP requests with errors.
func (h Handler) Abort(w http.ResponseWriter, message string, code int) {
util.HTTPAbort(w, message, code)
}
// Respond is a helper to respond HTTP requests.
func (h Handler) Respond(w http.ResponseWriter, message string, data interface{}) {
util.HTTPRespondJSON(w, message, data)
}
// Error is a helper to abort HTTP requests with errors.
func (h Handler) Error(w http.ResponseWriter, err error, code int) {
h.Abort(w, err.Error(), code)
}
// Static is a helper to serve static files.
func (h Handler) Static(path string) string {
prefix, ok := ctxPrefix(h.ctx)
if !ok {
return path
}
return filepath.Join(prefix, path)
}