Skip to content

Commit

Permalink
Merge pull request #396 from dfpc-coe/data
Browse files Browse the repository at this point in the history
AutoLoad Basemaps
  • Loading branch information
ingalls authored Oct 26, 2024
2 parents 5429039 + 0755107 commit 7ab5278
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 33 deletions.
14 changes: 7 additions & 7 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ EXPOSE 5000
ENV HOME=/home/etl
WORKDIR $HOME

RUN apk add nodejs-current npm memcached python3 make bash g++ openssl postgresql-client grep
RUN apk add git nodejs-current npm memcached python3 make bash g++ openssl postgresql-client grep

WORKDIR $HOME/api

COPY package.json ./
COPY package-lock.json ./
ADD package.json ./
ADD package-lock.json ./

RUN git clone --branch v1.1.0 --depth 1 https://github.com/dfpc-coe/CloudTAK-Data.git data

RUN npm install

Expand All @@ -24,9 +26,7 @@ RUN cd web \
&& cd ..

RUN npm run lint \
&& npm run build

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./start $HOME/api
&& npm run build \
&& mv data dist/

CMD ["./start"]
4 changes: 4 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ try {

const pkg = JSON.parse(String(fs.readFileSync(new URL('./package.json', import.meta.url))));

process.on('uncaughtException', (exception) => {
console.error('Error', exception);
});

if (import.meta.url === `file://${process.argv[1]}`) {
const config = await Config.env({
silent: args.silent || false,
Expand Down
14 changes: 10 additions & 4 deletions api/nginx.conf → api/nginx.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

let CSP = `add_header 'Content-Security-Policy' "default-src 'self' *.${process.env.API_URL}; $\{IMG}; $\{WORKER}; $\{CONNECT}; $\{STYLE_SRC_ATTR}; $\{STYLE_SRC_ELEM}; $\{FONT}; upgrade-insecure-requests;" always;`
if (process.env.API_URL.includes('localhost')) CSP = '';

console.log(`
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
Expand Down Expand Up @@ -31,21 +36,21 @@ http {
add_header 'Strict-Transport-Security' 'max-age=31536000; includeSubDomains; preload' always;
add_header 'Permissions-Policy' 'fullscreen=(self), geolocation=(self), clipboard-read=(self), clipboard-write=(self)' always;
set $IMG "img-src 'self' data: *.API_URL";
set $IMG "img-src 'self' data: *.${process.env.API_URL}";
set $FONT "font-src 'self' data:";
set $WORKER "worker-src 'self' blob:";
set $STYLE_SRC_ELEM "style-src-elem 'self' 'unsafe-inline'";
set $STYLE_SRC_ATTR "style-src-attr 'unsafe-inline'";
set $CONNECT "connect-src 'self' *.API_URL:* *.ROOT_URL:*";
add_header 'Content-Security-Policy' "default-src 'self' *.API_URL; ${IMG}; ${WORKER}; ${CONNECT}; ${STYLE_SRC_ATTR}; ${STYLE_SRC_ELEM}; ${FONT}; upgrade-insecure-requests;" always;
set $CONNECT "connect-src 'self' *.${process.env.API_URL}:* *.ROOT_URL:*";
${CSP}
location = / {
add_header 'X-Content-Type-Options' 'nosniff' always;
add_header 'X-Frame-Options' 'DENY' always;
add_header 'Referrer-Policy' 'strict-origin-when-cross-origin' always;
add_header 'Strict-Transport-Security' 'max-age=31536000; includeSubDomains; preload' always;
add_header 'Permissions-Policy' 'fullscreen=(self), geolocation=(self), clipboard-read=(self), clipboard-write=(self)' always;
add_header 'Content-Security-Policy' "default-src 'self' *.API_URL; ${IMG}; ${WORKER}; ${CONNECT}; ${STYLE_SRC_ATTR}; ${STYLE_SRC_ELEM}; ${FONT}; upgrade-insecure-requests;" always;
${CSP}
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate' always;
add_header 'Expires' 0 always;
Expand Down Expand Up @@ -93,3 +98,4 @@ http {
}
}
`)
41 changes: 22 additions & 19 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions api/routes/basemap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path';
import { bbox } from '@turf/bbox';
import fs from 'node:fs/promises'
import Err from '@openaddresses/batch-error';
import TileJSON, { TileJSONType } from '../lib/control/tilejson.js';
import Auth, { AuthUserAccess, ResourceCreationScope } from '../lib/auth.js';
Expand Down Expand Up @@ -30,6 +31,27 @@ const AugmentedBasemapResponse = Type.Composite([
])

export default async function router(schema: Schema, config: Config) {
const count = await config.models.Basemap.count();
if (count === 0) {
try {
await fs.access(new URL('../data/', import.meta.url));

for (const file of await fs.readdir(new URL('../data/basemaps/', import.meta.url))) {
console.error(`ok - loading basemap ${file}`);
const b = (await BasemapParser.parse(String(await fs.readFile(new URL(`../data/basemaps/${file}`, import.meta.url))))).to_json();

await config.models.Basemap.generate({
name: b.name || 'Unknown',
url: b.url,
minzoom: b.minZoom || 0,
maxzoom: b.maxZoom || 16
})
}
} catch (err) {
console.log('Could not automatically load basemaps', err);
}
}

await schema.put('/basemap', {
name: 'Import Basemaps',
group: 'BaseMap',
Expand Down
2 changes: 2 additions & 0 deletions api/routes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async function router(schema: Schema, config: Config) {
const user = await Auth.as_user(config, req);
const profile = await config.models.Profile.from(user.email);

// @ts-expect-error Update Batch-Generic to specify actual geometry type (Point) instead of Geometry
res.json(profile);
} catch (err) {
Err.respond(err, res);
Expand Down Expand Up @@ -52,6 +53,7 @@ export default async function router(schema: Schema, config: Config) {
updated: sql`Now()`
});

// @ts-expect-error Update Batch-Generic to specify actual geometry type (Point) instead of Geometry
res.json(profile);
} catch (err) {
Err.respond(err, res);
Expand Down
3 changes: 3 additions & 0 deletions api/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default async function router(schema: Schema, config: Config) {
`
});

// @ts-expect-error Update Batch-Generic to specify actual geometry type (Point) instead of Geometry
res.json(list);
} catch (err) {
Err.respond(err, res);
Expand All @@ -61,6 +62,7 @@ export default async function router(schema: Schema, config: Config) {

const user = await config.models.Profile.commit(req.params.username, req.body);

// @ts-expect-error Update Batch-Generic to specify actual geometry type (Point) instead of Geometry
res.json({
...user,
agency_admin: user.agency_admin || []
Expand All @@ -84,6 +86,7 @@ export default async function router(schema: Schema, config: Config) {

const user = await config.models.Profile.from(req.params.username);

// @ts-expect-error Update Batch-Generic to specify actual geometry type (Point) instead of Geometry
res.json({
...user,
agency_admin: user.agency_admin || []
Expand Down
3 changes: 1 addition & 2 deletions api/start
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ else
echo "ROOT_URL: ${ROOT_URL}"
fi

sed -i "s/API_URL/${API_URL}/g" /etc/nginx/nginx.conf
sed -i "s/ROOT_URL/${ROOT_URL}/g" /etc/nginx/nginx.conf
node nginx.conf.js > /etc/nginx/nginx.conf

memcached -d -u root

Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ services:
- store
ports:
- "5000:5000"
- "5001:5001"
environment:
- SigningSecret=coe-wildland-fire
- POSTGRES=postgres://docker:docker@postgis:5432/gis
- API_URL=localhost:5001
- API_URL=localhost:5000
- ROOT_URL=localhost:5001

postgis:
platform: linux/amd64
Expand Down

0 comments on commit 7ab5278

Please sign in to comment.