forked from CrunchyData/pg_tileserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
426 lines (364 loc) · 11.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"time"
// REST routing
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
// Database connectivity
"github.com/jackc/pgx/v4/pgxpool"
// Logging
log "github.com/sirupsen/logrus"
// Configuration
"github.com/pborman/getopt/v2"
"github.com/spf13/viper"
)
// programName is the name string we use
const programName string = "pg_tileserv"
// programVersion is the version string we use
const programVersion string = "0.1"
// worldMercWidth is the width of the Web Mercator plane
const worldMercWidth float64 = 40075016.6855784
// globalDb is a global database connection pointer
var globalDb *pgxpool.Pool = nil
// globalVersions holds the parsed output of postgis_full_version()
var globalVersions map[string]string = nil
// globalPostGISVersion is numeric, sortable postgis version (3.2.1 => 3002001)
var globalPostGISVersion int = 0
/******************************************************************************/
func init() {
viper.SetDefault("DbConnection", "sslmode=disable")
viper.SetDefault("HttpHost", "0.0.0.0")
viper.SetDefault("HttpPort", os.Getenv("PORT"))
viper.SetDefault("UrlBase", "")
viper.SetDefault("DefaultResolution", 4096)
viper.SetDefault("DefaultBuffer", 256)
viper.SetDefault("MaxFeaturesPerTile", 10000)
viper.SetDefault("DefaultMinZoom", 0)
viper.SetDefault("DefaultMaxZoom", 22)
viper.SetDefault("Debug", false)
viper.SetDefault("AssetsPath", "./assets")
// 1d, 1h, 1m, 1s, see https://golang.org/pkg/time/#ParseDuration
viper.SetDefault("DbPoolMaxConnLifeTime", "1h")
viper.SetDefault("DbPoolMaxConns", 4)
viper.SetDefault("DbTimeout", 10)
viper.SetDefault("CORSOrigins", "*")
}
func main() {
// Read the commandline
flagDebugOn := getopt.BoolLong("debug", 'd', "log debugging information")
flagConfigFile := getopt.StringLong("config", 'c', "", "full path to config file", "config.toml")
flagHelpOn := getopt.BoolLong("help", 'h', "display help output")
flagVersionOn := getopt.BoolLong("version", 'v', "display version number")
getopt.Parse()
if *flagHelpOn {
getopt.PrintUsage(os.Stdout)
os.Exit(1)
}
if *flagVersionOn {
fmt.Printf("%s %s\n", programName, programVersion)
os.Exit(0)
}
// Commandline over-rides config file for debugging
if *flagDebugOn {
viper.Set("Debug", true)
log.SetLevel(log.TraceLevel)
}
if *flagConfigFile != "" {
viper.SetConfigFile(*flagConfigFile)
} else {
viper.SetConfigName(programName)
viper.AddConfigPath("./config")
viper.AddConfigPath("/config")
viper.AddConfigPath("/etc")
}
// Report our status
log.Infof("%s %s", programName, programVersion)
log.Info("Run with --help parameter for commandline options")
// Read environment configuration first
if dbUrl := os.Getenv("DATABASE_URL"); dbUrl != "" {
viper.Set("DbConnection", dbUrl)
log.Info("Using database connection info from environment variable DATABASE_URL")
}
log.Infof("Serving at %s:%d", viper.GetString("HttpHost"), viper.GetInt("HttpPort"))
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Debugf("viper.ConfigFileNotFoundError: %s", err)
} else {
if _, ok := err.(viper.UnsupportedConfigError); ok {
log.Debugf("viper.UnsupportedConfigError: %s", err)
} else {
log.Fatalf("Configuration file error: %s", err)
}
}
} else {
// Really would like to log location of filename we found...
// log.Infof("Reading configuration file %s", cf)
if cf := viper.ConfigFileUsed(); cf != "" {
log.Infof("Using config file: %s", cf)
} else {
log.Info("Config file: none found, using defaults")
}
}
// Load the global layer list right away
// Also connects to database
if err := LoadLayers(); err != nil {
log.Fatal(err)
}
// Read the postgis_full_version string and store
// in a global for version testing
if errv := LoadVersions(); errv != nil {
log.Fatal(errv)
}
log.WithFields(log.Fields{
"event": "connect",
"topic": "versions",
"postgis": globalVersions["POSTGIS"],
"geos": globalVersions["GEOS"],
"pgsql": globalVersions["PGSQL"],
"libprotobuf": globalVersions["LIBPROTOBUF"],
}).Debugf("Connected to PostGIS version %s\n", globalVersions["POSTGIS"])
// Get to work
handleRequests()
}
/******************************************************************************/
func requestPreview(w http.ResponseWriter, r *http.Request) error {
lyrId := mux.Vars(r)["name"]
log.WithFields(log.Fields{
"event": "request",
"topic": "layerpreview",
"key": lyrId,
}).Tracef("requestPreview: %s", lyrId)
// reqProperties := r.FormValue("properties")
// reqLimit := r.FormValue("limit")
// reqResolution := r.FormValue("resolution")
// reqBuffer := r.FormValue("buffer")
// Refresh the layers list
if err := LoadLayers(); err != nil {
return err
}
// Get the requested layer
lyr, errLyr := GetLayer(lyrId)
if errLyr != nil {
return errLyr
}
switch lyr.(type) {
case LayerTable:
tmpl, err := template.ParseFiles(fmt.Sprintf("%s/preview-table.html", viper.GetString("AssetsPath")))
if err != nil {
return err
}
l, _ := lyr.(LayerTable)
tmpl.Execute(w, l)
case LayerFunction:
tmpl, err := template.ParseFiles(fmt.Sprintf("%s/preview-function.html", viper.GetString("AssetsPath")))
if err != nil {
return err
}
l, _ := lyr.(LayerFunction)
tmpl.Execute(w, l)
default:
return errors.New("unknown layer type") // never get here
}
return nil
}
func requestListHtml(w http.ResponseWriter, r *http.Request) error {
log.WithFields(log.Fields{
"event": "request",
"topic": "layerlist",
}).Trace("requestListHtml")
// Update the global in-memory list from
// the database
if err := LoadLayers(); err != nil {
return err
}
jsonLayers := GetJsonLayers(r)
t, err := template.ParseFiles(fmt.Sprintf("%s/index.html", viper.GetString("AssetsPath")))
if err != nil {
return err
}
t.Execute(w, jsonLayers)
return nil
}
func requestListJson(w http.ResponseWriter, r *http.Request) error {
log.WithFields(log.Fields{
"event": "request",
"topic": "layerlist",
}).Trace("requestListJson")
// Update the global in-memory list from
// the database
if err := LoadLayers(); err != nil {
return err
}
w.Header().Add("Content-Type", "application/json")
jsonLayers := GetJsonLayers(r)
json.NewEncoder(w).Encode(jsonLayers)
return nil
}
func requestDetailJson(w http.ResponseWriter, r *http.Request) error {
lyrId := mux.Vars(r)["name"]
log.WithFields(log.Fields{
"event": "request",
"topic": "layerdetail",
}).Tracef("requestDetailJson(%s)", lyrId)
// Refresh the layers list
if err := LoadLayers(); err != nil {
return err
}
lyr, errLyr := GetLayer(lyrId)
if errLyr != nil {
return errLyr
}
errWrite := lyr.WriteLayerJson(w, r)
if errWrite != nil {
return errWrite
}
return nil
}
func requestTile(w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
lyr, errLyr := GetLayer(vars["name"])
if errLyr != nil {
return errLyr
}
tile, errTile := makeTile(vars)
if errTile != nil {
return errTile
}
log.WithFields(log.Fields{
"event": "request",
"topic": "tile",
"key": tile.String(),
}).Tracef("RequestLayerTile: %s", tile.String())
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("DbTimeout")*time.Second)
defer cancel()
tilerequest := lyr.GetTileRequest(tile, r)
mvt, errMvt := DBTileRequest(ctx, &tilerequest)
if errMvt != nil {
return errMvt
}
w.Header().Add("Content-Type", "application/vnd.mapbox-vector-tile")
if _, errWrite := w.Write(mvt); errWrite != nil {
return errWrite
}
return nil
}
/******************************************************************************/
// tileAppError is an optional error structure functions can return
// if they want to specify the particular HTTP error code to be used
// in their error return
type tileAppError struct {
HttpCode int
SrcErr error
Topic string
Message string
}
// Error prints out a reasonable string format
func (tae tileAppError) Error() string {
if tae.Message != "" {
return fmt.Sprintf("%s\n%s", tae.Message, tae.SrcErr.Error())
}
return fmt.Sprintf("%s", tae.SrcErr.Error())
}
// tileAppHandler is a function handler that can replace the
// existing handler and provide richer error handling, see below and
// https://blog.golang.org/error-handling-and-go
type tileAppHandler func(w http.ResponseWriter, r *http.Request) error
// ServeHTTP logs as much useful information as possible in
// a field format for potential Json logging streams
// as well as returning HTTP error response codes on failure
// so clients can see what is going on
// TODO: return JSON document body for the HTTP error
func (fn tileAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"method": r.Method,
"url": r.URL,
}).Infof("%s %s", r.Method, r.URL)
if err := fn(w, r); err != nil {
if hdr, ok := r.Header["x-correlation-id"]; ok {
log.WithField("correlation-id", hdr[0])
}
if e, ok := err.(tileAppError); ok {
if e.HttpCode == 0 {
e.HttpCode = 500
}
if e.Topic != "" {
log.WithField("topic", e.Topic)
}
log.WithField("key", e.Message)
log.WithField("src", e.SrcErr.Error())
log.Error(err)
http.Error(w, e.Error(), e.HttpCode)
} else {
log.Error(err)
http.Error(w, err.Error(), 500)
}
}
}
/******************************************************************************/
func TileRouter() *mux.Router {
// creates a new instance of a mux router
r := mux.NewRouter().StrictSlash(true)
// Front page and layer list
r.Handle("/", tileAppHandler(requestListHtml))
r.Handle("/index.html", tileAppHandler(requestListHtml))
r.Handle("/index.json", tileAppHandler(requestListJson))
// Layer detail and demo pages
r.Handle("/{name}.html", tileAppHandler(requestPreview))
r.Handle("/{name}.json", tileAppHandler(requestDetailJson))
// Tile requests
r.Handle("/{name}/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.{ext}", tileAppHandler(requestTile))
return r
}
func handleRequests() {
// Get a configured router
r := TileRouter()
// Allow CORS from anywhere
corsOrigins := viper.GetString("CORSOrigins")
corsOpt := handlers.AllowedOrigins([]string{corsOrigins})
// Set a writeTimeout for the http server.
// This value is the application's DbTimeout config setting plus a
// grace period. The additional time allows the application to gracefully
// handle timeouts on its own, canceling outstanding database queries and
// returning an error to the client, while keeping the http.Server
// WriteTimeout as a fallback.
writeTimeout := (viper.GetDuration("DbTimeout") + 5) * time.Second
// more "production friendly" timeouts
// https://blog.simon-frey.eu/go-as-in-golang-standard-net-http-config-will-break-your-production/#You_should_at_least_do_this_The_easy_path
s := &http.Server{
ReadTimeout: 1 * time.Second,
WriteTimeout: writeTimeout,
Addr: fmt.Sprintf("%s:%d", viper.GetString("HttpHost"), viper.GetInt("HttpPort")),
Handler: handlers.CompressHandler(handlers.CORS(corsOpt)(r)),
}
// start http service
go func() {
// ListenAndServe returns http.ErrServerClosed when the server receives
// a call to Shutdown(). Other errors are unexpected.
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// wait here for interrupt signal
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
// Interrupt signal received: Start shutting down
log.Infoln("Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
defer cancel()
s.Shutdown(ctx)
if globalDb != nil {
log.Debugln("Closing DB connections")
globalDb.Close()
}
log.Infoln("Server stopped.")
}
/******************************************************************************/