From 17a3b981ab5380e7d6e044ab6c69ec6c96162582 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Thu, 15 Apr 2021 23:30:25 +0200 Subject: [PATCH] Adapt structure to add npm bin for facilmap-server --- docs/src/developers/server/standalone.md | 9 +++++++++ server/package.json | 10 +++++++--- server/src/config.ts | 6 ++---- server/src/database/line.ts | 2 +- server/src/database/marker.ts | 2 +- server/src/database/pad.ts | 5 ++--- server/src/database/route.ts | 8 ++++++-- server/src/database/type.ts | 2 +- server/src/database/view.ts | 2 +- server/src/elevation.ts | 2 +- server/src/export/geojson.ts | 3 +-- server/src/geoip.ts | 2 +- server/src/routing/routing.ts | 3 +-- server/src/utils/geo.ts | 3 +-- server/tsconfig.json | 1 + types/package.json | 13 +++++++++++-- types/tsconfig.json | 6 +++--- utils/package.json | 7 ++++++- utils/tsconfig.json | 4 ++-- yarn.lock | 2 +- 20 files changed, 59 insertions(+), 33 deletions(-) diff --git a/docs/src/developers/server/standalone.md b/docs/src/developers/server/standalone.md index 64d8ed10..afc2ae0c 100644 --- a/docs/src/developers/server/standalone.md +++ b/docs/src/developers/server/standalone.md @@ -5,6 +5,15 @@ The FacilMap server is written in [node.js](https://nodejs.org/en/). To run the * You need to create a database on one of the systems supported by [Sequelize](https://sequelize.org/master/), it is recommended to use MySQL/MariaDB. When creating a database for FacilMap, make sure to use the `utf8mb4` charset/collation to make sure that characters from all languages can be used on a map. By default, MySQL/MariaDB uses the `latin1` charset, which mostly supports only basic latin characters. When you start the FacilMap server for the first time, the necessary tables are created using the charset of the database. * It is recommended to run FacilMap as an unprivileged user. +## Run the latest release + +A bundled version of the FacilMap server is published on NPM as [facilmap-server](https://www.npmjs.com/package/facilmap-server). To run it, run the following steps: + +1. If you don’t have a global NPM prefix set up yet, run `npm config set prefix ~/.local`. This will install npm packages into `~/.local/bin`, rather than trying to install them into `/usr/local/bin`. +2. Install facilmap-server by running `npm install -g facilmap-server` +3. Create a `config.env` file based on [`config.env.example`](https://github.com/FacilMap/facilmap/blob/master/config.env.example) and to adjust the [configuration](./config). +4. Start the FacilMap server by running `~/.local/bin/facilmap-server dotenv_config_path=config.env`. + ## Run the development version diff --git a/server/package.json b/server/package.json index d6ca75b2..3b20a56e 100644 --- a/server/package.json +++ b/server/package.json @@ -18,6 +18,7 @@ "url": "https://github.com/FacilMap/facilmap.git" }, "files": [ + "dist", "src", "README.md", "tsconfig.json" @@ -25,14 +26,16 @@ "scripts": { "start": "npm run deps && npm run server", "deps": "npm install", - "server": "ts-node --transpile-only src/server.ts", - "dev-server": "FM_DEV=true ts-node --transpile-only src/server.ts", + "build": "tsc; chmod +x dist/server.js", + "clean": "rimraf dist", + "server": "dist/server.js dotenv_config_path=../config.env", + "dev-server": "FM_DEV=true ts-node --transpile-only src/server.ts dotenv_config_path=../config.env", "test": "jest", "types": "tsc --noEmit src/**/*.ts", "lint": "eslint src/**/*.ts" }, "bin": { - "facilmap-server": "ts-node --transpile-only src/server.ts" + "facilmap-server": "./dist/server.js" }, "dependencies": { "@mapbox/polyline": "^1.1.1", @@ -94,6 +97,7 @@ "@types/string-similarity": "^4.0.0", "debug": "^4.3.1", "jest": "^26.6.3", + "rimraf": "^3.0.2", "ts-jest": "^26.4.4", "ts-node": "^9.1.1", "typescript": "^4.1.3", diff --git a/server/src/config.ts b/server/src/config.ts index 0fc48163..b5ad9035 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -1,7 +1,5 @@ import { Options as SequelizeOptions } from "sequelize"; -import { config as dotenv } from "dotenv"; - -dotenv({ path: `${__dirname}/../../config.env`}); +import "dotenv/config"; export interface DbConfig { type: SequelizeOptions['dialect']; @@ -25,7 +23,7 @@ interface Config { } const config: Config = { - userAgent: process.env.USER_AGENT || 'FacilMap (https://facilmap.org/)', + userAgent: process.env.USER_AGENT || 'FacilMap', host: process.env.HOST || undefined, port: process.env.PORT ? Number(process.env.PORT) : 8080, db : { diff --git a/server/src/database/line.ts b/server/src/database/line.ts index 8811d3b6..6083166c 100644 --- a/server/src/database/line.ts +++ b/server/src/database/line.ts @@ -1,5 +1,5 @@ import { DataTypes, HasManyGetAssociationsMixin, Model, Op } from "sequelize"; -import { BboxWithZoom, ID, Latitude, Line, LineCreate, ExtraInfo, LineUpdate, Longitude, PadId, Point, Route, TrackPoint } from "../../../types/src"; +import { BboxWithZoom, ID, Latitude, Line, LineCreate, ExtraInfo, LineUpdate, Longitude, PadId, Point, Route, TrackPoint } from "facilmap-types"; import Database from "./database"; import { BboxWithExcept, dataDefinition, DataModel, getLatType, getLonType, makeBboxCondition, makeNotNullForeignKey, validateColour } from "./helpers"; import { isEqual, mapValues } from "lodash"; diff --git a/server/src/database/marker.ts b/server/src/database/marker.ts index 118a3b7e..43b0a0f0 100644 --- a/server/src/database/marker.ts +++ b/server/src/database/marker.ts @@ -1,5 +1,5 @@ import { DataTypes, Model } from "sequelize"; -import { BboxWithZoom, ID, Latitude, Longitude, Marker, MarkerCreate, MarkerUpdate, PadId } from "../../../types/src"; +import { BboxWithZoom, ID, Latitude, Longitude, Marker, MarkerCreate, MarkerUpdate, PadId } from "facilmap-types"; import { BboxWithExcept, dataDefinition, DataModel, getLatType, getLonType, makeBboxCondition, makeNotNullForeignKey, validateColour } from "./helpers"; import Database from "./database"; import { getElevationForPoint } from "../elevation"; diff --git a/server/src/database/pad.ts b/server/src/database/pad.ts index 57f9c3dc..058a3d13 100644 --- a/server/src/database/pad.ts +++ b/server/src/database/pad.ts @@ -1,6 +1,5 @@ -import { Model } from "sequelize"; -import { DataTypes, Op } from "sequelize"; -import { PadData, PadDataCreate, PadDataUpdate, PadId } from "../../../types/src"; +import { DataTypes, Model, Op } from "sequelize"; +import { PadData, PadDataCreate, PadDataUpdate, PadId } from "facilmap-types"; import Database from "./database"; import { streamEachPromise } from "../utils/streams"; diff --git a/server/src/database/route.ts b/server/src/database/route.ts index 5b836c52..e80549a6 100644 --- a/server/src/database/route.ts +++ b/server/src/database/route.ts @@ -1,7 +1,7 @@ import { generateRandomId } from "../utils/utils"; import { DataTypes, Model, Op } from "sequelize"; import Database from "./database"; -import { BboxWithZoom, ID, Latitude, Longitude, PadId, Point, Route, RouteMode, TrackPoint } from "../../../types/src"; +import { BboxWithZoom, ID, Latitude, Longitude, PadId, Point, Route, RouteMode, TrackPoint } from "facilmap-types"; import { BboxWithExcept, getLatType, getLonType, makeBboxCondition } from "./helpers"; import { WhereOptions } from "sequelize/types/lib/model"; import { calculateRouteForLine } from "../routing/routing"; @@ -172,7 +172,11 @@ export default class DatabaseRoutes { time: line.time, ascent: line.ascent, descent: line.descent, - extraInfo: line.extraInfo + extraInfo: line.extraInfo, + top: line.top, + left: line.left, + bottom: line.bottom, + right: line.right }; } diff --git a/server/src/database/type.ts b/server/src/database/type.ts index a9cd35e9..54536c69 100644 --- a/server/src/database/type.ts +++ b/server/src/database/type.ts @@ -1,5 +1,5 @@ import Sequelize, { Model } from "sequelize"; -import { Field, ID, PadId, Type, TypeCreate, TypeUpdate } from "../../../types/src"; +import { Field, ID, PadId, Type, TypeCreate, TypeUpdate } from "facilmap-types"; import Database from "./database"; import { makeNotNullForeignKey, validateColour } from "./helpers"; diff --git a/server/src/database/view.ts b/server/src/database/view.ts index e2849689..109abf56 100644 --- a/server/src/database/view.ts +++ b/server/src/database/view.ts @@ -1,5 +1,5 @@ import { DataTypes, Model } from "sequelize"; -import { ID, Latitude, Longitude, PadId, View, ViewCreate, ViewUpdate } from "../../../types/src"; +import { ID, Latitude, Longitude, PadId, View, ViewCreate, ViewUpdate } from "facilmap-types"; import Database from "./database"; import { getLatType, getLonType, makeNotNullForeignKey } from "./helpers"; diff --git a/server/src/elevation.ts b/server/src/elevation.ts index 47ed7a28..46bc06be 100644 --- a/server/src/elevation.ts +++ b/server/src/elevation.ts @@ -1,5 +1,5 @@ import highland from "highland"; -import { Point } from "../../types/src"; +import { Point } from "facilmap-types"; // const API_URL = "https://elevation.mapzen.com/height"; // const LIMIT = 500; diff --git a/server/src/export/geojson.ts b/server/src/export/geojson.ts index 7113f654..33742bac 100644 --- a/server/src/export/geojson.ts +++ b/server/src/export/geojson.ts @@ -1,11 +1,10 @@ import { jsonStream, streamToArrayPromise, toStream } from "../utils/streams"; import { clone } from "../utils/utils"; import { compileExpression } from "facilmap-utils"; -import { Marker, PadId } from "../../../types/src"; +import { Marker, MarkerFeature, LineFeature, PadId } from "facilmap-types"; import Database from "../database/database"; import { keyBy, mapValues, omit } from "lodash"; import { LineWithTrackPoints } from "../database/line"; -import { MarkerFeature, LineFeature } from "facilmap-types"; export function exportGeoJson(database: Database, padId: PadId, filter?: string): Highland.Stream { return toStream(async () => { diff --git a/server/src/geoip.ts b/server/src/geoip.ts index 3dd1de4f..4cd872b6 100644 --- a/server/src/geoip.ts +++ b/server/src/geoip.ts @@ -47,7 +47,7 @@ async function download() { const res = await new Promise((resolve, reject) => { https.get(url + (currentMd5 || ""), { headers: { - Authorization: `Basic ${new Buffer(config.maxmindUserId + ':' + config.maxmindLicenseKey).toString('base64')}` + Authorization: `Basic ${Buffer.from(config.maxmindUserId + ':' + config.maxmindLicenseKey).toString('base64')}` } }, resolve).on("error", reject); }); diff --git a/server/src/routing/routing.ts b/server/src/routing/routing.ts index bf24dacc..49d2dee2 100644 --- a/server/src/routing/routing.ts +++ b/server/src/routing/routing.ts @@ -1,7 +1,6 @@ import { calculateBbox, isInBbox } from "../utils/geo"; -import { Bbox, BboxWithZoom, Point, RouteMode, TrackPoint } from "../../../types/src"; +import { Bbox, BboxWithZoom, LineCreate, Point, Route, RouteInfo, RouteMode, TrackPoint } from "facilmap-types"; import { decodeRouteMode, DecodedRouteMode, calculateDistance } from "facilmap-utils"; -import { LineCreate, Route, RouteInfo } from "facilmap-types"; import { calculateOSRMRoute } from "./osrm"; import { calculateORSRoute, getMaximumDistanceBetweenRoutePoints } from "./ors"; diff --git a/server/src/utils/geo.ts b/server/src/utils/geo.ts index 4e2bff60..0d87b5e1 100644 --- a/server/src/utils/geo.ts +++ b/server/src/utils/geo.ts @@ -1,5 +1,4 @@ -import { Bbox, Point } from "facilmap-types"; -import { Latitude } from "../../../types/src"; +import { Bbox, Latitude, Point } from "facilmap-types"; const R = 6371; // km diff --git a/server/tsconfig.json b/server/tsconfig.json index e822ff2e..ab954c62 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "target": "esnext", + "module": "CommonJS", "esModuleInterop": true, "strict": true, "sourceMap": true, diff --git a/types/package.json b/types/package.json index dfd5b4ac..e26a3e6b 100644 --- a/types/package.json +++ b/types/package.json @@ -8,12 +8,18 @@ }, "license": "AGPL-3.0", "author": "Candid Dauth ", - "main": "./src/index.ts", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "repository": { "type": "git", "url": "https://github.com/FacilMap/facilmap.git" }, + "scripts": { + "build": "tsc", + "clean": "rimraf dist" + }, "files": [ + "dist", "src", "README.md", "tsconfig.json" @@ -21,5 +27,8 @@ "dependencies": { "@types/geojson": "^7946.0.7" }, - "devDependencies": {} + "devDependencies": { + "rimraf": "^3.0.2", + "typescript": "^4.2.4" + } } diff --git a/types/tsconfig.json b/types/tsconfig.json index 1b2345e0..e4c2c7ec 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { - "target": "es3", + "target": "esnext", + "module": "CommonJS", "esModuleInterop": true, "strict": true, "sourceMap": true, @@ -8,7 +9,6 @@ "outDir": "dist", "moduleResolution": "node", "noErrorTruncation": true, - "skipLibCheck": true, - "noEmit": true + "skipLibCheck": true } } \ No newline at end of file diff --git a/utils/package.json b/utils/package.json index d7e35ab1..89d121ec 100644 --- a/utils/package.json +++ b/utils/package.json @@ -12,16 +12,20 @@ }, "license": "AGPL-3.0", "author": "Candid Dauth ", - "main": "./src/index.ts", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "repository": { "type": "git", "url": "https://github.com/FacilMap/facilmap.git" }, "scripts": { + "build": "tsc", + "clean": "rimraf dist", "types": "tsc --noEmit src/*", "lint": "eslint src/*" }, "files": [ + "dist", "src", "README.md", "tsconfig.json" @@ -47,6 +51,7 @@ "@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/parser": "^4.18.0", "jest": "^26.6.3", + "rimraf": "^3.0.2", "ts-jest": "^26.5.4", "typescript": "^4.2.3" } diff --git a/utils/tsconfig.json b/utils/tsconfig.json index f63a90b7..bd76d6a7 100644 --- a/utils/tsconfig.json +++ b/utils/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "target": "esnext", + "module": "CommonJS", "esModuleInterop": true, "strict": true, "sourceMap": true, @@ -9,8 +10,7 @@ "moduleResolution": "node", "noErrorTruncation": true, "skipLibCheck": true, - "experimentalDecorators": true, - "noEmit": true + "experimentalDecorators": true }, "include": ["src/**/*"] } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 08306ff8..eb650677 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8415,7 +8415,7 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= -typescript@^4.1.3, typescript@^4.2.3: +typescript@^4.1.3, typescript@^4.2.3, typescript@^4.2.4: version "4.2.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.4.tgz#8610b59747de028fda898a8aef0e103f156d0961" integrity sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==