From bade24097f4a051528da9722045810bb9d1855ca Mon Sep 17 00:00:00 2001 From: some Date: Wed, 9 Oct 2024 00:14:54 -0400 Subject: [PATCH] Attempt generic NewServer --- go.mod | 2 +- go.sum | 4 ++-- server/callback.go | 22 +++++++--------------- server/token.go | 22 +++++++--------------- 4 files changed, 17 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index a94ac65..e2643ca 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.1 require ( somehole.com/common/log v0.1.3 somehole.com/common/security v0.2.1 - somehole.com/service/router v0.12.0 + somehole.com/service/router v0.13.3 ) require ( diff --git a/go.sum b/go.sum index 880fd00..25ece5b 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,5 @@ somehole.com/common/log v0.1.3 h1:2PAui0+5EryTAHqVUZQeepLcJTGssHKz2OL+jBknHI0= somehole.com/common/log v0.1.3/go.mod h1:NS2eHnN120GA6oFbBm3XhB5yHww0eXTbLuMQYZxYNyA= somehole.com/common/security v0.2.1 h1:H7TpErYKsCOxKYYie75jU7azO5vEgeqrQ9uA+AjXafA= somehole.com/common/security v0.2.1/go.mod h1:6SMKdIrfxT460XXWafpD6A9yT/ykwipGsGztA5g5vVM= -somehole.com/service/router v0.12.0 h1:nrBn+OJF9cN22vgXcZYdcSbLTamPwMEvu4NG2lJLO88= -somehole.com/service/router v0.12.0/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM= +somehole.com/service/router v0.13.3 h1:q4jGnqMIB+o78ZUmKy9HVMFpO/fjRh6Wud3WGIMH1rM= +somehole.com/service/router v0.13.3/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM= diff --git a/server/callback.go b/server/callback.go index 73ade5c..57b1e02 100644 --- a/server/callback.go +++ b/server/callback.go @@ -1,14 +1,12 @@ package server import ( - "encoding/json" - "somehole.com/service/oauth2/session" "somehole.com/service/router" ) type CallbackRequestBuilder struct { - router.UnimplementedRequestBuilder + router.DefaultRequestBuilder values struct { router.Values State session.State `form:"state"` @@ -17,27 +15,21 @@ type CallbackRequestBuilder struct { } func (rb CallbackRequestBuilder) RequestBuilder() router.RequestBuilder { + rb.DefaultRequestBuilder = router.NewDefaultRequestBuilder(&rb, nil, nil, nil, &rb.values.Values, &rb.values, nil, nil) return &rb } -func (rb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) { - err := rb.values.Values.Parse(values) - if err != nil { - return ErrorBadRequest - } - return Ok -} - type CallbackResponse struct { - router.UnimplementedResponse + router.DefaultResponse Body struct { + router.Body Message string `json:"message"` } } -func (r *CallbackResponse) BodyBytes() (body []byte) { - body, _ = json.Marshal(r.Body) - return +func (r CallbackResponse) Response() router.Response { + r.DefaultResponse = router.NewDefaultResponse(&r, nil, nil, &r.Body.Body, &r.Body) + return &r } type UnimplementedCallbackServer struct{} diff --git a/server/token.go b/server/token.go index 1ee938b..cb7703f 100644 --- a/server/token.go +++ b/server/token.go @@ -1,14 +1,12 @@ package server import ( - "encoding/json" - "somehole.com/service/oauth2/session" "somehole.com/service/router" ) type TokenRequestBuilder struct { - router.UnimplementedRequestBuilder + router.DefaultRequestBuilder values struct { router.Values ResponseType string `form:"response_type"` @@ -18,20 +16,14 @@ type TokenRequestBuilder struct { } func (rb TokenRequestBuilder) RequestBuilder() router.RequestBuilder { + rb.DefaultRequestBuilder = router.NewDefaultRequestBuilder(&rb, nil, nil, nil, &rb.values.Values, &rb.values, nil, nil) return &rb } -func (rb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) { - err := rb.values.Values.Parse(values) - if err != nil { - return ErrorBadRequest - } - return Ok -} - type TokenResponse struct { - router.UnimplementedResponse + router.DefaultResponse Body struct { + router.Body VerificationUri string `json:"verification_uri"` UserCode session.Code `json:"user_code"` DeviceCode session.Code `json:"device_code"` @@ -40,9 +32,9 @@ type TokenResponse struct { } } -func (r *TokenResponse) BodyBytes() (body []byte) { - body, _ = json.Marshal(r.Body) - return +func (r TokenResponse) Response() router.Response { + r.DefaultResponse = router.NewDefaultResponse(&r, nil, nil, &r.Body.Body, &r.Body) + return &r } type UnimplementedTokenServer struct{}