diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 10b731c..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 279f2fa..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7a83c72..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/requests.iml b/.idea/requests.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/requests.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 392905a..4ff97c4 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ ### Quick Start #### Get Started ```shell -cat examples/example_1/main.go +cat github.com/golang-io/examples/example_1/main.go ``` ```go @@ -44,7 +44,7 @@ func main() { ``` ```shell -$ go run examples/example_1/main.go +$ go run github.com/golang-io/examples/example_1/main.go * Connect: httpbin.org:80 * Got Conn: -> * Connect: httpbin.org:443 @@ -112,7 +112,7 @@ func main() { ``` ```shell -% go run examples/example_2/main.go +% go run github.com/golang-io/examples/example_2/main.go 2024/03/22 20:31:12 { "args": {}, "headers": { @@ -153,7 +153,7 @@ func main() { ``` ```shell -% go run examples/example_3/main.go +% go run github.com/golang-io/examples/example_3/main.go 2024/03/22 20:43:25 resp={ "args": {}, "data": "Hello world", @@ -285,7 +285,7 @@ pong ``` And, there are some logs from server. ```shell -% go run examples/server/example_1/main.go +% go run github.com/golang-io/examples/server/example_1/main.go 2024-03-27 02:47:47 http serve 0.0.0.0:1234 2024/03/27 02:47:59 "GET http://127.0.0.1:1234/echo HTTP/1.1" from 127.0.0.1:60922 - 000 0B in 9.208µs path use {} diff --git a/examples/example_1/main.go b/examples/example_1/main.go deleted file mode 100644 index 41bd72d..0000000 --- a/examples/example_1/main.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "context" - "github.com/golang-io/requests" -) - -func main() { - sess := requests.New(requests.URL("https://httpbin.org/uuid"), requests.TraceLv(4)) - _, _ = sess.DoRequest(context.TODO()) -} diff --git a/examples/example_2/main.go b/examples/example_2/main.go deleted file mode 100644 index 2edc86d..0000000 --- a/examples/example_2/main.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "bytes" - "github.com/golang-io/requests" - "log" -) - -func main() { - resp, err := requests.Get("https://httpbin.org/get") - if err != nil { - log.Fatalln(err) - } - defer resp.Body.Close() - var buf bytes.Buffer - if _, err := buf.ReadFrom(resp.Body); err != nil { - log.Fatalln(err) - } - log.Println(buf.String()) -} diff --git a/examples/example_3/main.go b/examples/example_3/main.go deleted file mode 100644 index 7aa081b..0000000 --- a/examples/example_3/main.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "context" - "github.com/golang-io/requests" - "log" -) - -func main() { - sess := requests.New(requests.URL("http://httpbin.org/post")) - resp, err := sess.DoRequest(context.TODO(), requests.MethodPost, requests.Body("Hello world")) - log.Printf("resp=%s, err=%v", resp.Content, err) -} diff --git a/examples/server/example_1/go.mod b/examples/server/example_1/go.mod deleted file mode 100644 index 4f1f5a2..0000000 --- a/examples/server/example_1/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module main - -go 1.22.1 - -require ( - github.com/go-chi/chi/v5 v5.0.12 - github.com/golang-io/requests v0.0.0-00010101000000-000000000000 - github.com/gorilla/websocket v1.5.1 -) - -require golang.org/x/net v0.17.0 // indirect - -replace github.com/golang-io/requests => ../../../ diff --git a/examples/server/example_1/go.sum b/examples/server/example_1/go.sum deleted file mode 100644 index 05c21df..0000000 --- a/examples/server/example_1/go.sum +++ /dev/null @@ -1,6 +0,0 @@ -github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= -github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= diff --git a/examples/server/example_1/main.go b/examples/server/example_1/main.go deleted file mode 100644 index ead76da..0000000 --- a/examples/server/example_1/main.go +++ /dev/null @@ -1,72 +0,0 @@ -package main - -import ( - "context" - "fmt" - "github.com/go-chi/chi/v5/middleware" - "github.com/golang-io/requests" - "github.com/gorilla/websocket" - "io" - "log" - "net/http" -) - -var upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, - CheckOrigin: func(r *http.Request) bool { - return true - }, -} - -func ws(w http.ResponseWriter, r *http.Request) { - conn, err := upgrader.Upgrade(w, r, nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer conn.Close() - for { - messageType, message, err := conn.ReadMessage() - if err != nil { - log.Printf("Failed to read message: %v", err) - break - } - - log.Printf("Received message: %s", message) - - err = conn.WriteMessage(messageType, message) - if err != nil { - log.Printf("Failed to write message: %v", err) - break - } - } -} - -func main() { - r := requests.NewServeMux( - requests.URL("0.0.0.0:1234"), - requests.Use(middleware.Recoverer, middleware.Logger), - requests.Use(func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - next.ServeHTTP(w, r) - }) - }), - ) - r.Route("/panic", func(w http.ResponseWriter, r *http.Request) { - panic("panic test") - }) - r.Route("/echo", func(w http.ResponseWriter, r *http.Request) { - _, _ = io.Copy(w, r.Body) - }) - r.Route("/ping", func(w http.ResponseWriter, r *http.Request) { - _, _ = fmt.Fprintf(w, "pong\n") - }, requests.Use(func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - next.ServeHTTP(w, r) - }) - })) - r.Route("/ws", ws) - err := requests.ListenAndServe(context.Background(), r) - fmt.Println(err) -} diff --git a/examples/websocket/client/go.mod b/examples/websocket/client/go.mod deleted file mode 100644 index 23828ec..0000000 --- a/examples/websocket/client/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module main - -go 1.22.2 - -require github.com/gorilla/websocket v1.5.1 - -require golang.org/x/net v0.17.0 // indirect diff --git a/examples/websocket/client/go.sum b/examples/websocket/client/go.sum deleted file mode 100644 index 272772f..0000000 --- a/examples/websocket/client/go.sum +++ /dev/null @@ -1,4 +0,0 @@ -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= diff --git a/examples/websocket/client/main.go b/examples/websocket/client/main.go deleted file mode 100644 index ef6f928..0000000 --- a/examples/websocket/client/main.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "github.com/gorilla/websocket" - "log" - "os" - "os/signal" - "time" -) - -func main() { - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt) - - conn, _, err := websocket.DefaultDialer.Dial("ws://localhost:1234/ws", nil) - if err != nil { - log.Fatalf("Failed to connect to WebSocket server: %v", err) - } - defer conn.Close() - - done := make(chan struct{}) - - go func() { - defer close(done) - for { - _, message, err := conn.ReadMessage() - if err != nil { - log.Printf("Failed to read message: %v", err) - return - } - log.Printf("Received message: %s", message) - } - }() - - ticker := time.NewTicker(time.Second) - defer ticker.Stop() - - for { - select { - case <-done: - return - case <-ticker.C: - err := conn.WriteMessage(websocket.TextMessage, []byte("Hello, WebSocket server!")) - if err != nil { - log.Printf("Failed to write message: %v", err) - return - } - case <-interrupt: - log.Println("Interrupted, closing connection...") - - err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) - if err != nil { - log.Printf("Failed to write close message: %v", err) - return - } - - select { - case <-done: - case <-time.After(time.Second): - } - return - } - } -} diff --git a/stat.go b/stat.go index c4e94a3..69f8868 100644 --- a/stat.go +++ b/stat.go @@ -6,19 +6,32 @@ import ( ) const RequestId = "Request-Id" +const dateTime = "2006-01-02 15:04:05.000" // Stat stats type Stat struct { - RequestId string + RequestId string `json:"RequestId"` StartAt string `json:"StartAt"` Cost int64 `json:"Cost"` - Request struct { + + Request struct { + // Remote is remote addr in server side, + // For client requests, it is unused. + Remote string `json:"Remote"` + + // URL is Request.URL + // For client requests, is request addr. contains schema://ip:port/path/xx + // For server requests, is only path. eg: /api/v1/xxx + URL string `json:"URL"` Method string `json:"Method"` Header map[string]string `json:"Header"` - URL string `json:"URL"` Body any `json:"Body"` } `json:"Request"` Response struct { + + // URL is server addr(http://127.0.0.1:8080). + // For client requests, it is unused. + URL string `json:"URL"` Header map[string]string `json:"Header"` Body any `json:"Body"` StatusCode int `json:"StatusCode"` @@ -33,8 +46,6 @@ func (stat *Stat) String() string { return string(b) } -const dateTime = "2006-01-02 15:04:05.000" - // StatLoad stat. func StatLoad(resp *Response) *Stat { stat := &Stat{