添加一个websocket长连接

master
zcy 2021-08-16 00:19:25 +08:00
parent c7668e7859
commit 9614b34bd3
5 changed files with 47 additions and 5 deletions

View File

@ -34,6 +34,13 @@ mongo:
db: test db: test
max_open: 100 max_open: 100
MaxIdle: 99 MaxIdle: 99
ws_server_conf:
type: gateway
id: 1
ipadress: :9850
zkserver:
- "49.235.25.67
ca_cert: "" ca_cert: ""
client_cert: "" client_cert: ""
client_key: "" client_key: ""

View File

@ -5,6 +5,8 @@ import (
"os" "os"
"runtime" "runtime"
wsConf "tcptemplate/config"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -23,6 +25,7 @@ type ConfAPI struct {
ElasticSerach ElasticSearch `yaml:"elasticsearch"` ElasticSerach ElasticSearch `yaml:"elasticsearch"`
Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置 Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
MongoConf MongoConfig `yaml:"mongo"` MongoConf MongoConfig `yaml:"mongo"`
WsServerConf wsConf.Config `yaml:"ws_server_conf"`
CaCert string `yaml:"ca_cert"` CaCert string `yaml:"ca_cert"`
ClientCert string `yaml:"client_cert"` ClientCert string `yaml:"client_cert"`
ClientKey string `yaml:"client_key"'` ClientKey string `yaml:"client_key"'`
@ -83,6 +86,7 @@ func Init(path string) error {
} }
stat, _ := file.Stat() stat, _ := file.Stat()
filec := make([]byte, stat.Size()) filec := make([]byte, stat.Size())
log.Print(filec)
file.Read(filec) file.Read(filec)
e = yaml.Unmarshal(filec, &gConf) e = yaml.Unmarshal(filec, &gConf)
if nil != e { if nil != e {

18
main.go
View File

@ -11,6 +11,8 @@ import (
"log" "log"
"strconv" "strconv"
"tcptemplate/network"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
"github.com/swaggo/gin-swagger/swaggerFiles" "github.com/swaggo/gin-swagger/swaggerFiles"
@ -34,9 +36,8 @@ func CORSMiddleware(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
if config.ApiConfig().RunMode == "release" { if config.ApiConfig().RunMode == "release" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club") c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club")
} else { } else {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1") c.Writer.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:8080")
} }
c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Headers", c.Writer.Header().Set("Access-Control-Allow-Headers",
@ -101,7 +102,18 @@ func main() {
store := sessions.NewCookieStore([]byte("secret123")) store := sessions.NewCookieStore([]byte("secret123"))
r.Use(sessions.Middleware("sess_store", store)) r.Use(sessions.Middleware("sess_store", store))
r.Use(CORSMiddleware) // Cross domain r.Use(CORSMiddleware) // Cross domain
go func () { go func () {
server := network.ServerFactory("0.0.0.0",config.ApiConfig().WsServerConf)
server.SetHandler(nil)
e := server.Run()
if nil != e {
}
}()
go func() {
// programatically set swagger info // programatically set swagger info
r := gin.New() r := gin.New()
// use ginSwagger middleware to serve the API docs // use ginSwagger middleware to serve the API docs
@ -205,6 +217,8 @@ func main() {
openapi.POST("/ddl2markdown", openapiController.DDL2Markdown) // sql ddl转markdown 文档 openapi.POST("/ddl2markdown", openapiController.DDL2Markdown) // sql ddl转markdown 文档
openapi.POST("/ui2css", openapiController.UI2CSS) // qt ui文件转css文档 openapi.POST("/ui2css", openapiController.UI2CSS) // qt ui文件转css文档
} }
e := r.Run(":" + strconv.Itoa(config.GetPort())) e := r.Run(":" + strconv.Itoa(config.GetPort()))
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())

View File

@ -35,7 +35,6 @@ func MysqlToElasticSearchMapping(types string, Key string) string {
} }
return "" return ""
} }
/* /*
"settings":{ "settings":{
"number_of_shards":1, "number_of_shards":1,
@ -121,7 +120,6 @@ func CreateIndexFromMysqlTable(tblname string) error {
"properties": props, "properties": props,
}, },
} }
for _, v := range columns { for _, v := range columns {
props[v.Field] = map[string]string{"type": MysqlToElasticSearchMapping(v.Type, v.Key)} props[v.Field] = map[string]string{"type": MysqlToElasticSearchMapping(v.Type, v.Key)}
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author: your name * @Author: your name
* @Date: 2021-08-02 00:02:17 * @Date: 2021-08-02 00:02:17
* @LastEditTime: 2021-08-02 00:32:36 * @LastEditTime: 2021-08-15 23:15:46
* @LastEditors: Please set LastEditors * @LastEditors: Please set LastEditors
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: \background\test\goroutine_test.go * @FilePath: \background\test\goroutine_test.go
@ -68,6 +68,23 @@ func TestChanel(t *testing.T){
} }
func TestTimeoutContext(t * testing.T){
ctx,_ := context.WithTimeout(context.Background(), time.Second)
go func () {
for {
select {
case <-ctx.Done():
t.Logf("exit")
return
case <- time.After(2 * time.Second) :
t.Logf("time out ")
}
}
}()
time.Sleep(5 * time.Second)
}
func TestContext(t *testing.T){ func TestContext(t *testing.T){
ctx,cancel := context.WithCancel(context.Background()) ctx,cancel := context.WithCancel(context.Background())
@ -108,5 +125,7 @@ func TestContext(t *testing.T){
}() }()
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
cancel() cancel()
t.Logf("finish") t.Logf("finish")
} }