From c72c76af8174fc7b5de2aff2b97a721df38a370d Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 19 Sep 2023 15:48:11 +0200 Subject: [PATCH] add peristence to docker --- Dockerfile | 8 ++++++-- routes/geocoders/Geocoder.ts | 2 +- routes/weatherProviders/local.ts | 11 ++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index baa6f6b..596b951 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,13 @@ FROM node - +ENV PERSISTENCE_LOCATION=/data EXPOSE 3000 -RUN groupadd osweather && useradd --no-log-init -m -g osweather osweather +RUN groupadd osweather && useradd --no-log-init -m -g osweather osweather && \ + mkdir /data && \ + chown osweather:osweather /data + USER osweather +VOLUME /data ADD --chown=osweather:osweather . weather diff --git a/routes/geocoders/Geocoder.ts b/routes/geocoders/Geocoder.ts index 3498502..0b81a72 100644 --- a/routes/geocoders/Geocoder.ts +++ b/routes/geocoders/Geocoder.ts @@ -5,7 +5,7 @@ import { CodedError, ErrorCode } from "../../errors"; export abstract class Geocoder { - private static cacheFile: string = __dirname + "/../../../geocoderCache.json"; + private static cacheFile: string = (process.env.PERSISTENCE_LOCATION || __dirname + "/../../..") + "/geocoderCache.json"; private cache: Map; diff --git a/routes/weatherProviders/local.ts b/routes/weatherProviders/local.ts index 1d79465..0f5b2b4 100644 --- a/routes/weatherProviders/local.ts +++ b/routes/weatherProviders/local.ts @@ -38,7 +38,8 @@ export const captureWUStream = async function( req: express.Request, res: expres }; export default class LocalWeatherProvider extends WeatherProvider { - + public static observationsFile: string = ( process.env.PERSISTENCE_LOCATION ? process.env.PERSISTENCE_LOCATION + "/observations.json" : "observations.json"); + public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > { queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 ); @@ -126,16 +127,16 @@ export default class LocalWeatherProvider extends WeatherProvider { function saveQueue() { queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 ); try { - fs.writeFileSync( "observations.json" , JSON.stringify( queue ), "utf8" ); + fs.writeFileSync( LocalWeatherProvider.observationsFile , JSON.stringify( queue ), "utf8" ); } catch ( err ) { console.error( "Error saving historical observations to local storage.", err ); } } -if ( process.env.WEATHER_PROVIDER === "local" && process.env.LOCAL_PERSISTENCE ) { - if ( fs.existsSync( "observations.json" ) ) { +if ( process.env.WEATHER_PROVIDER === "local" && (process.env.LOCAL_PERSISTENCE || process.env.PERSISTENCE_LOCATION) ) { + if ( fs.existsSync( LocalWeatherProvider.observationsFile ) ) { try { - queue = JSON.parse( fs.readFileSync( "observations.json", "utf8" ) ); + queue = JSON.parse( fs.readFileSync( LocalWeatherProvider.observationsFile, "utf8" ) ); queue = queue.filter( obs => moment().unix() - obs.timestamp < 24*60*60 ); } catch ( err ) { console.error( "Error reading historical observations from local storage.", err );