@@ -5,6 +5,7 @@ var os = require('os');
5
5
const app = express ( ) ;
6
6
const port = 999 ;
7
7
var pointsdb = [ ] ;
8
+ var lastUserAgent = '' ;
8
9
9
10
app . use ( function ( req , res , next ) {
10
11
var data = '' ;
@@ -18,11 +19,23 @@ app.use (function(req, res, next) {
18
19
next ( ) ;
19
20
} ) ;
20
21
} ) ;
21
-
22
+ app . get ( '/test/user-agent' , ( req , res ) => {
23
+ res . status ( 200 ) . send ( lastUserAgent ) ;
24
+ } )
22
25
app . get ( '/ready' , ( req , res ) => {
26
+ lastUserAgent = req . get ( 'User-Agent' ) ;
23
27
res . status ( 200 ) . send ( "<html><body><h1>OK</h1></body></html>" ) ;
24
28
} )
25
29
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
+
26
39
app . post ( '/api/v2/write' , ( req , res ) => {
27
40
if ( checkWriteParams ( req , res ) && handleAuthentication ( req , res ) ) {
28
41
var points = req . body ;
@@ -76,6 +89,44 @@ app.post('/api/v2/write', (req,res) => {
76
89
}
77
90
} )
78
91
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
+
79
130
app . post ( '/api/v2/delete' , ( req , res ) => {
80
131
console . log ( 'Deleteting points' ) ;
81
132
pointsdb = [ ] ;
@@ -194,6 +245,16 @@ function checkWriteParams(req, res) {
194
245
}
195
246
}
196
247
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
+
197
258
function checkQueryParams ( req , res ) {
198
259
var org = req . query [ 'org' ] ;
199
260
if ( org != 'my-org' ) {
0 commit comments