@@ -2,13 +2,15 @@ import express from "express";
22import bodyParser from "body-parser" ;
33import path from "path" ;
44import ejs from "ejs" ;
5- import moment from "moment" ;
65
76import devMode from "./dev-mode" ;
87import SyncAgent from "./sync-agent" ;
98
9+ import KueRouter from "./util/kue-router" ;
10+
11+
1012module . exports = function server ( options = { } ) {
11- const { Hull, hostSecret } = options ;
13+ const { Hull, hostSecret, queue } = options ;
1214 const { Routes } = Hull ;
1315 const { Readme, Manifest } = Routes ;
1416 const app = express ( ) ;
@@ -29,24 +31,38 @@ module.exports = function server(options = {}) {
2931 app . get ( "/" , Readme ) ;
3032 app . get ( "/readme" , Readme ) ;
3133
34+ app . use ( "/kue" , KueRouter ( { hostSecret, queue } ) ) ;
35+
3236 app . use ( Hull . Middleware ( { hostSecret, fetchShip : true , cacheShip : false , requireCredentials : false } ) ) ;
3337
34- app . get ( "/admin.html" , ( req , res ) => {
35- const { ship } = req . hull ;
36- if ( ship . private_settings . connection_string ) {
38+ app . use ( ( req , res , next ) => {
39+ req . agent = new SyncAgent ( { ...req . hull , queue } ) ;
40+ next ( ) ;
41+ } ) ;
42+
43+ function checkConfiguration ( { agent } , res , next ) {
44+ if ( ! agent . isEnabled ( ) ) {
45+ res . status ( 403 ) . json ( { status : "ignored" } ) ;
46+ } else if ( ! agent . isConfigured ( ) ) {
47+ res . status ( 403 ) . json ( { status : "not configured" } ) ;
48+ } else {
49+ next ( ) ;
50+ }
51+ }
52+
53+ app . get ( "/admin.html" , ( { agent } , res ) => {
54+ if ( agent . isConfigured ( ) ) {
3755 res . render ( "connected.html" , {
3856 last_sync_at : null ,
39- ...ship . private_settings
57+ ...agent . ship . private_settings
4058 } ) ;
4159 } else {
4260 res . render ( "home.html" , { } ) ;
4361 }
4462 } ) ;
4563
46- app . post ( "/run" , ( req , res ) => {
47- const { ship } = req . hull ;
48- const query = req . body . query || ship . private_settings . query ;
49- const agent = new SyncAgent ( req . hull ) ;
64+ app . post ( "/run" , ( { body, agent } , res ) => {
65+ const query = body . query || agent . getQuery ( ) ;
5066 agent
5167 . runQuery ( query , { timeout : 20000 } )
5268 . then ( data => res . json ( data ) )
@@ -55,41 +71,15 @@ module.exports = function server(options = {}) {
5571 ) ;
5672 } ) ;
5773
58- app . post ( "/import" , ( req , res ) => {
59- const { private_settings } = req . hull . ship ;
60- const agent = new SyncAgent ( req . hull ) ;
61- agent . streamQuery ( private_settings . query )
62- . catch ( ( err ) => {
63- const { status, message } = err || { } ;
64- res . status ( status || 500 ) . send ( { message } ) ;
65- } )
66- . then ( stream => {
67- res . json ( { status : "working..." } ) ;
68- return agent . startSync ( stream , new Date ( ) ) ;
69- } ) ;
74+ app . post ( "/import" , checkConfiguration , ( { agent } , res ) => {
75+ agent . async ( "startImport" ) ;
76+ res . json ( { status : "scheduled" } ) ;
7077 } ) ;
7178
72- app . post ( "/sync" , ( req , res ) => {
73- const { private_settings = { } } = req . hull . ship ;
74-
75- const oneHourAgo = moment ( ) . subtract ( 1 , "hour" ) . utc ( ) ;
76- const last_updated_at = private_settings . last_updated_at || private_settings . last_sync_at || oneHourAgo . toISOString ( ) ;
77-
78- if ( private_settings . enabled === true ) {
79- req . hull . client . logger . info ( "startSync" , { last_updated_at } ) ;
80- const agent = new SyncAgent ( req . hull ) ;
81- agent . streamQuery ( private_settings . query , { last_updated_at } )
82- . then ( stream => {
83- res . json ( { status : "working" , last_updated_at } ) ;
84- return agent . startSync ( stream , new Date ( ) ) ;
85- } )
86- . catch ( ( { status, message } ) => {
87- res . status ( status || 500 ) . send ( { message } ) ;
88- } ) ;
89- } else {
90- req . hull . client . logger . info ( "skipSync" ) ;
91- res . json ( { status : "ignored" } ) ;
92- }
79+ app . post ( "/sync" , checkConfiguration , ( { agent } , res ) => {
80+ // Return early if sync not enabled
81+ agent . async ( "startSync" ) ;
82+ res . json ( { status : "scheduled" } ) ;
9383 } ) ;
9484
9585 // Error Handler
0 commit comments