Skip to content

Commit 7f09650

Browse files
committed
env for all args
1 parent 7d06872 commit 7f09650

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

index.js

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,74 @@
11
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()
44

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
813
})
914

10-
// compression - add x-protobuf
15+
// COMPRESSION
16+
// add x-protobuf
1117
fastify.register(
12-
require('fastify-compress'),
18+
require('@fastify/compress'),
1319
{ customTypes: /x-protobuf$/ }
1420
)
1521

16-
// cache
22+
// CACHE SETTINGS
1723
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
2228
}
2329
)
2430

2531
// CORS
26-
fastify.register(require('fastify-cors'))
32+
fastify.register(require('@fastify/cors'))
2733

28-
// swagger
29-
fastify.register(require('fastify-swagger'), {
34+
// INITIALIZE SWAGGER
35+
fastify.register(require('@fastify/swagger'), {
3036
exposeRoute: true,
3137
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+
}
3363
})
3464

35-
// routes
36-
fastify.register(require('fastify-autoload'), {
65+
// ADD ROUTES
66+
fastify.register(require('@fastify/autoload'), {
3767
dir: path.join(__dirname, 'routes')
3868
})
3969

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) => {
4272
if (err) {
4373
console.log(err)
4474
process.exit(1)

0 commit comments

Comments
 (0)