submodule+revert /fetchlog back to REST
This commit is contained in:
parent
f54eed47e9
commit
22eea482b9
5 changed files with 133 additions and 128 deletions
4
.gitmodules
vendored
4
.gitmodules
vendored
|
|
@ -0,0 +1,4 @@
|
||||||
|
[submodule "server/cofffeemachineConfig"]
|
||||||
|
path = server/cofffeemachineConfig
|
||||||
|
url = ssh://ikong@192.168.10.159/1TBHDD/ikong/repo/cofffeemachineConfig
|
||||||
|
branch = master
|
||||||
|
|
@ -19,7 +19,7 @@ export class ChangelogComponent {
|
||||||
showLogModal: boolean = false;
|
showLogModal: boolean = false;
|
||||||
showMergeModal: boolean = false;
|
showMergeModal: boolean = false;
|
||||||
|
|
||||||
constructor(private httpClient: HttpClient, private wss: WebsocketService) {
|
constructor(private httpClient: HttpClient) {
|
||||||
this.fetchLoglist();
|
this.fetchLoglist();
|
||||||
// this.translateLogDirToString();
|
// this.translateLogDirToString();
|
||||||
}
|
}
|
||||||
|
|
@ -29,50 +29,50 @@ export class ChangelogComponent {
|
||||||
// fetch("/changelog")
|
// fetch("/changelog")
|
||||||
let dirlist: any[] = [];
|
let dirlist: any[] = [];
|
||||||
|
|
||||||
this.wss.connect(environment.wsapi + '/listFileInDir');
|
// this.wss.connect(environment.wsapi + '/listFileInDir');
|
||||||
this.wss.send({
|
// this.wss.send({
|
||||||
topic: 'changelog',
|
// topic: 'changelog',
|
||||||
});
|
// });
|
||||||
|
|
||||||
let response = this.wss.data;
|
// let response = this.wss.data;
|
||||||
console.log(response);
|
// console.log(response);
|
||||||
|
|
||||||
// this.httpClient.get(environment.api+"/listFileInDir/changelog", {
|
this.httpClient.get(environment.api+"/listFileInDir/changelog", {
|
||||||
// withCredentials: true
|
withCredentials: true
|
||||||
// }).subscribe({
|
}).subscribe({
|
||||||
// next: (value) => {
|
next: (value) => {
|
||||||
// console.log(value)
|
console.log(value)
|
||||||
// if(typeof value === "object" && value !== null){
|
if(typeof value === "object" && value !== null){
|
||||||
// if("dirs" in value){
|
if("dirs" in value){
|
||||||
// console.log("dirs", value)
|
console.log("dirs", value)
|
||||||
// // dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
|
// dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
|
||||||
// this.displayableLogs = value.dirs as any[];
|
this.displayableLogs = value.dirs as any[];
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// error: (err) => {
|
error: (err) => {
|
||||||
// console.error('Error fetch json: ',err);
|
console.error('Error fetch json: ',err);
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
// // console.log(dirlist);
|
// console.log(dirlist);
|
||||||
|
|
||||||
// this.httpClient.get(environment.api+"/listFileInDir/merge", {
|
this.httpClient.get(environment.api+"/listFileInDir/merge", {
|
||||||
// withCredentials: true
|
withCredentials: true
|
||||||
// }).subscribe({
|
}).subscribe({
|
||||||
// next: (value) => {
|
next: (value) => {
|
||||||
// console.log(value)
|
console.log(value)
|
||||||
// if(typeof value === "object" && value !== null){
|
if(typeof value === "object" && value !== null){
|
||||||
// if("dirs" in value){
|
if("dirs" in value){
|
||||||
// console.log("dirs", value)
|
console.log("dirs", value)
|
||||||
// // dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
|
// dirlist.push(...(value as {dirs: unknown[]})["dirs"]);
|
||||||
// this.displayableMergeJson = value.dirs as any[];
|
this.displayableMergeJson = value.dirs as any[];
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// error: (err) => {
|
error: (err) => {
|
||||||
// console.error('Error fetch json: ',err);
|
console.error('Error fetch json: ',err);
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
|
|
||||||
return dirlist;
|
return dirlist;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,9 +38,9 @@ export class WebsocketService {
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public data = this.subject.pipe(
|
// public data = this.subject.pipe(
|
||||||
share()
|
// share()
|
||||||
);
|
// );
|
||||||
|
|
||||||
public send(url: any){
|
public send(url: any){
|
||||||
this.subject.next(url);
|
this.subject.next(url);
|
||||||
|
|
|
||||||
1
server/cofffeemachineConfig
Submodule
1
server/cofffeemachineConfig
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit bb7e2e1d3ce239ac877ed0ecdad934eef4f2513d
|
||||||
166
server/server.go
166
server/server.go
|
|
@ -342,98 +342,98 @@ func (s *Server) createHandler() {
|
||||||
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||||
// })
|
// })
|
||||||
|
|
||||||
r.Get("/listFileInDir", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// socket
|
// // socket
|
||||||
socket, err := upgrader.Upgrade(w, r, nil)
|
// socket, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
Log.Error("Error while trying to upgrade socket: ", zap.Error(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Change to websocket for better performance
|
|
||||||
for {
|
|
||||||
msg_type, msg, err := socket.ReadMessage()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
Log.Warn("Idle ", zap.Any("status", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var msgPayload map[string]interface{}
|
|
||||||
|
|
||||||
err = json.Unmarshal(msg, &msgPayload)
|
|
||||||
if err != nil {
|
|
||||||
Log.Error("Error while trying to unmarshal message: ", zap.Error(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// topic := msgPayload["topic"].(string)
|
|
||||||
|
|
||||||
Log.Info("Receive message: ", zap.Any("msg_type", msg_type), zap.Any("msg", msgPayload))
|
|
||||||
|
|
||||||
response, err := json.Marshal(map[string]interface{}{
|
|
||||||
"message": "ok",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
Log.Error("Error at marshalling response: ", zap.Error(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// respons to client
|
|
||||||
socket.WriteMessage(msg_type, response)
|
|
||||||
}
|
|
||||||
|
|
||||||
// spl
|
|
||||||
// spl_path := strings.Split(r.RequestURI, "/")
|
|
||||||
// if len(spl_path) > 3 {
|
|
||||||
// Log.Warn("Unexpected depth: ",
|
|
||||||
// zap.String("path", r.RequestURI),
|
|
||||||
// zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if spl_path[2] == "" {
|
|
||||||
// Log.Error("Empty target dir", zap.String("path", r.RequestURI))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
|
||||||
|
|
||||||
// // Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig"))
|
|
||||||
|
|
||||||
// main_folder := "cofffeemachineConfig"
|
|
||||||
// target_path := main_folder + "/" + spl_path[2]
|
|
||||||
|
|
||||||
// dir, err := os.ReadDir(target_path)
|
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err))
|
// Log.Error("Error while trying to upgrade socket: ", zap.Error(err))
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // File ext
|
// // TODO: Change to websocket for better performance
|
||||||
// file_ext := ".json"
|
// for {
|
||||||
// switch spl_path[2] {
|
// msg_type, msg, err := socket.ReadMessage()
|
||||||
// case "changelog":
|
|
||||||
// file_ext = ".html"
|
|
||||||
// break
|
|
||||||
// case "merge":
|
|
||||||
// file_ext = ".json"
|
|
||||||
// break
|
|
||||||
// }
|
|
||||||
// Log.Debug("Set file ext = ", zap.String("file_ext", file_ext))
|
|
||||||
|
|
||||||
// displayable := make([]string, 0)
|
// if err != nil {
|
||||||
// for _, file := range dir {
|
// Log.Warn("Idle ", zap.Any("status", err))
|
||||||
// if strings.Contains(file.Name(), file_ext) {
|
// return
|
||||||
// Log.Debug("Found file: ", zap.String("file", file.Name()))
|
|
||||||
// displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// var msgPayload map[string]interface{}
|
||||||
|
|
||||||
|
// err = json.Unmarshal(msg, &msgPayload)
|
||||||
|
// if err != nil {
|
||||||
|
// Log.Error("Error while trying to unmarshal message: ", zap.Error(err))
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // topic := msgPayload["topic"].(string)
|
||||||
|
|
||||||
|
// Log.Info("Receive message: ", zap.Any("msg_type", msg_type), zap.Any("msg", msgPayload))
|
||||||
|
|
||||||
|
// response, err := json.Marshal(map[string]interface{}{
|
||||||
|
// "message": "ok",
|
||||||
|
// })
|
||||||
|
// if err != nil {
|
||||||
|
// Log.Error("Error at marshalling response: ", zap.Error(err))
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// // respons to client
|
||||||
|
// socket.WriteMessage(msg_type, response)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // send back
|
//spl
|
||||||
// w.Header().Set("Content-Type", "application/json")
|
spl_path := strings.Split(r.RequestURI, "/")
|
||||||
// w.WriteHeader(http.StatusOK)
|
if len(spl_path) > 3 {
|
||||||
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
Log.Warn("Unexpected depth: ",
|
||||||
// Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
|
zap.String("path", r.RequestURI),
|
||||||
|
zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
|
||||||
|
}
|
||||||
|
|
||||||
|
if spl_path[2] == "" {
|
||||||
|
Log.Error("Empty target dir", zap.String("path", r.RequestURI))
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
||||||
|
|
||||||
|
// Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig"))
|
||||||
|
|
||||||
|
main_folder := "cofffeemachineConfig"
|
||||||
|
target_path := main_folder + "/" + spl_path[2]
|
||||||
|
|
||||||
|
dir, err := os.ReadDir(target_path)
|
||||||
|
if err != nil {
|
||||||
|
Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err))
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// File ext
|
||||||
|
file_ext := ".json"
|
||||||
|
switch spl_path[2] {
|
||||||
|
case "changelog":
|
||||||
|
file_ext = ".html"
|
||||||
|
break
|
||||||
|
case "merge":
|
||||||
|
file_ext = ".json"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
Log.Debug("Set file ext = ", zap.String("file_ext", file_ext))
|
||||||
|
|
||||||
|
displayable := make([]string, 0)
|
||||||
|
for _, file := range dir {
|
||||||
|
if strings.Contains(file.Name(), file_ext) {
|
||||||
|
Log.Debug("Found file: ", zap.String("file", file.Name()))
|
||||||
|
displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send back
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||||
|
Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Recipe Router
|
// Recipe Router
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue