|
1 | 1 | const path = require('path') |
2 | | -const config = require('./config') |
3 | | -const fastify = require('fastify')({ logger: config.logger}) |
| 2 | +const fastify = require('fastify')({ logger: process.env.SERVER_LOGGER || false}) |
| 3 | +require('dotenv').config() |
4 | 4 |
|
5 | | -// postgres connection |
6 | | -fastify.register(require('fastify-postgres'), { |
7 | | - connectionString: process.env.POSTGRES_CONNECTION || config.db |
| 5 | +// EXIT IF POSTGRES_CONNECTION ENV VARIABLE NOT SET |
| 6 | +if (!("POSTGRES_CONNECTION" in process.env)) { |
| 7 | + throw new Error("Required ENV variable POSTGRES_CONNECTION is not set. Please see README.md for more information."); |
| 8 | +} |
| 9 | + |
| 10 | +// POSTGRES CONNECTION |
| 11 | +fastify.register(require('@fastify/postgres'), { |
| 12 | + connectionString: process.env.POSTGRES_CONNECTION |
8 | 13 | }) |
9 | 14 |
|
10 | | -// compression - add x-protobuf |
| 15 | +// COMPRESSION |
| 16 | +// add x-protobuf |
11 | 17 | fastify.register( |
12 | | - require('fastify-compress'), |
| 18 | + require('@fastify/compress'), |
13 | 19 | { customTypes: /x-protobuf$/ } |
14 | 20 | ) |
15 | 21 |
|
16 | | -// cache |
| 22 | +// CACHE SETTINGS |
17 | 23 | fastify.register( |
18 | | - require('fastify-caching'), { |
19 | | - privacy: config.cachePrivacy || 'private', |
20 | | - expiresIn: config.cache, |
21 | | - serverExpiresIn: config.serverCache |
| 24 | + require('@fastify/caching'), { |
| 25 | + privacy: process.env.CACHE_PRIVACY || 'private', |
| 26 | + expiresIn: process.env.CACHE_EXPIRESIN || 3600, |
| 27 | + serverExpiresIn: process.env.CACHE_SERVERCACHE |
22 | 28 | } |
23 | 29 | ) |
24 | 30 |
|
25 | 31 | // CORS |
26 | | -fastify.register(require('fastify-cors')) |
| 32 | +fastify.register(require('@fastify/cors')) |
27 | 33 |
|
28 | | -// swagger |
29 | | -fastify.register(require('fastify-swagger'), { |
| 34 | +// INITIALIZE SWAGGER |
| 35 | +fastify.register(require('@fastify/swagger'), { |
30 | 36 | exposeRoute: true, |
31 | 37 | routePrefix: '/', |
32 | | - swagger: config.swagger |
| 38 | + swagger: { |
| 39 | + "info": { |
| 40 | + "title": "Dirst Simple Postgres HTTP API", |
| 41 | + "description": "The Dirt-Simple PostGIS HTTP API is an easy way to expose geospatial functionality to your applications. It takes simple requests over HTTP and returns JSON, JSONP, or protobuf (Mapbox Vector Tile) to the requester. Although the focus of the project has generally been on exposing PostGIS functionality to web apps, you can use the framework to make an API to any database.", |
| 42 | + "version": process.env.npm_package_version || "" |
| 43 | + }, |
| 44 | + "externalDocs": { |
| 45 | + "url": "https://github.com/tobinbradley/dirt-simple-postgis-http-api", |
| 46 | + "description": "Source code on Github" |
| 47 | + }, |
| 48 | + "schemes": [ |
| 49 | + "http", |
| 50 | + "https" |
| 51 | + ], |
| 52 | + "tags": [{ |
| 53 | + "name": "api", |
| 54 | + "description": "code related end-points" |
| 55 | + }, { |
| 56 | + "name": "feature", |
| 57 | + "description": "features in common formats for direct mapping." |
| 58 | + }, { |
| 59 | + "name": "meta", |
| 60 | + "description": "meta information for tables and views." |
| 61 | + }] |
| 62 | + } |
33 | 63 | }) |
34 | 64 |
|
35 | | -// routes |
36 | | -fastify.register(require('fastify-autoload'), { |
| 65 | +// ADD ROUTES |
| 66 | +fastify.register(require('@fastify/autoload'), { |
37 | 67 | dir: path.join(__dirname, 'routes') |
38 | 68 | }) |
39 | 69 |
|
40 | | -// Launch server |
41 | | -fastify.listen(config.port, config.host || 'localhost', function (err, address) { |
| 70 | +// LAUNCH SERVER |
| 71 | +fastify.listen({port: process.env.SERVER_PORT || 3000, host: process.env.SERVER_HOST || '0.0.0.0'}, (err, address) => { |
42 | 72 | if (err) { |
43 | 73 | console.log(err) |
44 | 74 | process.exit(1) |
|
0 commit comments