Skip to content

Commit

Permalink
Adapt structure to add npm bin for facilmap-server
Browse files Browse the repository at this point in the history
  • Loading branch information
cdauth committed Apr 15, 2021
1 parent 986d62b commit 17a3b98
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 33 deletions.
9 changes: 9 additions & 0 deletions docs/src/developers/server/standalone.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@
"url": "https://github.com/FacilMap/facilmap.git"
},
"files": [
"dist",
"src",
"README.md",
"tsconfig.json"
],
"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",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions server/src/config.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand All @@ -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 : {
Expand Down
2 changes: 1 addition & 1 deletion server/src/database/line.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/src/database/marker.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
5 changes: 2 additions & 3 deletions server/src/database/pad.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
8 changes: 6 additions & 2 deletions server/src/database/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
};
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/database/type.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion server/src/database/view.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion server/src/elevation.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions server/src/export/geojson.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return toStream(async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/geoip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function download() {
const res = await new Promise<IncomingMessage>((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);
});
Expand Down
3 changes: 1 addition & 2 deletions server/src/routing/routing.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
3 changes: 1 addition & 2 deletions server/src/utils/geo.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"esModuleInterop": true,
"strict": true,
"sourceMap": true,
Expand Down
13 changes: 11 additions & 2 deletions types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@
},
"license": "AGPL-3.0",
"author": "Candid Dauth <[email protected]>",
"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"
],
"dependencies": {
"@types/geojson": "^7946.0.7"
},
"devDependencies": {}
"devDependencies": {
"rimraf": "^3.0.2",
"typescript": "^4.2.4"
}
}
6 changes: 3 additions & 3 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"compilerOptions": {
"target": "es3",
"target": "esnext",
"module": "CommonJS",
"esModuleInterop": true,
"strict": true,
"sourceMap": true,
"declaration": true,
"outDir": "dist",
"moduleResolution": "node",
"noErrorTruncation": true,
"skipLibCheck": true,
"noEmit": true
"skipLibCheck": true
}
}
7 changes: 6 additions & 1 deletion utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
},
"license": "AGPL-3.0",
"author": "Candid Dauth <[email protected]>",
"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"
Expand All @@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"esModuleInterop": true,
"strict": true,
"sourceMap": true,
Expand All @@ -9,8 +10,7 @@
"moduleResolution": "node",
"noErrorTruncation": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"noEmit": true
"experimentalDecorators": true
},
"include": ["src/**/*"]
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 17a3b98

Please sign in to comment.