Skip to content

Commit dff6e93

Browse files
add route to reflect requestbody (#1)
* add route to reflect requestbody * handle req body more generically * unmarshal to interface
1 parent 04a683a commit dff6e93

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

cmd/server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gorilla/mux"
88
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/auth"
9+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/requestbody"
910
)
1011

1112
func main() {
@@ -14,6 +15,7 @@ func main() {
1415
_, _ = w.Write([]byte("pong"))
1516
}).Methods(http.MethodGet)
1617
r.HandleFunc("/auth", auth.HandleAuth).Methods(http.MethodPost)
18+
r.HandleFunc("/requestbody", requestbody.HandleRequestBody).Methods(http.MethodPost)
1719

1820
log.Println("Listening on :8080")
1921
if err := http.ListenAndServe(":8080", r); err != nil {

internal/auth/service.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package auth
22

33
import (
44
"encoding/json"
5-
"errors"
5+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/utils"
66
"io"
7-
"log"
87
"net/http"
98

109
"github.com/speakeasy-api/speakeasy-auth-test-service/pkg/models"
@@ -13,39 +12,18 @@ import (
1312
func HandleAuth(w http.ResponseWriter, r *http.Request) {
1413
body, err := io.ReadAll(r.Body)
1514
if err != nil {
16-
handleError(w, err)
15+
utils.HandleError(w, err)
1716
return
1817
}
1918

2019
var req models.AuthRequest
2120
if err := json.Unmarshal(body, &req); err != nil {
22-
handleError(w, err)
21+
utils.HandleError(w, err)
2322
return
2423
}
2524

2625
if err := checkAuth(req, r); err != nil {
27-
handleError(w, err)
26+
utils.HandleError(w, err)
2827
return
2928
}
3029
}
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-
}

internal/requestbody/service.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

internal/utils/utils.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)