Skip to content

Commit 961a10a

Browse files
committed
feat: added HandleRequest to Handler and renamed package as 'handler'
1 parent 6f85612 commit 961a10a

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

http/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
var (
9+
BadRequest = http.StatusText(http.StatusBadRequest)
910
InternalServerError = http.StatusText(http.StatusInternalServerError)
1011
ServiceUnavailable = http.StatusText(http.StatusServiceUnavailable)
1112
Unauthorized = http.StatusText(http.StatusUnauthorized)

http/response/common.go renamed to http/handler/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package response
1+
package handler
22

33
import (
44
gonethttp "github.com/ralvarezdev/go-net/http"

http/response/errors.go renamed to http/handler/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package response
1+
package handler
22

33
import (
44
"errors"

http/response/handler.go renamed to http/handler/handler.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package response
1+
package handler
22

33
import (
44
goflagsmode "github.com/ralvarezdev/go-flags/mode"
@@ -9,39 +9,63 @@ import (
99
type (
1010
// Handler interface for handling the responses
1111
Handler interface {
12-
HandleSuccess(w http.ResponseWriter, response *Response)
13-
HandleErrorProne(
12+
HandleRequest(
13+
w http.ResponseWriter,
14+
r *http.Request,
15+
data interface{},
16+
) (err error)
17+
HandleSuccessResponse(w http.ResponseWriter, response *Response)
18+
HandleErrorProneResponse(
1419
w http.ResponseWriter,
1520
successResponse *Response,
1621
errorResponse *Response,
1722
)
18-
HandleError(w http.ResponseWriter, response *Response)
23+
HandleErrorResponse(w http.ResponseWriter, response *Response)
1924
}
2025

2126
// DefaultHandler struct
2227
DefaultHandler struct {
2328
mode *goflagsmode.Flag
2429
jsonEncoder gonethttpjson.Encoder
30+
jsonDecoder gonethttpjson.Decoder
2531
}
2632
)
2733

2834
// NewDefaultHandler creates a new default request handler
2935
func NewDefaultHandler(
3036
mode *goflagsmode.Flag,
3137
jsonEncoder gonethttpjson.Encoder,
38+
jsonDecoder gonethttpjson.Decoder,
3239
) (*DefaultHandler, error) {
33-
// Check if the flag mode or the JSON encoder is nil
40+
// Check if the flag mode, the JSON encoder or the JSON decoder is nil
3441
if mode == nil {
3542
return nil, goflagsmode.ErrNilModeFlag
3643
}
3744
if jsonEncoder == nil {
3845
return nil, gonethttpjson.ErrNilJSONEncoder
3946
}
40-
return &DefaultHandler{mode: mode, jsonEncoder: jsonEncoder}, nil
47+
if jsonDecoder == nil {
48+
return nil, gonethttpjson.ErrNilJSONDecoder
49+
}
50+
51+
return &DefaultHandler{
52+
mode: mode,
53+
jsonEncoder: jsonEncoder,
54+
jsonDecoder: jsonDecoder,
55+
}, nil
56+
}
57+
58+
// HandleRequest handles the request
59+
func (d *DefaultHandler) HandleRequest(
60+
w http.ResponseWriter,
61+
r *http.Request,
62+
data interface{},
63+
) (err error) {
64+
return d.jsonDecoder.Decode(w, r, data)
4165
}
4266

43-
// HandleSuccess handles the response
44-
func (d *DefaultHandler) HandleSuccess(
67+
// HandleSuccessResponse handles the response
68+
func (d *DefaultHandler) HandleSuccessResponse(
4569
w http.ResponseWriter,
4670
response *Response,
4771
) {
@@ -52,24 +76,24 @@ func (d *DefaultHandler) HandleSuccess(
5276
}
5377
}
5478

55-
// HandleErrorProne handles the response that may contain an error
56-
func (d *DefaultHandler) HandleErrorProne(
79+
// HandleErrorProneResponse handles the response that may contain an error
80+
func (d *DefaultHandler) HandleErrorProneResponse(
5781
w http.ResponseWriter,
5882
successResponse *Response,
5983
errorResponse *Response,
6084
) {
6185
// Check if the error response is nil
6286
if errorResponse != nil {
63-
d.HandleError(w, errorResponse)
87+
d.HandleErrorResponse(w, errorResponse)
6488
return
6589
}
6690

6791
// Handle the success response
68-
d.HandleSuccess(w, successResponse)
92+
d.HandleSuccessResponse(w, successResponse)
6993
}
7094

71-
// HandleError handles the error response
72-
func (d *DefaultHandler) HandleError(
95+
// HandleErrorResponse handles the error response
96+
func (d *DefaultHandler) HandleErrorResponse(
7397
w http.ResponseWriter,
7498
response *Response,
7599
) {

http/response/types.go renamed to http/handler/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package response
1+
package handler
22

33
type (
44
// Response struct

http/jwt/validator/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package validator
22

33
import (
4+
gonethttpresponse "github.com/ralvarezdev/go-net/http/handler"
45
gonethttpjson "github.com/ralvarezdev/go-net/http/json"
5-
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
66
"net/http"
77
)
88

http/middleware/auth/middleware.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ import (
77
gojwtinterception "github.com/ralvarezdev/go-jwt/token/interception"
88
gojwtvalidator "github.com/ralvarezdev/go-jwt/token/validator"
99
gonethttp "github.com/ralvarezdev/go-net/http"
10+
gonethttpresponse "github.com/ralvarezdev/go-net/http/handler"
1011
gonethttpjwtvalidator "github.com/ralvarezdev/go-net/http/jwt/validator"
11-
gonethttpresponse "github.com/ralvarezdev/go-net/http/response"
1212
"net/http"
1313
"strings"
1414
)
1515

1616
// Middleware struct
1717
type Middleware struct {
1818
validator gojwtvalidator.Validator
19-
responseHandler gonethttpresponse.Handler
19+
handler gonethttpresponse.Handler
2020
jwtValidatorErrorHandler gonethttpjwtvalidator.ErrorHandler
2121
}
2222

2323
// NewMiddleware creates a new authentication middleware
2424
func NewMiddleware(
2525
validator gojwtvalidator.Validator,
26-
responseHandler gonethttpresponse.Handler,
26+
handler gonethttpresponse.Handler,
2727
jwtValidatorErrorHandler gonethttpjwtvalidator.ErrorHandler,
2828
) (*Middleware, error) {
2929
// Check if either the validator, response handler or validator handler is nil
3030
if validator == nil {
3131
return nil, gojwtvalidator.ErrNilValidator
3232
}
33-
if responseHandler == nil {
33+
if handler == nil {
3434
return nil, gonethttpresponse.ErrNilHandler
3535
}
3636
if jwtValidatorErrorHandler == nil {
@@ -39,7 +39,7 @@ func NewMiddleware(
3939

4040
return &Middleware{
4141
validator: validator,
42-
responseHandler: responseHandler,
42+
handler: handler,
4343
jwtValidatorErrorHandler: jwtValidatorErrorHandler,
4444
}, nil
4545
}
@@ -60,7 +60,7 @@ func (m *Middleware) Authenticate(
6060
// Parse the authorization to a string
6161
authorizationStr, ok := authorization.(string)
6262
if !ok {
63-
m.responseHandler.HandleError(
63+
m.handler.HandleErrorResponse(
6464
w,
6565
gonethttpresponse.NewErrorResponseWithCode(
6666
gonethttp.ErrInvalidAuthorizationHeader,
@@ -75,7 +75,7 @@ func (m *Middleware) Authenticate(
7575

7676
// Return an error if the authorization is missing or invalid
7777
if len(parts) < 2 || parts[0] != gojwt.BearerPrefix {
78-
m.responseHandler.HandleError(
78+
m.handler.HandleErrorResponse(
7979
w,
8080
gonethttpresponse.NewErrorResponseWithCode(
8181
gonethttp.ErrInvalidAuthorizationHeader,

0 commit comments

Comments
 (0)