Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
lingling | 20cc0d355f | |
lingling | 02b57be779 |
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/"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
5
go.mod
5
go.mod
|
@ -4,11 +4,12 @@ go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
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/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
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -13,6 +13,8 @@ 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/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/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/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 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
|
||||||
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
|
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
|
||||||
github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk=
|
github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk=
|
||||||
|
|
109
main.go
109
main.go
|
@ -1,98 +1,49 @@
|
||||||
/*
|
|
||||||
* @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 (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/idoubi/goz"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TheServerReturns struct {
|
func ws(c echo.Context) error {
|
||||||
Code int `json:"code"`
|
websocket.Handler(server).ServeHTTP(c.Response(), c.Request())
|
||||||
Message string `json:"message"`
|
return nil
|
||||||
Data Data `json:"data"`
|
|
||||||
}
|
}
|
||||||
type Items struct {
|
func add(c echo.Context) error {
|
||||||
Code int `json:"code"`
|
tip_number++
|
||||||
Time int `json:"time"`
|
return c.String(http.StatusOK, strconv.Itoa(tip_number))
|
||||||
URL string `json:"url"`
|
|
||||||
}
|
|
||||||
type Data struct {
|
|
||||||
Items []Items `json:"items"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAccept struct {
|
func server(ws *websocket.Conn) {
|
||||||
Urls []Urls `json:"urls"`
|
defer ws.Close()
|
||||||
|
fmt.Printf("new connection\n")
|
||||||
|
for {
|
||||||
|
if tip_number == 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
type Urls struct {
|
var strr string = strconv.Itoa(tip_number)
|
||||||
URL string `json:"url"`
|
err := websocket.Message.Send(ws, strr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Print(err)
|
||||||
}
|
}
|
||||||
|
tip_number = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var tip_number int = 0
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}}))
|
e.Use(middleware.Logger())
|
||||||
e.GET("/", func(c echo.Context) error {
|
e.Use(middleware.Recover())
|
||||||
return c.String(http.StatusOK, "Hello, World!")
|
e.Static("/", "./public")
|
||||||
})
|
e.GET("/ws", ws)
|
||||||
e.POST("/textserver", textserver)
|
e.GET("/add", add)
|
||||||
e.Logger.Fatal(e.Start(":1323"))
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<!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>
|
Loading…
Reference in New Issue