aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-09-25 02:56:13 +0700
committerShulhan <ms@kilabit.info>2021-09-25 02:56:13 +0700
commit1eac43241745b46aa16fdae17945409f8cf85203 (patch)
tree9fd3fd61a25b4f17031e343611bd35021b1fc91e
parent4bf3c12f75d140734559acd3d2fcbe6bdc47331a (diff)
downloadgorankusu-1eac43241745b46aa16fdae17945409f8cf85203.tar.xz
example: return all response like HTTP Request in JSON format
The endpoint for GET and POST examples now return the object like HTTP Request which contains the HTTP Method, URL, Headers, Form, MultipartForm, and the Body. This will allow us to inspect the request with logging in to console.
-rw-r--r--example/example.go39
1 files changed, 35 insertions, 4 deletions
diff --git a/example/example.go b/example/example.go
index 3bf7395..3d582eb 100644
--- a/example/example.go
+++ b/example/example.go
@@ -8,7 +8,9 @@ import (
"context"
"encoding/json"
"fmt"
+ "mime/multipart"
"net/http"
+ "net/url"
"sync"
"time"
@@ -30,6 +32,15 @@ const (
websocketAddress = "127.0.0.1:28240"
)
+type requestResponse struct {
+ Method string
+ Url string
+ Headers http.Header
+ Form url.Values
+ MultipartForm *multipart.Form
+ Body string
+}
+
type Example struct {
trunks *trunks.Trunks
wsServer *websocket.Server
@@ -128,7 +139,7 @@ func (ex *Example) registerEndpoints() (err error) {
Path: pathExample,
RequestType: libhttp.RequestTypeForm,
ResponseType: libhttp.ResponseTypeJSON,
- Call: ex.pathExamplePostForm,
+ Call: ex.pathExamplePost,
})
return err
@@ -310,10 +321,19 @@ func (ex *Example) registerTargets() (err error) {
}
func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error) {
+ data := &requestResponse{
+ Method: epr.HttpRequest.Method,
+ Url: epr.HttpRequest.URL.String(),
+ Headers: epr.HttpRequest.Header,
+ Form: epr.HttpRequest.Form,
+ MultipartForm: epr.HttpRequest.MultipartForm,
+ Body: string(epr.RequestBody),
+ }
+
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
res.Message = pathExample
- res.Data = epr.HttpRequest.Form
+ res.Data = data
return json.Marshal(&res)
}
@@ -322,11 +342,22 @@ func (ex *Example) pathExampleErrorGet(epr *libhttp.EndpointRequest) ([]byte, er
return nil, liberrors.Internal(fmt.Errorf("server error"))
}
-func (ex *Example) pathExamplePostForm(epr *libhttp.EndpointRequest) ([]byte, error) {
+func (ex *Example) pathExamplePost(epr *libhttp.EndpointRequest) ([]byte, error) {
+ epr.HttpRequest.ParseMultipartForm(0)
+
+ data := &requestResponse{
+ Method: epr.HttpRequest.Method,
+ Url: epr.HttpRequest.URL.String(),
+ Headers: epr.HttpRequest.Header,
+ Form: epr.HttpRequest.Form,
+ MultipartForm: epr.HttpRequest.MultipartForm,
+ Body: string(epr.RequestBody),
+ }
+
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
res.Message = pathExample
- res.Data = epr.HttpRequest.Form
+ res.Data = data
return json.Marshal(&res)
}