import conf from './dev-server-config.json';

function tryRequire(path, message) {
  try {
    const mod = require(path);
    return mod.default || mod;
  } catch (e) {
    e.message = `${message}\n"${path}" threw: ${e.message}`;
    throw e;
  }
}

export default function serveTask({ port = conf.server.port }) {
  const uwaveClient = tryRequire('../lib/middleware',
    'Could not find the client middleware. Did you run `gulp middleware`?'
  );
  const UWaveServer = tryRequire('u-wave/lib/server',
    'Could not find the u-wave server module. Did you run `npm install u-wave`?'
  );
  const UWaveAPI = tryRequire('u-wave-api-v1/lib/api',
    'Could not find the u-wave API module. Did you run `npm install u-wave-api-v1`?'
  );

  process.on('unhandledRejection', reason => {
    console.error('Unhandled rejection:');
    throw reason;
  });

  const server = new UWaveServer(conf);
  const v1 = new UWaveAPI(conf);

  server.on('stopped', () => process.exit(0));
  server.on('started', uwave => {
    v1.registerWSServer(uwave);
  });

  server.registerAPI('/v1', v1.router);

  server.app.use(uwaveClient());

  server.start();
  server.server.listen(port);
}

@@ -909,16 +909,24 @@
 WaveAPI(
+server, 
 conf);%0A%0A
@@ -972,16 +972,17 @@
 it(0));%0A
+%0A
   server
@@ -986,92 +986,20 @@
 ver.
-on('started', uwave =%3E %7B%0A    v1.registerWSServer(uwave);%0A  %7D);%0A%0A  server.registerAPI
+app%0A    .use
 ('/v
@@ -1012,31 +1012,21 @@
 .router)
-;%0A
 %0A  
-server.app
+  
 .use(uwa

upgrade api-v1 usage