Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Support trap URL suffixes & return 404 for traps
Browse files Browse the repository at this point in the history
Now any trap URL path can be suffixed with a string that begins with
either "." or "/". Also query strings are supported. These suffixes
(path suffix + query string) are stored for potential later use.

Implements changes suggested in #28.
  • Loading branch information
jviide committed Nov 9, 2016
1 parent d0a2f07 commit e842d4b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 63 deletions.
2 changes: 1 addition & 1 deletion browser/visits.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function updateTable(_props, rootElement, minInterval=1000, maxInterval=15000) {
render(<Visits {...props} />, rootElement);

const baseUrl = props.updateUrl;
const cursor = props.updateCursor;
const cursor = props.cursor;
fetchUpdates(baseUrl, cursor, minInterval, minInterval, maxInterval, null, (err, visits) => {
if (err) {
props = Object.assign({}, props, {
Expand Down
114 changes: 52 additions & 62 deletions server/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,94 +99,84 @@ app.get("/new", (req, res, next) => {
.catch(next);
});

app.get("/:id([a-zA-Z0-9_-]{22})", (req, res, next) => {
const id = req.params.id;
store.get(id)
.then(item => {
if (!item) {
next();
return;
}
function getData(req, target, cursor) {
return store.list(target, cursor).then(({ cursor, visits }) => {
return {
trapUrl: fullUrl(req, target),
cursor: cursor,
visits: visits.map(entity => {
return mergeAndClean(entity.info, {
timestamp: entity.timestamp
});
})
};
});
}

analytics.pageView(req).catch(errors.report);
app.get(/^\/([a-zA-Z0-9_-]{22})([./].*)?$/, (req, res, next) => {
const id = req.params[0];
const rest = req.params[1];

if (!item.isView) {
store.get(id)
.then(item => {
if (item && !item.isView) {
analytics.event(req, "trap", "view").catch(errors.report);

const suffix = req.url.substring(1 + id.length) || undefined;
return taskQueue.publish("trap-topic", {
target: id,
timestamp: Date.now(),
info: extractInfo(req)
info: mergeAndClean(extractInfo(req), {
suffix: suffix || undefined
})
}).then(() => {
const styles = [asset("common", "css")];
const scripts = [asset("common", "js")];
res.send(render(
res.status(404).send(render(
<Layout title="URI:teller trap" className="page-trap" styles={styles} scripts={scripts}>
<Trap />
</Layout>
));
});
}

return store.list(item.other).then(({ cursor, visits }) => {
const initialData = {
js: false,
trapUrl: fullUrl(req, item.other),
updateUrl: fullUrl(req, id + ".json"),
updateCursor: cursor,
visits: visits.map(entity => {
return mergeAndClean(entity.info, {
timestamp: entity.timestamp
});
})
};

const styles = [asset("common", "css"), asset("visits", "css")];
const scripts = [asset("common", "js"), asset("visits", "js")];
res.send(render(
<Layout title="URI:teller monitor" className="page-monitor" styles={styles} scripts={scripts}>
<EmbeddedJSON id="initial-data" content={initialData} />
<Visits {...initialData} />
</Layout>
));
});
})
.catch(next);
});
if (item && item.isView && !rest) {
analytics.pageView(req).catch(errors.report);

app.get("/:id([a-zA-Z0-9_-]{22}).json", (req, res, next) => {
const id = req.params.id;
return getData(req, item.other).then(data => {
const initialData = mergeAndClean({
js: false,
updateUrl: fullUrl(req, id + ".json")
}, data);

let cursor = req.query.cursor;
if (Array.isArray(cursor)) {
return res.sendStatus(400);
}
if (cursor !== undefined) {
cursor = Number(cursor);
}
const styles = [asset("common", "css"), asset("visits", "css")];
const scripts = [asset("common", "js"), asset("visits", "js")];
res.send(render(
<Layout title="URI:teller monitor" className="page-monitor" styles={styles} scripts={scripts}>
<EmbeddedJSON id="initial-data" content={initialData} />
<Visits {...initialData} />
</Layout>
));
});
}

store.get(id)
.then(item => {
if (!item || !item.isView) {
next();
return;
if (item && item.isView && rest === ".json") {
let cursor = req.query.cursor;
if (Array.isArray(cursor)) {
return res.sendStatus(400);
}
if (cursor !== undefined) {
cursor = Number(cursor);
}
return getData(req, item.other, cursor).then(data => res.json(data));
}

return store.list(item.other, cursor).then(({ cursor, visits }) => {
res.json({
cursor: cursor,
trapUrl: fullUrl(req, item.other),
visits: visits.map(entity => {
return mergeAndClean(entity.info, {
timestamp: entity.timestamp
});
})
});
});
next();
})
.catch(next);
});


app.use(errors.express);

const server = app.listen(process.env.PORT || 8080, () => {
Expand Down

0 comments on commit e842d4b

Please sign in to comment.