Skip to content

Commit

Permalink
Merge pull request #23 from versx/pregen-fix
Browse files Browse the repository at this point in the history
Fix pregen issue and update example templates
  • Loading branch information
versx authored Jun 18, 2022
2 parents 33012cb + a60ef1b commit 4a2dc5d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/models/staticmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class StaticMap {
}

// If regeneratable staticmap, store for later use
// TODO: Verify `this.regeneratable` is being set
if (this.regeneratable) {
const id = await utils.storeRegenerable<StaticMap>(this);
return id;
Expand Down
16 changes: 11 additions & 5 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export class RouteController {
const templateData = await template.render(req.body);
const tplObj = JSON.parse(templateData);
const staticMap = new StaticMap(tplObj);
//console.debug('Template StaticMap:', staticMap);

let fileName: string;
try {
Expand All @@ -180,7 +179,13 @@ export class RouteController {
return sendErrorResponse(res, e);
}
console.info(`Serving Static: ${fileName}`);
sendResponse(res, fileName, !staticMap.regeneratable, staticMap.regeneratable);
const regen = req.query.regeneratable === 'true';
if (regen) {
const id = await utils.storeRegenerable(staticMap);
sendResponse(res, id, !regen, regen);
} else {
sendResponse(res, fileName, !regen, regen);
}
}

/**
Expand Down Expand Up @@ -312,15 +317,16 @@ export class RouteController {
}
}

const sendResponse = (res: Response, path: string, setCacheControl = true, regeneratable = false): void => {
const sendResponse = (res: Response, filePath: string, setCacheControl = true, regeneratable = false): void => {
if (setCacheControl && !regeneratable) {
res.setHeader('Cache-Control', 'max-age=604800, must-revalidate');
}
if (regeneratable) {
res.setHeader('content-type', 'text/html');
res.send(path);
const fileName = path.basename(filePath);
res.send(fileName);
} else {
res.sendFile(path, (err: Error) => {
res.sendFile(filePath, (err: Error) => {
if (err) {
console.error('Failed to send static file:', err);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const touch = async (fileName: string): Promise<void> => {
};

export const storeRegenerable = async <T>(staticmap: T): Promise<string> => {
const id = getHashCode(staticmap);
const id = getHashCode(staticmap) + '.png';
const fileName = path.resolve(globals.RegeneratableCacheDir, id) + '.json';
const fileData = JSON.stringify(staticmap);
fs.writeFileSync(fileName, fileData);
Expand Down
20 changes: 14 additions & 6 deletions templates/staticmap-complex.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"height": 250,
"scale": 1,
"markers": [
<% if (stops != null) {
stops.forEach(function(stop) { %>
<% if (pokestops != null) {
pokestops.forEach(function(pokestop) { %>
{
"url": "<%= stop.marker %>",
"latitude": <%= stop.lat %>,
"longitude": <%= stop.lon %>,
"url": "<%= pokestop.marker %>",
"latitude": <%= pokestop.lat %>,
"longitude": <%= pokestop.lon %>,
"width": 50,
"height": 50
},
Expand All @@ -36,5 +36,13 @@
"width": 50,
"height": 50
}
]
],
"circles": [{
"latitude": <%= lat %>,
"longitude": <%= lon %>,
"radius": 25,
"fill_color": "rgba(100.0%, 100.0%, 100.0%, 0.5)",
"stroke_color": "rgb(45, 45, 45)",
"stroke_width": 1
}]
}
10 changes: 5 additions & 5 deletions templates/staticmap-complex2.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"height": 250,
"scale": 1,
"markers": [
<% if (stops != null) {
stops.forEach(function(stop) { %>
<% if (pokestops != null) {
pokestops.forEach(function(pokestop) { %>
{
"url": "https://raw.githubusercontent.com/whitewillem/PMSF/develop/static/forts/Pstop.png",
"latitude": <%= stop.lat %>,
"longitude": <%= stop.lon %>,
"latitude": <%= pokestop.lat %>,
"longitude": <%= pokestop.lon %>,
"width": 20,
"height": 20,
"y_offset": -8
Expand All @@ -22,7 +22,7 @@
<% if (gyms != null) {
gyms.forEach(function(gym) { %>
{
"url": "https://raw.githubusercontent.com/whitewillem/PMSF/develop/static/forts/shield/<%= gym.team %>",
"url": "https://raw.githubusercontent.com/whitewillem/PMSF/develop/static/forts/shield/<%= gym.team %>.png",
"latitude": <%= gym.lat %>,
"longitude": <%= gym.lon %>,
"width": 20,
Expand Down

0 comments on commit 4a2dc5d

Please sign in to comment.