@@ -12,9 +12,8 @@ import (
12
12
"net/http"
13
13
"net/url"
14
14
"strconv"
15
+ "strings"
15
16
"time"
16
-
17
- "github.com/gorilla/mux"
18
17
)
19
18
20
19
// An HandlerStartOperationResult is the return type from the [Handler] StartOperation and [Operation] Start methods. It
@@ -264,18 +263,7 @@ func (h *baseHTTPHandler) writeFailure(writer http.ResponseWriter, err error) {
264
263
}
265
264
}
266
265
267
- func (h * httpHandler ) startOperation (writer http.ResponseWriter , request * http.Request ) {
268
- vars := mux .Vars (request )
269
- service , err := url .PathUnescape (vars ["service" ])
270
- if err != nil {
271
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
272
- return
273
- }
274
- operation , err := url .PathUnescape (vars ["operation" ])
275
- if err != nil {
276
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
277
- return
278
- }
266
+ func (h * httpHandler ) startOperation (service , operation string , writer http.ResponseWriter , request * http.Request ) {
279
267
options := StartOperationOptions {
280
268
RequestID : request .Header .Get (headerRequestID ),
281
269
CallbackURL : request .URL .Query ().Get (queryCallbackURL ),
@@ -304,23 +292,7 @@ func (h *httpHandler) startOperation(writer http.ResponseWriter, request *http.R
304
292
}
305
293
}
306
294
307
- func (h * httpHandler ) getOperationResult (writer http.ResponseWriter , request * http.Request ) {
308
- vars := mux .Vars (request )
309
- service , err := url .PathUnescape (vars ["service" ])
310
- if err != nil {
311
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
312
- return
313
- }
314
- operation , err := url .PathUnescape (vars ["operation" ])
315
- if err != nil {
316
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
317
- return
318
- }
319
- operationID , err := url .PathUnescape (vars ["operation_id" ])
320
- if err != nil {
321
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
322
- return
323
- }
295
+ func (h * httpHandler ) getOperationResult (service , operation , operationID string , writer http.ResponseWriter , request * http.Request ) {
324
296
options := GetOperationResultOptions {Header : httpHeaderToNexusHeader (request .Header )}
325
297
326
298
// If both Request-Timeout http header and wait query string are set, the minimum of the Request-Timeout header
@@ -365,23 +337,7 @@ func (h *httpHandler) getOperationResult(writer http.ResponseWriter, request *ht
365
337
h .writeResult (writer , result )
366
338
}
367
339
368
- func (h * httpHandler ) getOperationInfo (writer http.ResponseWriter , request * http.Request ) {
369
- vars := mux .Vars (request )
370
- service , err := url .PathUnescape (vars ["service" ])
371
- if err != nil {
372
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
373
- return
374
- }
375
- operation , err := url .PathUnescape (vars ["operation" ])
376
- if err != nil {
377
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
378
- return
379
- }
380
- operationID , err := url .PathUnescape (vars ["operation_id" ])
381
- if err != nil {
382
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
383
- return
384
- }
340
+ func (h * httpHandler ) getOperationInfo (service , operation , operationID string , writer http.ResponseWriter , request * http.Request ) {
385
341
options := GetOperationInfoOptions {Header : httpHeaderToNexusHeader (request .Header )}
386
342
387
343
ctx , cancel , ok := h .contextWithTimeoutFromHTTPRequest (writer , request )
@@ -407,23 +363,7 @@ func (h *httpHandler) getOperationInfo(writer http.ResponseWriter, request *http
407
363
}
408
364
}
409
365
410
- func (h * httpHandler ) cancelOperation (writer http.ResponseWriter , request * http.Request ) {
411
- vars := mux .Vars (request )
412
- service , err := url .PathUnescape (vars ["service" ])
413
- if err != nil {
414
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
415
- return
416
- }
417
- operation , err := url .PathUnescape (vars ["operation" ])
418
- if err != nil {
419
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
420
- return
421
- }
422
- operationID , err := url .PathUnescape (vars ["operation_id" ])
423
- if err != nil {
424
- h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
425
- return
426
- }
366
+ func (h * httpHandler ) cancelOperation (service , operation , operationID string , writer http.ResponseWriter , request * http.Request ) {
427
367
options := CancelOperationOptions {Header : httpHeaderToNexusHeader (request .Header )}
428
368
429
369
ctx , cancel , ok := h .contextWithTimeoutFromHTTPRequest (writer , request )
@@ -488,6 +428,67 @@ type HandlerOptions struct {
488
428
Serializer Serializer
489
429
}
490
430
431
+ func (h * httpHandler ) handleRequest (writer http.ResponseWriter , request * http.Request ) {
432
+ parts := strings .Split (request .URL .EscapedPath (), "/" )
433
+ // First part is empty (due to leading /)
434
+ if len (parts ) < 3 {
435
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeNotFound , "not found" ))
436
+ return
437
+ }
438
+ service , err := url .PathUnescape (parts [1 ])
439
+ if err != nil {
440
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
441
+ return
442
+ }
443
+ operation , err := url .PathUnescape (parts [2 ])
444
+ if err != nil {
445
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
446
+ return
447
+ }
448
+ var operationID string
449
+ if len (parts ) > 3 {
450
+ operationID , err = url .PathUnescape (parts [3 ])
451
+ if err != nil {
452
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "failed to parse URL path" ))
453
+ return
454
+ }
455
+ }
456
+
457
+ switch len (parts ) {
458
+ case 3 : // /{service}/{operation}
459
+ if request .Method != "POST" {
460
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "invalid request method: expected POST, got %q" , request .Method ))
461
+ return
462
+ }
463
+ h .startOperation (service , operation , writer , request )
464
+ case 4 : // /{service}/{operation}/{operation_id}
465
+ if request .Method != "GET" {
466
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "invalid request method: expected GET, got %q" , request .Method ))
467
+ return
468
+ }
469
+ h .getOperationInfo (service , operation , operationID , writer , request )
470
+ case 5 :
471
+ switch parts [4 ] {
472
+ case "result" : // /{service}/{operation}/{operation_id}/result
473
+ if request .Method != "GET" {
474
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "invalid request method: expected GET, got %q" , request .Method ))
475
+ return
476
+ }
477
+ h .getOperationResult (service , operation , operationID , writer , request )
478
+ case "cancel" : // /{service}/{operation}/{operation_id}/cancel
479
+ if request .Method != "POST" {
480
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeBadRequest , "invalid request method: expected POST, got %q" , request .Method ))
481
+ return
482
+ }
483
+ h .cancelOperation (service , operation , operationID , writer , request )
484
+ default :
485
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeNotFound , "not found" ))
486
+ }
487
+ default :
488
+ h .writeFailure (writer , HandlerErrorf (HandlerErrorTypeNotFound , "not found" ))
489
+ }
490
+ }
491
+
491
492
// NewHTTPHandler constructs an [http.Handler] from given options for handling Nexus service requests.
492
493
func NewHTTPHandler (options HandlerOptions ) http.Handler {
493
494
if options .Logger == nil {
@@ -506,10 +507,5 @@ func NewHTTPHandler(options HandlerOptions) http.Handler {
506
507
options : options ,
507
508
}
508
509
509
- router := mux .NewRouter ().UseEncodedPath ()
510
- router .HandleFunc ("/{service}/{operation}" , handler .startOperation ).Methods ("POST" )
511
- router .HandleFunc ("/{service}/{operation}/{operation_id}" , handler .getOperationInfo ).Methods ("GET" )
512
- router .HandleFunc ("/{service}/{operation}/{operation_id}/result" , handler .getOperationResult ).Methods ("GET" )
513
- router .HandleFunc ("/{service}/{operation}/{operation_id}/cancel" , handler .cancelOperation ).Methods ("POST" )
514
- return router
510
+ return http .HandlerFunc (handler .handleRequest )
515
511
}
0 commit comments