Compare commits
No commits in common. "aba4040c6e8ee48b7473e717430dfc8e11bca14c" and "87c52f79a4c041675056ba4290119e82b1ae327b" have entirely different histories.
aba4040c6e
...
87c52f79a4
43
README.md
43
README.md
|
@ -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
4
go.mod
|
@ -6,9 +6,9 @@ require (
|
|||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/howeyc/fsnotify v0.9.0 // 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/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/fresh v0.0.0-20190826141211-0fa698148017 // indirect
|
||||
golang.org/x/net v0.8.0 // indirect
|
||||
|
|
73
main.go
73
main.go
|
@ -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
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/idoubi/goz"
|
||||
|
@ -18,25 +9,16 @@ import (
|
|||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
type TheServerReturns struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
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 server struct {
|
||||
// 通过json标签指定json字段名
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type ServerAccept struct {
|
||||
Urls []Urls `json:"urls"`
|
||||
}
|
||||
type Urls struct {
|
||||
URL string `json:"url"`
|
||||
type result struct {
|
||||
// 通过json标签指定json字段名
|
||||
Sataus int `json:"sataus"`
|
||||
Code int `json:"code"`
|
||||
Time int `json:"time"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -45,6 +27,7 @@ func main() {
|
|||
e.GET("/", func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!")
|
||||
})
|
||||
|
||||
e.POST("/textserver", textserver)
|
||||
e.Logger.Fatal(e.Start(":1323"))
|
||||
}
|
||||
|
@ -52,32 +35,21 @@ func main() {
|
|||
func textserver(c echo.Context) error {
|
||||
|
||||
// 初始化接收json参数的对象
|
||||
u := new(ServerAccept)
|
||||
u := new(server)
|
||||
// 通过Bind将json参数绑定到struct对象
|
||||
if err := c.Bind(u); err != nil {
|
||||
return err
|
||||
}
|
||||
// 通过结构体对象就可以访问json参数
|
||||
json := new(TheServerReturns)
|
||||
json := new(result)
|
||||
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()
|
||||
start := time.Now().UnixMilli()
|
||||
json.Sataus = text_url(u.Url)
|
||||
elapsed := time.Now().UnixMilli() - start
|
||||
json.Time = int(elapsed)
|
||||
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()
|
||||
func text_url(url string) int {
|
||||
cli := goz.NewClient(goz.Options{
|
||||
Timeout: 2,
|
||||
})
|
||||
|
@ -85,14 +57,11 @@ func text_url2(json *TheServerReturns, url string, item *Items, done func()) int
|
|||
resp, err := cli.Get(url)
|
||||
if err != nil {
|
||||
if resp.IsTimeout() {
|
||||
item.Code = -1
|
||||
item.Time = -1
|
||||
return 0
|
||||
return -1
|
||||
}
|
||||
}
|
||||
elapsed := time.Now().UnixMilli() - start
|
||||
item.Code = resp.GetStatusCode()
|
||||
item.Time = int(elapsed)
|
||||
json.Data.Items = append(json.Data.Items, *item)
|
||||
return 0
|
||||
return resp.GetStatusCode()
|
||||
}
|
||||
func setAccessOriginUrl(c echo.Context) {
|
||||
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue