From 6ea48b2551a199f1cb222fbc4b54ff19075b8215 Mon Sep 17 00:00:00 2001 From: yuan_rh <545873205@qq.com> Date: Tue, 30 Mar 2021 15:10:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9B=E5=A2=9E=E5=8A=A0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=80=9A=E7=9F=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 ++ handle/handle.go | 32 ++++++++++++++++++++++---------- main.go | 2 +- websocket/websocket.go | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 78d62e4..de9c4dd 100644 --- a/config/config.go +++ b/config/config.go @@ -24,6 +24,8 @@ var ServerIpfsUrl = "10.240.10.238:5001" var UpdaterName ="LOCKING更新.exe" var EtcdUrl="127.0.0.1:2379" +var ServerUrl = "http://www.lockingos.org:9003" + var GobalWatch *fsnotify.Watcher //全局的chanel map diff --git a/handle/handle.go b/handle/handle.go index 8e442fa..d0a641f 100644 --- a/handle/handle.go +++ b/handle/handle.go @@ -43,6 +43,9 @@ var gobalLocalProjectDir string var gobalSubscriptionFileChangeSwitch int =0 //订阅文件变更开关 +//全局消息通知管道 +var GobalMessageNotify = make(chan string,1000) + var ipfsPath=os.Getenv("IPFS-PATH") @@ -615,7 +618,7 @@ type FileParam struct { //获取文件的历史版本管理文件最新hash值 func postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath string)(string,error){ - url:="http://www.lockingos.org:9003/api/pms/sdk/queryFileHistoryCurrentCid" + url:=config.ServerUrl+"/api/pms/sdk/queryFileHistoryCurrentCid" contentType := "application/json" fileParam :=FileParam{ @@ -674,7 +677,7 @@ func postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath s //更新文件记录 func postUpdateFile(projectName, folderName, relativePath, ipfsCid, fileName, historyCurrentIpfsCid, historyPreIpfsCid,userId string, fileSize int64, fileVersion int)(err error){ - url:="http://www.lockingos.org:9003/api/pms/sdk/updateFile" + url:=config.ServerUrl+"/api/pms/sdk/updateFile" contentType := "application/json" intUserId,_:=strconv.ParseInt(userId,10,64) @@ -1041,18 +1044,24 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ for actionAndModifyFilePathStr :=range config.GobalWatchChannelMap[gobalLocalProjectDir] { //log.Println(actionAndModifyFilePathStr) actionAndModifyFilePath := strings.Split(actionAndModifyFilePathStr,";") + queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1) + + //当前登陆用户判断 + if gobalLoginUserName != strings.Split(queryKey,"\\")[0]{ + log.Printf("非法用户修改%v", actionAndModifyFilePathStr) + continue + } + if actionAndModifyFilePath[0]=="remove"{ - k := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1) - queryMap,err :=etcdclient.QueryWithPrefix(k) + queryMap,err :=etcdclient.QueryWithPrefix(queryKey) if len(queryMap)==0{ continue } - err = etcdclient.DeleteWithPrefix(k) + err = etcdclient.DeleteWithPrefix(queryKey) if err != nil { log.Println(err) } }else if actionAndModifyFilePath[0]=="write"{ - queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1) querymap,err := etcdclient.QueryWithPrefix(queryKey) if err != nil { log.Println(err) @@ -1072,9 +1081,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ log.Printf("文件变更 [ %v ] write", actionAndModifyFilePathStr) //如果非新增文件则自动post - //if goabalAddFileMap[] filePath := actionAndModifyFilePath[1] - //TODO //获取文件的历史版本管理文件hash fileName :=filepath.Base(filePath) @@ -1088,6 +1095,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ } historyCurrentHash,err :=postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath) if err!=nil{ + GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName) log.Printf("postGetHistoryCurrentHash 返回失败,%v",err.Error()) continue } @@ -1095,6 +1103,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ //自动更新文件 err =UploadCommand(nil,filePath,fileName,projectName,dir,historyCurrentHash,"",gobalLoginUserName,false) if err!=nil{ + GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName) log.Printf("UploadCommand 返回失败,%v",err.Error()) //记录修改状态 newValue := oldValue[0]+";" +"1" @@ -1105,9 +1114,9 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ } continue } + GobalMessageNotify <- fmt.Sprintf("文件更新成功\n%v",fileName) }else if actionAndModifyFilePath[0]=="create"{ - queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1) querymap,err := etcdclient.QueryWithPrefix(queryKey) if err != nil { log.Println(err) @@ -1129,7 +1138,6 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ //如果非新增文件则自动post //if goabalAddFileMap[] filePath := actionAndModifyFilePath[1] - //TODO //获取文件的历史版本管理文件hash fileName :=filepath.Base(filePath) @@ -1143,6 +1151,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ } historyCurrentHash,err :=postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath) if err!=nil{ + GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName) log.Printf("postGetHistoryCurrentHash 返回失败,%v",err.Error()) continue } @@ -1150,6 +1159,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ //自动更新文件 err =UploadCommand(nil,filePath,fileName,projectName,dir,historyCurrentHash,"",gobalLoginUserName,false) if err!=nil{ + GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName) log.Printf("UploadCommand 返回失败,%v",err.Error()) //记录修改状态 newValue := oldValue[0]+";" +"1" @@ -1160,6 +1170,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ } continue } + GobalMessageNotify <- fmt.Sprintf("文件更新成功\n%v",fileName) } err = sendFileListFromEtcd(keyPrefix,projectName,conn) if err != nil { @@ -1171,6 +1182,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{ return nil } + func sendFileListFromEtcd(keyPrefix,projectName string,conn *websocket.Conn) error{ dataMap,err := etcdclient.QueryWithPrefix(keyPrefix) if err != nil { diff --git a/main.go b/main.go index a3dfb7e..638f572 100644 --- a/main.go +++ b/main.go @@ -180,7 +180,7 @@ func main() { http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler) //http.HandleFunc("/watchFile", websocket.WatchFileHandler) http.HandleFunc("/keeplive", websocket.KeepliveHandler) - + http.HandleFunc("/messageNotify", websocket.MessageNotifyHandler) http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler) http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler) diff --git a/websocket/websocket.go b/websocket/websocket.go index d428b3a..f482183 100644 --- a/websocket/websocket.go +++ b/websocket/websocket.go @@ -123,8 +123,8 @@ func InitLocalWorkSpaceHandler(w http.ResponseWriter, r *http.Request) { //log.Println("param initLocalWorkSpaceHandler:"+string(data)) params :=strings.Split(string(data),"|") - //TODO userId - err := handle.InitLocalWorkSpace(conn,params[0],"367294106252087297",params[1]) + //userId = "367294106252087297" + err := handle.InitLocalWorkSpace(conn,params[0],params[1],params[2]) if err!=nil{ log.Println(err) goto ERR @@ -423,6 +423,34 @@ ERR: } +//消息通知 +func MessageNotifyHandler(w http.ResponseWriter, r *http.Request){ + //w.Write([]byte("hello")) + //收到http请求(upgrade),完成websocket协议转换 + //在应答的header中放上upgrade:websoket + var ( + conn *websocket.Conn + err error + //msgType int + //data []byte + ) + if conn, err = upgrader.Upgrade(w, r, nil); err !=nil { + //报错了,直接返回底层的websocket链接就会终断掉 + return + } + + //读取通道消息 + for msg := range handle.GobalMessageNotify { + if err := conn.WriteMessage(websocket.TextMessage, []byte(msg)); err != nil { + log.Println(err) + goto ERR + } + } + //error的标签 +ERR: + conn.Close() +} + //查询历史版本记录 func QueryCommitHistoryHandler(w http.ResponseWriter, r *http.Request) { //w.Write([]byte("hello"))