@@ -5,6 +5,7 @@ var os = require('os');
55const app = express ( ) ;
66const port = 999 ;
77var pointsdb = [ ] ;
8+ var lastUserAgent = '' ;
89
910app . use ( function ( req , res , next ) {
1011 var data = '' ;
@@ -18,11 +19,23 @@ app.use (function(req, res, next) {
1819 next ( ) ;
1920 } ) ;
2021} ) ;
21-
22+ app . get ( '/test/user-agent' , ( req , res ) => {
23+ res . status ( 200 ) . send ( lastUserAgent ) ;
24+ } )
2225app . get ( '/ready' , ( req , res ) => {
26+ lastUserAgent = req . get ( 'User-Agent' ) ;
2327 res . status ( 200 ) . send ( "<html><body><h1>OK</h1></body></html>" ) ;
2428} )
2529
30+ app . get ( '/ping' , ( req , res ) => {
31+ lastUserAgent = req . get ( 'User-Agent' ) ;
32+ if ( req . query [ 'verbose' ] == 'true' ) {
33+ res . status ( 200 ) . send ( "<html><body><h1>OK</h1></body></html>" ) ;
34+ } else {
35+ res . status ( 204 ) . end ( ) ;
36+ }
37+ } )
38+
2639app . post ( '/api/v2/write' , ( req , res ) => {
2740 if ( checkWriteParams ( req , res ) && handleAuthentication ( req , res ) ) {
2841 var points = req . body ;
@@ -76,6 +89,44 @@ app.post('/api/v2/write', (req,res) => {
7689 }
7790} )
7891
92+ app . post ( '/write' , ( req , res ) => {
93+ if ( checkWriteParamsV1 ( req , res ) ) {
94+ var points = req . body ;
95+ if ( Array . isArray ( points ) && points . length > 0 ) {
96+ var point = points [ 0 ] ;
97+ if ( point . tags . hasOwnProperty ( 'direction' ) ) {
98+ switch ( point . tags . direction ) {
99+ case 'delete-all' :
100+ pointsdb = [ ] ;
101+ res . status ( 204 ) . end ( ) ;
102+ break ;
103+ case '400' :
104+ points = [ ] ;
105+ res . status ( 400 ) . send ( "bad request" ) ;
106+ break ;
107+ case '500' :
108+ points = [ ] ;
109+ res . status ( 500 ) . send ( "internal server error" ) ;
110+ break ;
111+ }
112+ points . shift ( ) ;
113+ }
114+ console . log ( "write " + points . length + ' points' ) ;
115+ points . forEach ( ( item , index ) => {
116+ pointsdb . push ( item ) ;
117+ } )
118+ if ( res . statusCode < 299 ) {
119+ res . status ( 204 ) . end ( ) ;
120+ }
121+ } else {
122+ res . status ( 204 ) . end ( ) ;
123+ }
124+ }
125+ if ( res . statusCode != 204 ) {
126+ console . log ( 'Responded with ' + res . statusCode ) ;
127+ }
128+ } )
129+
79130app . post ( '/api/v2/delete' , ( req , res ) => {
80131 console . log ( 'Deleteting points' ) ;
81132 pointsdb = [ ] ;
@@ -194,6 +245,16 @@ function checkWriteParams(req, res) {
194245 }
195246}
196247
248+ function checkWriteParamsV1 ( req , res ) {
249+ var db = req . query [ 'db' ] ;
250+ if ( db != 'my-db' ) {
251+ res . status ( 404 ) . send ( `{"code":"not found","message":"database \"${ db } \" not found"}` ) ;
252+ return false ;
253+ } else {
254+ return true ;
255+ }
256+ }
257+
197258function checkQueryParams ( req , res ) {
198259 var org = req . query [ 'org' ] ;
199260 if ( org != 'my-org' ) {
0 commit comments