Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
giaogiao | aba4040c6e | |
giaogiao | 30027df01c | |
giaogiao | 4224431625 |
|
@ -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/"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
4
go.mod
4
go.mod
|
@ -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 // indirect
|
github.com/idoubi/goz v1.4.4
|
||||||
github.com/labstack/echo v3.3.10+incompatible
|
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/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
|
||||||
|
|
71
main.go
71
main.go
|
@ -1,7 +1,16 @@
|
||||||
|
/*
|
||||||
|
* @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"
|
||||||
|
@ -9,16 +18,25 @@ import (
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
type server struct {
|
type TheServerReturns struct {
|
||||||
// 通过json标签指定json字段名
|
Code int `json:"code"`
|
||||||
Url string `json:"url"`
|
Message string `json:"message"`
|
||||||
|
Data Data `json:"data"`
|
||||||
}
|
}
|
||||||
|
type Items struct {
|
||||||
type result struct {
|
|
||||||
// 通过json标签指定json字段名
|
|
||||||
Sataus int `json:"sataus"`
|
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Time int `json:"time"`
|
Time int `json:"time"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
type Data struct {
|
||||||
|
Items []Items `json:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerAccept struct {
|
||||||
|
Urls []Urls `json:"urls"`
|
||||||
|
}
|
||||||
|
type Urls struct {
|
||||||
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -27,7 +45,6 @@ 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"))
|
||||||
}
|
}
|
||||||
|
@ -35,21 +52,32 @@ func main() {
|
||||||
func textserver(c echo.Context) error {
|
func textserver(c echo.Context) error {
|
||||||
|
|
||||||
// 初始化接收json参数的对象
|
// 初始化接收json参数的对象
|
||||||
u := new(server)
|
u := new(ServerAccept)
|
||||||
// 通过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(result)
|
json := new(TheServerReturns)
|
||||||
json.Code = http.StatusOK
|
json.Code = http.StatusOK
|
||||||
start := time.Now().UnixMilli()
|
var wg sync.WaitGroup
|
||||||
json.Sataus = text_url(u.Url)
|
wg.Add(len(u.Urls))
|
||||||
elapsed := time.Now().UnixMilli() - start
|
|
||||||
json.Time = int(elapsed)
|
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)
|
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,
|
||||||
})
|
})
|
||||||
|
@ -57,11 +85,14 @@ func text_url(url string) int {
|
||||||
resp, err := cli.Get(url)
|
resp, err := cli.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resp.IsTimeout() {
|
if resp.IsTimeout() {
|
||||||
return -1
|
item.Code = -1
|
||||||
|
item.Time = -1
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resp.GetStatusCode()
|
elapsed := time.Now().UnixMilli() - start
|
||||||
}
|
item.Code = resp.GetStatusCode()
|
||||||
func setAccessOriginUrl(c echo.Context) {
|
item.Time = int(elapsed)
|
||||||
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
|
json.Data.Items = append(json.Data.Items, *item)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue