-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathweb.ts
More file actions
47 lines (39 loc) · 1.62 KB
/
web.ts
File metadata and controls
47 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// <reference path="utils.ts" />
/// <reference path="../common/models.ts" />
/// <reference path="../common/messaging.ts" />
/// <reference path="persister.ts"/>
import Models = require("../common/models");
import Messaging = require("../common/messaging");
import Utils = require("./utils");
import _ = require("lodash");
import util = require("util");
import express = require("express");
import Q = require("q");
import Persister = require("./persister");
import moment = require("moment");
export class StandaloneHttpPublisher<T> {
constructor(
private _wrapped: Messaging.IPublish<T>,
private route: string,
private _httpApp: express.Application,
private _persister: Persister.ILoadAll<T>) {
_httpApp.get("/data/" + route, (req: express.Request, res: express.Response) => {
var getParameter = <T>(pName: string, cvt: (r: string) => T) => {
var rawMax : string = req.param(pName, null);
return (rawMax === null ? null : cvt(rawMax));
};
var max = getParameter<number>("max", r => parseInt(r));
var startTime = getParameter<moment.Moment>("start_time", r => moment(r));
var handler = (d: T[]) => {
if (max !== null && max <= d.length)
d = _.takeRight(d, max);
res.json(d);
};
_persister.loadAll(max, startTime).then(handler);
});
}
public publish = this._wrapped.publish;
public registerSnapshot = (generator: () => T[]) => {
return this._wrapped.registerSnapshot(generator);
}
}