Minimum functional handlers for /tt/ to return index.hbs and /tt/status/ to return JSON.
This commit is contained in:
		
							parent
							
								
									6099fbd24a
								
							
						
					
					
						commit
						566dae354d
					
				
							
								
								
									
										76
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								server.js
									
									
									
									
									
								
							@ -37,7 +37,6 @@ app.set('view engine', 'hbs');
 | 
			
		||||
app.set('views', __dirname + '/views');
 | 
			
		||||
 | 
			
		||||
app.get('/', function(req, res){
 | 
			
		||||
    var d = new Date();
 | 
			
		||||
    winston.info("GET /");
 | 
			
		||||
    pg.connect(conString, function(err, client, done) {
 | 
			
		||||
        if(err) {
 | 
			
		||||
@ -63,7 +62,6 @@ app.get('/', function(req, res){
 | 
			
		||||
                    row.statusclass = 'closed';
 | 
			
		||||
                } else { row.statusclass = 'open'; }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            res.render('index', {values: result.rows}, function(err, html) {
 | 
			
		||||
                if(err !== null) {
 | 
			
		||||
                    winston.error(err);
 | 
			
		||||
@ -79,7 +77,6 @@ app.post('/', function(req, res){
 | 
			
		||||
    var postEvent = req.body.postEvent;
 | 
			
		||||
    var source = req.body.source;
 | 
			
		||||
    winston.info(req.body);
 | 
			
		||||
 | 
			
		||||
    // If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value!!!
 | 
			
		||||
    // ???how to fix???
 | 
			
		||||
    // If post body includes coreid and published_at, assume it's from a particle devcie and parse accordingly.
 | 
			
		||||
@ -120,6 +117,79 @@ app.post('/', function(req, res){
 | 
			
		||||
    res.status(204).send('POST received');
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
app.get('/:loc', function(req, res) {
 | 
			
		||||
    var loc = req.params.loc;
 | 
			
		||||
    if (loc !== 'tt') {
 | 
			
		||||
        res.status(404).send('Not found');
 | 
			
		||||
    } else {
 | 
			
		||||
        // This is repeated code from app.get('/') handler.
 | 
			
		||||
        // TODO, refactor as exported function in courtsopenUtils.js.
 | 
			
		||||
        winston.info("GET /tt");
 | 
			
		||||
        pg.connect(conString, function(err, client, done) {
 | 
			
		||||
            if(err) {
 | 
			
		||||
                return winston.error('error fetching client from pool', err);
 | 
			
		||||
            }
 | 
			
		||||
            var devIndexQry = 
 | 
			
		||||
            "select status, published_at " + 
 | 
			
		||||
            "from alerts " + 
 | 
			
		||||
            "where status in ('Open', 'Closed') " +
 | 
			
		||||
            "order by published_at desc " +
 | 
			
		||||
            "limit 2"
 | 
			
		||||
            client.query(devIndexQry, function(err, result) {
 | 
			
		||||
                //call `done()` to release the client back to the pool
 | 
			
		||||
                done();
 | 
			
		||||
                if(err) {
 | 
			
		||||
                    return winston.error('error running query', err);
 | 
			
		||||
                }
 | 
			
		||||
                // Loop over elements in rows array, convert ugly UTC times to pretty local times.
 | 
			
		||||
                result.rows.forEach(function(row){
 | 
			
		||||
                    row.pubdate = courtsopenUtils.getLocDateFromUTC(row.published_at);
 | 
			
		||||
                    row.pubtime = courtsopenUtils.getLocTimeFromUTC(row.published_at);
 | 
			
		||||
                    if(row.status.toLowerCase().indexOf('closed') > -1){
 | 
			
		||||
                        row.statusclass = 'closed';
 | 
			
		||||
                    } else { row.statusclass = 'open'; }
 | 
			
		||||
                });
 | 
			
		||||
                res.render('index', {values: result.rows}, function(err, html) {
 | 
			
		||||
                    if(err !== null) {
 | 
			
		||||
                        winston.error(err);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        res.send(html);
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
app.get('/:loc/status', function(req, res) {
 | 
			
		||||
    var loc = req.params.loc;
 | 
			
		||||
    if (loc !== 'tt') {
 | 
			
		||||
        res.status(404).send('Not found');
 | 
			
		||||
    } else {
 | 
			
		||||
        winston.info('GET ' + loc + '/status');
 | 
			
		||||
        // Lookup most recent status from DB and return as JSON.
 | 
			
		||||
        pg.connect(conString, function(err, client, done) {
 | 
			
		||||
            if(err) {
 | 
			
		||||
                return winston.error('error fetching client from pool', err);
 | 
			
		||||
            }
 | 
			
		||||
            var mostRecentStatusQry = 
 | 
			
		||||
            "select origjson " + 
 | 
			
		||||
            "from alerts " + 
 | 
			
		||||
            "where status in ('Open', 'Closed') " +
 | 
			
		||||
            "order by published_at desc " +
 | 
			
		||||
            "limit 1"
 | 
			
		||||
            client.query(mostRecentStatusQry, function(err, result) {
 | 
			
		||||
                //call `done()` to release the client back to the pool
 | 
			
		||||
                done();
 | 
			
		||||
                if(err) {
 | 
			
		||||
                    return winston.error('error running query', err);
 | 
			
		||||
                }
 | 
			
		||||
                res.send(result.rows[0].origjson);
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
setInterval(function() {
 | 
			
		||||
    // Check every hour to see if GoodMorning or GoodEvening has gone missing.
 | 
			
		||||
    pg.connect(conString, function(err, client, done) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user