Skip to content

Commit bbded3b

Browse files
authored
Remove gorilla mux dependency (#15)
Also bump version to 0.0.9. ## Why? We want to minimize dependencies for our SDKs.
1 parent cd9d6c5 commit bbded3b

File tree

4 files changed

+68
-75
lines changed

4 files changed

+68
-75
lines changed

Diff for: go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.21
44

55
require (
66
github.com/google/uuid v1.3.0
7-
github.com/gorilla/mux v1.8.0
87
github.com/stretchr/testify v1.8.4
98
)
109

Diff for: go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
44
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5-
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
6-
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
75
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
86
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
97
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=

Diff for: nexus/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// Package version.
18-
const version = "v0.0.8"
18+
const version = "v0.0.9"
1919

2020
const (
2121
// Nexus specific headers.

Diff for: nexus/server.go

+67-71
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212
"net/http"
1313
"net/url"
1414
"strconv"
15+
"strings"
1516
"time"
16-
17-
"github.com/gorilla/mux"
1817
)
1918

2019
// 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) {
264263
}
265264
}
266265

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) {
279267
options := StartOperationOptions{
280268
RequestID: request.Header.Get(headerRequestID),
281269
CallbackURL: request.URL.Query().Get(queryCallbackURL),
@@ -304,23 +292,7 @@ func (h *httpHandler) startOperation(writer http.ResponseWriter, request *http.R
304292
}
305293
}
306294

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) {
324296
options := GetOperationResultOptions{Header: httpHeaderToNexusHeader(request.Header)}
325297

326298
// 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
365337
h.writeResult(writer, result)
366338
}
367339

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) {
385341
options := GetOperationInfoOptions{Header: httpHeaderToNexusHeader(request.Header)}
386342

387343
ctx, cancel, ok := h.contextWithTimeoutFromHTTPRequest(writer, request)
@@ -407,23 +363,7 @@ func (h *httpHandler) getOperationInfo(writer http.ResponseWriter, request *http
407363
}
408364
}
409365

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) {
427367
options := CancelOperationOptions{Header: httpHeaderToNexusHeader(request.Header)}
428368

429369
ctx, cancel, ok := h.contextWithTimeoutFromHTTPRequest(writer, request)
@@ -488,6 +428,67 @@ type HandlerOptions struct {
488428
Serializer Serializer
489429
}
490430

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+
491492
// NewHTTPHandler constructs an [http.Handler] from given options for handling Nexus service requests.
492493
func NewHTTPHandler(options HandlerOptions) http.Handler {
493494
if options.Logger == nil {
@@ -506,10 +507,5 @@ func NewHTTPHandler(options HandlerOptions) http.Handler {
506507
options: options,
507508
}
508509

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)
515511
}

0 commit comments

Comments
 (0)