add display merged files. WIP json display
This commit is contained in:
parent
a5f4d48aad
commit
e7bb2639cd
7 changed files with 250 additions and 44 deletions
153
server/server.go
153
server/server.go
|
|
@ -78,8 +78,14 @@ func (s *Server) Run() error {
|
|||
// logger
|
||||
// defer log_inst.Sync()
|
||||
|
||||
if s.cfg.Debug {
|
||||
// logger.SetLevel("DEBUG")
|
||||
Log.Debug("Debug mode", zap.Bool("enable", s.cfg.Debug))
|
||||
logger.EnableDebug(s.cfg.Debug)
|
||||
}
|
||||
|
||||
s.createHandler()
|
||||
log.Printf("Server running on %s", s.server.Addr)
|
||||
// log.Printf("Server running on %s", s.server.Addr)
|
||||
Log.Info("Server running", zap.String("addr", s.server.Addr))
|
||||
return s.server.ListenAndServe()
|
||||
}
|
||||
|
|
@ -134,8 +140,11 @@ func (s *Server) createHandler() {
|
|||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), "user", user)
|
||||
|
||||
Log.Info("User is authenticated", zap.String("user", user.Name))
|
||||
if user == nil {
|
||||
Log.Error("User is not authenticated or timed out", zap.Any("requester", user))
|
||||
} else {
|
||||
Log.Info("User is authenticated", zap.String("user", user.Name))
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
|
|
@ -147,7 +156,7 @@ func (s *Server) createHandler() {
|
|||
err := json.NewDecoder(r.Body).Decode(&targetMap)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Fatalln("Merge request failed: ", err)
|
||||
Log.Fatal("Merge request failed", zap.Error(err))
|
||||
return
|
||||
}
|
||||
repo_path := "cofffeemachineConfig/coffeethai02_"
|
||||
|
|
@ -160,12 +169,12 @@ func (s *Server) createHandler() {
|
|||
// find target file in the cofffeemachineConfig
|
||||
if _, err := os.Stat(repo_path + master_version + ".json"); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Fatalln("Merge request failed. Master file not found: ", err)
|
||||
Log.Fatal("Merge request failed. Master file not found: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat(repo_path + dev_version + ".json"); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Fatalln("Merge request failed. Dev file not found: ", err)
|
||||
Log.Fatal("Merge request failed. Dev file not found: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -174,45 +183,58 @@ func (s *Server) createHandler() {
|
|||
|
||||
// Get who's requesting
|
||||
user := r.Context().Value("user").(*models.User)
|
||||
Log.Info("Request merge by", zap.String("user", user.Name))
|
||||
|
||||
// lookup for python exec
|
||||
py_exec, err := exec.LookPath("python")
|
||||
if err != nil {
|
||||
log.Fatalln("Python error: ", err)
|
||||
Log.Fatal("Python error: ", zap.Error(err))
|
||||
} else {
|
||||
py_exec, err = filepath.Abs(py_exec)
|
||||
}
|
||||
|
||||
log.Println("Found python exec: ", py_exec)
|
||||
Log.Info("Found python exec: ", zap.String("PythonPath", py_exec))
|
||||
// target api file
|
||||
merge_api, api_err := os.Open("./python_api/merge_recipe.py")
|
||||
if api_err != nil {
|
||||
log.Fatalln("Merge request failed. Python api error: ", api_err)
|
||||
Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error()))
|
||||
}
|
||||
defer merge_api.Close()
|
||||
log.Println("Locate python api", merge_api.Name())
|
||||
// log.Println("Locate python api", merge_api.Name())
|
||||
Log.Info("Locate python api", zap.String("ApiName", merge_api.Name()))
|
||||
cmd := exec.Command(py_exec, merge_api.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", user.Name)
|
||||
|
||||
log.Println("Run merge command", cmd)
|
||||
// log.Println("Run merge command", cmd)
|
||||
Log.Info("Merge", zap.String("master", master_path), zap.String("dev", dev_path), zap.String("output", output_path))
|
||||
Log.Debug("Run merge command", zap.String("Command", cmd.String()))
|
||||
out, err := cmd.CombinedOutput()
|
||||
log.Println(string(out))
|
||||
// log.Println(string(out))
|
||||
Log.Debug("Merge output", zap.String("Output", string(out)))
|
||||
if err != nil {
|
||||
log.Fatalln("Merge request failed. Python merge failed: ", err)
|
||||
// log.Fatalln("Merge request failed. Python merge failed: ", err)
|
||||
Log.Fatal("Merge request failed. Python merge failed", zap.Error(err))
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "Merge success"})
|
||||
|
||||
Log.Info("Merge success", zap.String("output", "merge success"))
|
||||
})
|
||||
|
||||
r.Post("/dllog", func(w http.ResponseWriter, r *http.Request) {
|
||||
Log.Debug("Request uri = ", zap.String("uri", r.RequestURI))
|
||||
|
||||
Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query")))
|
||||
// param
|
||||
param := r.URL.Query().Get("query")
|
||||
|
||||
var postRequest map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&postRequest)
|
||||
Log.Debug("Log request: ", zap.String("postRequest", fmt.Sprintf("%+v", postRequest)))
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Fatalln("Log request failed: ", err)
|
||||
Log.Fatal("Decode in request failed: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -228,11 +250,23 @@ func (s *Server) createHandler() {
|
|||
}
|
||||
|
||||
log_name := postRequest["filename"].(string)
|
||||
Log.Warn("Log file name: ", zap.String("filename", log_name))
|
||||
|
||||
if log_name == "" {
|
||||
Log.Fatal("Empty log file name")
|
||||
}
|
||||
|
||||
// log.Println("Log file ext: ", file_ext)
|
||||
changelog_path := "cofffeemachineConfig/changelog/" + log_name + file_ext
|
||||
default_changelog_path := "cofffeemachineConfig/" + param + "/"
|
||||
|
||||
changelog_path := default_changelog_path + log_name + file_ext
|
||||
if strings.Contains(log_name, default_changelog_path) && strings.Contains(log_name, ".json") {
|
||||
changelog_path = log_name
|
||||
}
|
||||
|
||||
logFile, err := os.Open(changelog_path)
|
||||
if err != nil {
|
||||
Log.Fatal("Log request failed: ", zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
|
|
@ -243,19 +277,20 @@ func (s *Server) createHandler() {
|
|||
var logFileJson map[string]interface{}
|
||||
err = json.NewDecoder(logFile).Decode(&logFileJson)
|
||||
if err != nil {
|
||||
log.Fatalf("Error when decode log file: %s", err)
|
||||
Log.Fatal("Error when decode log file: ", zap.Error(err))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(logFileJson)
|
||||
log.Println("Log file: ", changelog_path)
|
||||
Log.Info("Log file: ", zap.String("filename", log_name))
|
||||
} else {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=logfile"+file_ext)
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
|
||||
_, err = io.Copy(w, logFile)
|
||||
if err != nil {
|
||||
Log.Fatal("Could not send blob", zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
|
@ -266,6 +301,7 @@ 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)
|
||||
|
|
@ -281,6 +317,87 @@ func (s *Server) createHandler() {
|
|||
json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||
})
|
||||
|
||||
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// target_dir, err := os.ReadDir("cofffeemachineConfig")
|
||||
|
||||
// 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 {
|
||||
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))
|
||||
})
|
||||
|
||||
// dl merged json file
|
||||
// r.Get("/dlmerged", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// })
|
||||
|
||||
r.Get("/mergefileList", func(w http.ResponseWriter, r *http.Request) {
|
||||
merge_dir, err := os.ReadDir("cofffeemachineConfig/merge")
|
||||
if err != nil {
|
||||
Log.Error("Error while trying to read dir: ", zap.String("dir", "cofffeemachineConfig/merge"), zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
displayable := make([]string, 0)
|
||||
|
||||
for _, file := range merge_dir {
|
||||
if strings.Contains(file.Name(), ".json") {
|
||||
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})
|
||||
})
|
||||
|
||||
// Recipe Router
|
||||
rr := routers.NewRecipeRouter(database)
|
||||
rr.Route(r)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue