add edit json v1 WIP, testing

This commit is contained in:
pakintada@gmail.com 2023-10-20 14:32:16 +07:00
parent e5eee656d5
commit ea92145350
11 changed files with 216 additions and 67 deletions

View file

@ -324,67 +324,8 @@ func (s *Server) createHandler() {
})
// r.Get("/mergelogList", func(w http.ResponseWriter, r *http.Request) {
// ch_dir, err := os.ReadDir("cofffeemachineConfig/changelog")
// if err != nil {
// Log.Error("Error while trying to read dir: ", zap.String("dir", "cofffeemachineConfig/changelog"), zap.Error(err))
// http.Error(w, err.Error(), http.StatusInternalServerError)
// }
// displayable := make([]string, 0)
// for _, file := range ch_dir {
// if strings.Contains(file.Name(), ".html") {
// displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
// }
// }
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
// })
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) {
// // socket
// 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 {
@ -437,6 +378,51 @@ func (s *Server) createHandler() {
Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
})
r.Get("/get_log_relation", func(w http.ResponseWriter, r *http.Request) {
// Python looker
py_exec, err := exec.LookPath("python")
if err != nil {
Log.Error("Error while trying to find python: ", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
}
merge_timeline, api_err := os.Open("./python_api/merge_timeline.py")
if api_err != nil {
Log.Error("Error while trying to open merge_timeline.json: ", zap.Error(api_err))
http.Error(w, api_err.Error(), http.StatusInternalServerError)
}
defer merge_timeline.Close()
cmd := exec.Command(py_exec, merge_timeline.Name(), "get_relate")
Log.Debug("Command: ", zap.String("command", cmd.String()))
out, err := cmd.CombinedOutput()
Log.Debug("Output: ", zap.String("output", string(out)))
if err != nil {
Log.Error("Error while trying to run python: ", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// clean up output
clean1 := strings.ReplaceAll(string(out), "\n", "")
clean2 := strings.ReplaceAll(clean1, "\r", "")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"message": clean2,
})
})
r.Post("/diffpy/*", func(w http.ResponseWriter, r *http.Request) {
Log.Debug("Diffpy: ", zap.String("path", r.RequestURI))
// TODO: add command exec `python_Exec` `merge_recipe.py` `diff` `master_version` `version-version-version` `debug?` `flatten={true|false}` `out={true|false}`
})
sheetService, err := sheet.NewSheetService(context.Background(), s.cfg)
if err != nil {