Compare commits

..

3 Commits

Author SHA1 Message Date
giaogiao aba4040c6e 格式 2023-08-07 12:00:34 +08:00
giaogiao 30027df01c 增加示例 2023-08-07 11:58:53 +08:00
giaogiao 4224431625 完成第二版本 批量接受 2023-08-07 11:55:17 +08:00
5 changed files with 125 additions and 75 deletions

43
README.md Normal file
View File

@ -0,0 +1,43 @@
## API URL
```
http://127.0.0.1:1323/textserver
```
## RequestData
```
{
"Urls": [{
"url": "https://www.github.com/"
}, {
"url": "http://127.0.0.1"
}, {
"url": "https://www.github.com/"
}]
}
```
## ReturnData
```
{
"code": 200,
"message": "",
"data": {
"items": [
{
"code": 200,
"time": 36,
"url": "http://127.0.0.1"
},
{
"code": 200,
"time": 991,
"url": "https://www.github.com/"
},
{
"code": 200,
"time": 994,
"url": "https://www.github.com/"
}
]
}
}
```

5
go.mod
View File

@ -4,12 +4,11 @@ go 1.16
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gorilla/websocket v1.5.0
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/idoubi/goutils v1.3.2 // indirect
github.com/idoubi/goz v1.4.4 // indirect
github.com/idoubi/goz v1.4.4
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo/v4 v4.10.2 // indirect
github.com/labstack/echo/v4 v4.10.2
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
golang.org/x/net v0.8.0 // indirect

2
go.sum
View File

@ -13,8 +13,6 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk=

109
main.go
View File

@ -1,49 +1,98 @@
/*
* @Author: giaogiao giaogiao
* @Date: 2023-08-07 09:56:42
* @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-08-07 11:54:17
* @FilePath: \go_echo\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package main
import (
"fmt"
"net/http"
"strconv"
"sync"
"time"
"github.com/idoubi/goz"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/net/websocket"
)
func ws(c echo.Context) error {
websocket.Handler(server).ServeHTTP(c.Response(), c.Request())
return nil
type TheServerReturns struct {
Code int `json:"code"`
Message string `json:"message"`
Data Data `json:"data"`
}
func add(c echo.Context) error {
tip_number++
return c.String(http.StatusOK, strconv.Itoa(tip_number))
type Items struct {
Code int `json:"code"`
Time int `json:"time"`
URL string `json:"url"`
}
type Data struct {
Items []Items `json:"items"`
}
func server(ws *websocket.Conn) {
defer ws.Close()
fmt.Printf("new connection\n")
for {
if tip_number == 0 {
continue
type ServerAccept struct {
Urls []Urls `json:"urls"`
}
var strr string = strconv.Itoa(tip_number)
err := websocket.Message.Send(ws, strr)
if err != nil {
fmt.Print(err)
type Urls struct {
URL string `json:"url"`
}
tip_number = 0
}
}
var tip_number int = 0
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", "./public")
e.GET("/ws", ws)
e.GET("/add", add)
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.POST("/textserver", textserver)
e.Logger.Fatal(e.Start(":1323"))
}
func textserver(c echo.Context) error {
// 初始化接收json参数的对象
u := new(ServerAccept)
// 通过Bind将json参数绑定到struct对象
if err := c.Bind(u); err != nil {
return err
}
// 通过结构体对象就可以访问json参数
json := new(TheServerReturns)
json.Code = http.StatusOK
var wg sync.WaitGroup
wg.Add(len(u.Urls))
for _, url := range u.Urls {
item := new(Items)
go text_url2(json, url.URL, item, wg.Done)
}
wg.Wait()
return c.JSON(http.StatusOK, json)
}
func text_url2(json *TheServerReturns, url string, item *Items, done func()) int {
defer done()
// item := new(Items)
item.URL = url
start := time.Now().UnixMilli()
cli := goz.NewClient(goz.Options{
Timeout: 2,
})
resp, err := cli.Get(url)
if err != nil {
if resp.IsTimeout() {
item.Code = -1
item.Time = -1
return 0
}
}
elapsed := time.Now().UnixMilli() - start
item.Code = resp.GetStatusCode()
item.Time = int(elapsed)
json.Data.Items = append(json.Data.Items, *item)
return 0
}

View File

@ -1,39 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebSocket</title>
</head>
<body>
<p id="output"></p>
<script>
var loc = window.location;
var uri = 'ws:';
if (loc.protocol === 'https:') {
uri = 'wss:';
}
uri += '//' + loc.host;
uri += loc.pathname + 'ws';
ws = new WebSocket(uri)
ws.onopen = function() {
console.log('Connected')
}
ws.onmessage = function(evt) {
var out = document.getElementById('output');
out.innerHTML += evt.data + '<br>';
}
// setInterval(function() {
// ws.send('Hello, Server!');
// }, 1000);
</script>
</body>
</html>