Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bin/cmd/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ module.exports = {
// optional params
yargs.option('db', {
type: 'string',
default: 'geo.db',
describe: 'location of spatial database file'
})
},
handler: (argv) => {
const script = path.resolve(__dirname, '../../server/http.js')
spawn('node', [script, argv.db], { stdio: 'inherit' })
spawn('node', [script, ...argv.db ? [argv.db] : []], { stdio: 'inherit' })
}
}
13 changes: 13 additions & 0 deletions config/ServiceConfiguration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const _ = require('lodash')
const path = require('path')
const peliasConfig = require('pelias-config')
const PragmaStatement = require('./PragmaStatement')

class ServiceConfiguration {
Expand All @@ -22,6 +24,17 @@ class ServiceConfiguration {
new PragmaStatement('cache_size', ['2000']),
new PragmaStatement('recursive_triggers', ['ON', 'OFF'])
])

// optionally read database filename from pelias/config (when available)
// note: you must set `config.pelias=true` to enable this functionality as
// it is undesirable at index generation time (for instance).
// note: existing `config.filename` value takes precedence over pelias config
if (!_.has(config, 'filename') && _.get(config, 'pelias') === true) {
const config = peliasConfig.generate().get('services.spatial')
if (config && !_.isEmpty(config.datapath) && !_.isEmpty(config.files)) {
this.filename = path.resolve(config.datapath, _.first(_.castArray(config.files)))
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"locale": "^0.1.0",
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"pelias-config": "^6.0.0",
"pelias-logger": "^1.4.1",
"split2": "^3.1.1",
"through2": "^3.0.1",
Expand Down
7 changes: 4 additions & 3 deletions server/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const handlebars = require('express-handlebars')
const helpers = require('handlebars-helpers')()
const QueryService = require('../service/QueryService')
const logger = require('pelias-logger').get('geometry')
const dbFilename = process.argv[2] || 'geo.db'
const filename = process.argv[2]

// select the amount of cpus we will use
const envCpus = parseInt(process.env.CPUS, 10)
Expand Down Expand Up @@ -68,10 +68,11 @@ app.engine('.hbs', handlebars({
app.set('view engine', '.hbs')

// init service
logger.info(`load: ${dbFilename}`)
logger.info(`load: ${filename}`)
const service = new QueryService({
readonly: true,
filename: dbFilename
pelias: true,
filename
})

// store $service on app
Expand Down
8 changes: 8 additions & 0 deletions service/QueryService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const Database = require('better-sqlite3')
const ServiceConfiguration = require('../config/ServiceConfiguration')
const SpatialiteModule = require('../module/spatialite/SpatialiteModule')
Expand All @@ -19,6 +20,13 @@ class QueryService {
let dbconf = { fileMustExist: true }
if (this.config.readonly === true) { dbconf.readonly = true }
if (this.config.verbose === true) { dbconf.verbose = console.error }

// ensure database file exists
if (!fs.existsSync(this.config.filename)) {
console.error(`database file not found: ${this.config.filename}`)
process.exit(1)
}

this.db = new Database(this.config.filename, dbconf)

// set up modules
Expand Down