-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dd1174
commit 35ba3be
Showing
3 changed files
with
142 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const http = require('http'); | ||
Check failure on line 1 in node_mbtiles/poc/main.js GitHub Actions / test
|
||
const sqlite3 = require("sqlite3"); | ||
const url = require("url"); | ||
|
||
const morgan = require('morgan'); | ||
Check failure on line 5 in node_mbtiles/poc/main.js GitHub Actions / test
Check failure on line 5 in node_mbtiles/poc/main.js GitHub Actions / test
Check failure on line 5 in node_mbtiles/poc/main.js GitHub Actions / test
|
||
const logger = morgan('combined'); | ||
Check failure on line 6 in node_mbtiles/poc/main.js GitHub Actions / test
|
||
|
||
const sourceConfigs = require('source-configs'); | ||
const schema = require('./configs-schema'); | ||
const configs = sourceConfigs(schema); | ||
|
||
try { | ||
configs.databases = parseDatabases(configs.databases); | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(); | ||
} | ||
|
||
const databases = configs.databases; | ||
|
||
const mode = configs.mode; | ||
const port = configs.port; | ||
|
||
let databaseAlias = Object.keys(databases); | ||
databaseAlias.forEach(element => { | ||
databases[element].db = new sqlite3.Database(databases[element].filePath, sqlite3.OPEN_READONLY, (err) => { | ||
if (err) { | ||
console.log("Failed to load: " + databases[element].filePath); | ||
throw err; | ||
} | ||
}); | ||
}); | ||
|
||
const requestListener = function (req, res) { | ||
logger(req, res, function(err) { | ||
if (err) { | ||
return done(err) | ||
} | ||
let dbAlias; | ||
let z; | ||
let x; | ||
let y; | ||
//Argument mode - Slower, but compatible with non pretty PHP urls | ||
if (mode == "argument") { | ||
const queryObject = url.parse(req.url, true).query; | ||
z = (queryObject.z ? parseInt(queryObject.z) : undefined); | ||
x = (queryObject.x ? parseInt(queryObject.x) : undefined); | ||
y = (queryObject.y ? parseInt(queryObject.y) : undefined); | ||
dbAlias = (queryObject.db ? queryObject.db.split(".")[0] : undefined); | ||
} | ||
//End argument mode | ||
|
||
//Pretty mode - Faster and better to use if it is a new initiation. | ||
else if (mode == "pretty") { | ||
args = parseUrl(req.url); | ||
dbAlias = args[1]; | ||
z = (args[2] ? parseInt(args[2]) : undefined); | ||
x = (args[3] ? parseInt(args[3]) : undefined); | ||
y = (args[4] ? parseInt(args[4]) : undefined); | ||
} | ||
//End prettymode | ||
|
||
if (databaseAlias.includes(dbAlias) && typeof z == "number" && typeof x == "number" && typeof y == "number") { | ||
|
||
databases[dbAlias].db.get(`SELECT * FROM tiles WHERE zoom_level = ${z} AND tile_column = ${x} AND tile_row = ${y}`, (err, row) => { | ||
if (err || row == undefined) { | ||
res.writeHead(404, { | ||
"Access-Control-Allow-Origin": "*" | ||
}); | ||
res.end(); | ||
} else { | ||
res.writeHead(200, { | ||
"Content-Type": "application/octet-stream", | ||
"Content-Encoding": "gzip", | ||
"Access-Control-Allow-Origin": "*" | ||
}); | ||
res.end(row.tile_data); | ||
} | ||
}); | ||
} else { | ||
res.writeHead(400); | ||
res.end(); | ||
} | ||
}) | ||
} | ||
|
||
const server = http.createServer(requestListener); | ||
server.listen(port); | ||
server.on("error", err => console.log(err)); | ||
console.log("Listening for connections on port: " + port); | ||
|
||
function parseUrl(url) { | ||
return url.split("/"); | ||
} | ||
|
||
function parseDatabases(databasesConfig) { | ||
if (!databasesConfig) { | ||
throw new Error("No databases defined"); | ||
} | ||
let databases = {}; | ||
|
||
databasesSplit = databasesConfig.split(','); | ||
|
||
console.log("databases: " + JSON.stringify(databasesSplit)) | ||
|
||
databasesSplit.forEach(database => { | ||
const databaseSplit = database.split(';'); | ||
console.log("parsing " + database); | ||
if (databaseSplit.length != 2) { | ||
throw new Error("Unable to parse Database string. Possible use of wrong delimiter"); | ||
} | ||
databases[databaseSplit[0]] = { | ||
filePath: databaseSplit[1] | ||
} | ||
}) | ||
return databases; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters