File tree Expand file tree Collapse file tree 4 files changed +66
-26
lines changed Expand file tree Collapse file tree 4 files changed +66
-26
lines changed Original file line number Diff line number Diff line change 6
6
7
7
"github.com/gorilla/mux"
8
8
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/auth"
9
+ "github.com/speakeasy-api/speakeasy-auth-test-service/internal/requestbody"
9
10
)
10
11
11
12
func main () {
@@ -14,6 +15,7 @@ func main() {
14
15
_ , _ = w .Write ([]byte ("pong" ))
15
16
}).Methods (http .MethodGet )
16
17
r .HandleFunc ("/auth" , auth .HandleAuth ).Methods (http .MethodPost )
18
+ r .HandleFunc ("/requestbody" , requestbody .HandleRequestBody ).Methods (http .MethodPost )
17
19
18
20
log .Println ("Listening on :8080" )
19
21
if err := http .ListenAndServe (":8080" , r ); err != nil {
Original file line number Diff line number Diff line change @@ -2,9 +2,8 @@ package auth
2
2
3
3
import (
4
4
"encoding/json"
5
- "errors "
5
+ "github.com/speakeasy-api/speakeasy-auth-test-service/internal/utils "
6
6
"io"
7
- "log"
8
7
"net/http"
9
8
10
9
"github.com/speakeasy-api/speakeasy-auth-test-service/pkg/models"
@@ -13,39 +12,18 @@ import (
13
12
func HandleAuth (w http.ResponseWriter , r * http.Request ) {
14
13
body , err := io .ReadAll (r .Body )
15
14
if err != nil {
16
- handleError (w , err )
15
+ utils . HandleError (w , err )
17
16
return
18
17
}
19
18
20
19
var req models.AuthRequest
21
20
if err := json .Unmarshal (body , & req ); err != nil {
22
- handleError (w , err )
21
+ utils . HandleError (w , err )
23
22
return
24
23
}
25
24
26
25
if err := checkAuth (req , r ); err != nil {
27
- handleError (w , err )
26
+ utils . HandleError (w , err )
28
27
return
29
28
}
30
29
}
31
-
32
- func handleError (w http.ResponseWriter , err error ) {
33
- log .Println (err )
34
-
35
- data , marshalErr := json .Marshal (models.ErrorResponse {
36
- Error : models.ErrorMessage {
37
- Message : err .Error (),
38
- },
39
- })
40
- if marshalErr != nil {
41
- w .WriteHeader (http .StatusInternalServerError )
42
- return
43
- }
44
-
45
- if errors .Is (err , authError ) {
46
- w .WriteHeader (http .StatusUnauthorized )
47
- } else {
48
- w .WriteHeader (http .StatusInternalServerError )
49
- }
50
- _ , _ = w .Write (data )
51
- }
Original file line number Diff line number Diff line change
1
+ package requestbody
2
+
3
+ import (
4
+ "encoding/json"
5
+ "github.com/speakeasy-api/speakeasy-auth-test-service/internal/utils"
6
+ "io"
7
+ "net/http"
8
+ )
9
+
10
+ func HandleRequestBody (w http.ResponseWriter , r * http.Request ) {
11
+ body , err := io .ReadAll (r .Body )
12
+ if err != nil {
13
+ utils .HandleError (w , err )
14
+ return
15
+ }
16
+
17
+ var req interface {}
18
+ if err := json .Unmarshal (body , & req ); err != nil {
19
+ utils .HandleError (w , err )
20
+ return
21
+ }
22
+
23
+ w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
24
+
25
+ if err := json .NewEncoder (w ).Encode (req ); err != nil {
26
+ utils .HandleError (w , err )
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package utils
2
+
3
+ import (
4
+ "encoding/json"
5
+ "errors"
6
+ "github.com/speakeasy-api/speakeasy-auth-test-service/pkg/models"
7
+ "log"
8
+ "net/http"
9
+ )
10
+
11
+ var authError = errors .New ("invalid auth" )
12
+
13
+ func HandleError (w http.ResponseWriter , err error ) {
14
+ log .Println (err )
15
+
16
+ data , marshalErr := json .Marshal (models.ErrorResponse {
17
+ Error : models.ErrorMessage {
18
+ Message : err .Error (),
19
+ },
20
+ })
21
+ if marshalErr != nil {
22
+ w .WriteHeader (http .StatusInternalServerError )
23
+ return
24
+ }
25
+
26
+ if errors .Is (err , authError ) {
27
+ w .WriteHeader (http .StatusUnauthorized )
28
+ } else {
29
+ w .WriteHeader (http .StatusInternalServerError )
30
+ }
31
+ _ , _ = w .Write (data )
32
+ }
You can’t perform that action at this time.
0 commit comments