完成第二版本 批量接受

This commit is contained in:
giaogiao 2023-08-07 11:55:17 +08:00
parent 87c52f79a4
commit 4224431625
2 changed files with 54 additions and 22 deletions

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 // 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

72
main.go
View File

@ -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 {
Code int `json:"code"`
Time int `json:"time"`
URL string `json:"url"`
}
type Data struct {
Items []Items `json:"items"`
} }
type result struct { type ServerAccept struct {
// 通过json标签指定json字段名 Urls []Urls `json:"urls"`
Sataus int `json:"sataus"` }
Code int `json:"code"` type Urls struct {
Time int `json:"time"` URL string `json:"url"`
} }
func main() { func main() {
@ -35,21 +53,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 +86,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
} }