Compare commits

..

No commits in common. "aba4040c6e8ee48b7473e717430dfc8e11bca14c" and "87c52f79a4c041675056ba4290119e82b1ae327b" have entirely different histories.

3 changed files with 23 additions and 97 deletions

View File

@ -1,43 +0,0 @@
## 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/"
}
]
}
}
```

4
go.mod
View File

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

73
main.go
View File

@ -1,16 +1,7 @@
/*
* @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 package main
import ( import (
"net/http" "net/http"
"sync"
"time" "time"
"github.com/idoubi/goz" "github.com/idoubi/goz"
@ -18,25 +9,16 @@ import (
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
) )
type TheServerReturns struct { type server struct {
Code int `json:"code"` // 通过json标签指定json字段名
Message string `json:"message"` Url string `json:"url"`
Data Data `json:"data"`
}
type Items struct {
Code int `json:"code"`
Time int `json:"time"`
URL string `json:"url"`
}
type Data struct {
Items []Items `json:"items"`
} }
type ServerAccept struct { type result struct {
Urls []Urls `json:"urls"` // 通过json标签指定json字段名
} Sataus int `json:"sataus"`
type Urls struct { Code int `json:"code"`
URL string `json:"url"` Time int `json:"time"`
} }
func main() { func main() {
@ -45,6 +27,7 @@ func main() {
e.GET("/", func(c echo.Context) error { e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!") return c.String(http.StatusOK, "Hello, World!")
}) })
e.POST("/textserver", textserver) e.POST("/textserver", textserver)
e.Logger.Fatal(e.Start(":1323")) e.Logger.Fatal(e.Start(":1323"))
} }
@ -52,32 +35,21 @@ func main() {
func textserver(c echo.Context) error { func textserver(c echo.Context) error {
// 初始化接收json参数的对象 // 初始化接收json参数的对象
u := new(ServerAccept) u := new(server)
// 通过Bind将json参数绑定到struct对象 // 通过Bind将json参数绑定到struct对象
if err := c.Bind(u); err != nil { if err := c.Bind(u); err != nil {
return err return err
} }
// 通过结构体对象就可以访问json参数 // 通过结构体对象就可以访问json参数
json := new(TheServerReturns) json := new(result)
json.Code = http.StatusOK json.Code = http.StatusOK
var wg sync.WaitGroup start := time.Now().UnixMilli()
wg.Add(len(u.Urls)) json.Sataus = text_url(u.Url)
elapsed := time.Now().UnixMilli() - start
for _, url := range u.Urls { json.Time = int(elapsed)
item := new(Items)
go text_url2(json, url.URL, item, wg.Done)
}
wg.Wait()
return c.JSON(http.StatusOK, json) return c.JSON(http.StatusOK, json)
} }
func text_url(url string) int {
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{ cli := goz.NewClient(goz.Options{
Timeout: 2, Timeout: 2,
}) })
@ -85,14 +57,11 @@ func text_url2(json *TheServerReturns, url string, item *Items, done func()) int
resp, err := cli.Get(url) resp, err := cli.Get(url)
if err != nil { if err != nil {
if resp.IsTimeout() { if resp.IsTimeout() {
item.Code = -1 return -1
item.Time = -1
return 0
} }
} }
elapsed := time.Now().UnixMilli() - start return resp.GetStatusCode()
item.Code = resp.GetStatusCode() }
item.Time = int(elapsed) func setAccessOriginUrl(c echo.Context) {
json.Data.Items = append(json.Data.Items, *item) c.Response().Header().Set("Access-Control-Allow-Origin", "*")
return 0
} }