@@ -2,6 +2,7 @@ const url = require('url');
22const { send, createError, sendError } = require ( 'micro' ) ;
33
44const db = require ( './db' ) ;
5+ const healthcheckHandler = require ( './healthcheck' ) ;
56const { pushView } = require ( './utils' ) ;
67
78let sse ;
@@ -13,19 +14,18 @@ if (db.hasFeature('subscribe')) {
1314 sseHandler ( sse ) ;
1415}
1516
16- module . exports = async function ( req , res ) {
17- const { pathname, query } = url . parse ( req . url , /* parseQueryString */ true ) ;
18-
19- if ( pathname === '/_realtime' ) {
20- if ( sse ) {
21- sse . addClient ( req , res ) ;
22- } else {
23- send ( res , 400 , {
24- error : 'The current database adapter does not support live updates.' ,
25- } ) ;
26- }
17+ function realtimeHandler ( req , res ) {
18+ if ( sse ) {
19+ sse . addClient ( req , res ) ;
20+ } else {
21+ send ( res , 400 , {
22+ error : 'The current database adapter does not support live updates.' ,
23+ } ) ;
2724 }
25+ }
2826
27+ async function analyticsHandler ( req , res ) {
28+ const { pathname, query } = url . parse ( req . url , /* parseQueryString */ true ) ;
2929 res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
3030 // Send all views down if "?all" is true
3131 if ( String ( query . all ) === 'true' ) {
@@ -72,4 +72,21 @@ module.exports = async function(req, res) {
7272 console . log ( err ) ;
7373 throw createError ( 500 , 'Internal server error.' ) ;
7474 }
75+ }
76+
77+ module . exports = function createHandler ( options ) {
78+ return async function ( req , res ) {
79+ const { pathname, query } = url . parse ( req . url , /* parseQueryString */ true ) ;
80+
81+ switch ( pathname ) {
82+ case '/_realtime' :
83+ return realtimeHandler ( req , res ) ;
84+
85+ case '/_healthcheck' :
86+ return healthcheckHandler ( options , req , res ) ;
87+
88+ default :
89+ return analyticsHandler ( req , res ) ;
90+ }
91+ } ;
7592} ;
0 commit comments