move merge fn
This commit is contained in:
parent
baa5382c8b
commit
49017ab39a
4 changed files with 48 additions and 319 deletions
Binary file not shown.
|
|
@ -15,6 +15,7 @@ import (
|
|||
"recipe-manager/services/sheet"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
|
@ -29,6 +30,10 @@ type RecipeRouter struct {
|
|||
taoLogger *logger.TaoLogger
|
||||
}
|
||||
|
||||
var (
|
||||
binaryApiLock sync.Mutex
|
||||
)
|
||||
|
||||
func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger) *RecipeRouter {
|
||||
return &RecipeRouter{
|
||||
data,
|
||||
|
|
@ -72,6 +77,8 @@ func (rr *RecipeRouter) Route(r chi.Router) {
|
|||
}
|
||||
})
|
||||
|
||||
r.Post("/merge", rr.doMergeJson)
|
||||
|
||||
r.Get("/{country}/versions", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
|
|
@ -410,11 +417,6 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request) {
|
||||
// get saved files
|
||||
// r.Get(, func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Log.Debug("Saved Files: ", zap.Any("files", commits))
|
||||
// })
|
||||
|
||||
file_version := chi.URLParam(r, "filename_version_only")
|
||||
country := chi.URLParam(r, "country")
|
||||
|
|
@ -425,25 +427,6 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
|
|||
return
|
||||
}
|
||||
|
||||
// recipe_root_path := "./cofffeemachineConfig/"
|
||||
|
||||
// structure
|
||||
// full_file_name_targets := []string{}
|
||||
|
||||
// files, err := os.ReadDir(recipe_root_path + countryID)
|
||||
|
||||
// if err != nil {
|
||||
// Log.Error("Error when read directory", zap.Error(err))
|
||||
// return
|
||||
// }
|
||||
|
||||
// for _, file := range files {
|
||||
// Log.Debug("File: ", zap.Any("file", file.Name()))
|
||||
// if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") {
|
||||
// full_file_name_targets = append(full_file_name_targets, file.Name())
|
||||
// }
|
||||
// }
|
||||
|
||||
commits, err := data.GetCommitLogOfFilename(countryID, file_version)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -456,3 +439,40 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
|
|||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"files": commits})
|
||||
}
|
||||
|
||||
func (rr *RecipeRouter) doMergeJson(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: v2, change to binary instead
|
||||
if !binaryAPIhandler(w, r) {
|
||||
rr.taoLogger.Log.Warn("RecipeRouter.doMergeJson", zap.Error(errors.New("API is busy")))
|
||||
return
|
||||
} else {
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.doMergeJson", zap.Any("status", "ready"))
|
||||
}
|
||||
|
||||
// TODO: add binary command here
|
||||
}
|
||||
|
||||
func binaryAPIhandler(w http.ResponseWriter, r *http.Request) bool {
|
||||
timeout := 10 * time.Second
|
||||
|
||||
if !lockThenTimeout(&binaryApiLock, timeout) {
|
||||
http.Error(w, "API is busy", http.StatusServiceUnavailable)
|
||||
return false
|
||||
}
|
||||
defer binaryApiLock.Unlock()
|
||||
return true
|
||||
}
|
||||
|
||||
func lockThenTimeout(mutex *sync.Mutex, timeout time.Duration) bool {
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
mutex.Lock()
|
||||
close(ch)
|
||||
}()
|
||||
select {
|
||||
case <-ch:
|
||||
return true
|
||||
case <-time.After(timeout):
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
298
server/server.go
298
server/server.go
|
|
@ -4,13 +4,8 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"recipe-manager/config"
|
||||
"recipe-manager/data"
|
||||
"recipe-manager/enums/permissions"
|
||||
|
|
@ -23,8 +18,8 @@ import (
|
|||
"recipe-manager/services/sheet"
|
||||
"recipe-manager/services/user"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
|
|
@ -32,10 +27,6 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
pythonApiLock sync.Mutex
|
||||
)
|
||||
|
||||
func loadConfig(path string) (*config.ServerConfig, error) {
|
||||
viper.AddConfigPath(path)
|
||||
viper.SetConfigName("app")
|
||||
|
|
@ -123,6 +114,7 @@ func (s *Server) createHandler() {
|
|||
|
||||
// Seed
|
||||
_ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "kenta420", "poomipat.c@forth.co.th", "", permissions.SuperAdmin)
|
||||
_ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "phu", "pakin.t@forth.co.th", "", permissions.SuperAdmin)
|
||||
|
||||
// Auth Router
|
||||
r.Group(func(r chi.Router) {
|
||||
|
|
@ -137,265 +129,6 @@ func (s *Server) createHandler() {
|
|||
return middlewares.Authorize(s.oauth, userService, next)
|
||||
})
|
||||
|
||||
r.Post("/merge", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// locking
|
||||
if !pyAPIhandler(w, r) {
|
||||
s.taoLogger.Log.Warn("Merge - u tried to access while another u is requesting merge",
|
||||
zap.String("user", r.Context().Value("user").(*models.User).Name))
|
||||
return
|
||||
} else {
|
||||
s.taoLogger.Log.Debug("Merge - u has access", zap.String("user", r.Context().Value("user").(*models.User).Name))
|
||||
}
|
||||
|
||||
var targetMap map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&targetMap)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
s.taoLogger.Log.Fatal("Merge request failed", zap.Error(err))
|
||||
return
|
||||
}
|
||||
repo_path := "cofffeemachineConfig/coffeethai02_"
|
||||
|
||||
master_version := targetMap["master"].(string)
|
||||
dev_version := targetMap["dev"].(string)
|
||||
output_path := targetMap["output"].(string)
|
||||
changelog_path := targetMap["changelog"].(string)
|
||||
|
||||
// find target file in the cofffeemachineConfig
|
||||
if _, err := os.Stat(repo_path + master_version + ".json"); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
s.taoLogger.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)
|
||||
s.taoLogger.Log.Fatal("Merge request failed. Dev file not found: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
master_path := repo_path + master_version + ".json"
|
||||
dev_path := repo_path + dev_version + ".json"
|
||||
|
||||
// Get who's requesting
|
||||
u := r.Context().Value("user").(*models.User)
|
||||
s.taoLogger.Log.Info("Request merge by", zap.String("user", u.Name))
|
||||
|
||||
// lookup for python exec
|
||||
pyExec, err := exec.LookPath("python")
|
||||
if err != nil {
|
||||
s.taoLogger.Log.Fatal("Python error: ", zap.Error(err))
|
||||
} else {
|
||||
pyExec, err = filepath.Abs(pyExec)
|
||||
}
|
||||
|
||||
s.taoLogger.Log.Info("Found python exec: ", zap.String("PythonPath", pyExec))
|
||||
// target api file
|
||||
mergeApi, api_err := os.Open("./python_api/merge_recipe.py")
|
||||
if api_err != nil {
|
||||
s.taoLogger.Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error()))
|
||||
}
|
||||
defer mergeApi.Close()
|
||||
// log.Println("Locate python api", merge_api.Name())
|
||||
s.taoLogger.Log.Info("Locate python api", zap.String("ApiName", mergeApi.Name()))
|
||||
cmd := exec.Command(pyExec, mergeApi.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", u.Name)
|
||||
|
||||
// log.Println("Run merge command", cmd)
|
||||
s.taoLogger.Log.Info("Merge", zap.String("master", master_path), zap.String("dev", dev_path), zap.String("output", output_path))
|
||||
s.taoLogger.Log.Debug("Run merge command", zap.String("Command", cmd.String()))
|
||||
out, err := cmd.CombinedOutput()
|
||||
// log.Println(string(out))
|
||||
s.taoLogger.Log.Debug("Merge output", zap.String("Output", string(out)))
|
||||
if err != nil {
|
||||
// log.Fatalln("Merge request failed. Python merge failed: ", err)
|
||||
s.taoLogger.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"})
|
||||
s.taoLogger.Log.Info("Merge success", zap.String("output", "merge success"))
|
||||
})
|
||||
|
||||
r.Post("/dllog", func(w http.ResponseWriter, r *http.Request) {
|
||||
s.taoLogger.Log.Debug("Request uri = ", zap.String("uri", r.RequestURI))
|
||||
|
||||
s.taoLogger.Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query")))
|
||||
// param
|
||||
param := r.URL.Query().Get("query")
|
||||
s.taoLogger.Log.Debug("Param = ", zap.String("param", param))
|
||||
|
||||
var postRequest map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&postRequest)
|
||||
s.taoLogger.Log.Debug("Log request: ", zap.String("postRequest", fmt.Sprintf("%+v", postRequest)))
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
s.taoLogger.Log.Fatal("Decode in request failed: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
file_ext := ".html"
|
||||
if rb, ok := postRequest["htmlfile"].(bool); ok {
|
||||
if rj, ok := postRequest["requestJson"].(bool); ok {
|
||||
if rj {
|
||||
file_ext = ".json"
|
||||
} else if !rb && !rj {
|
||||
file_ext = ".log"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log_name := postRequest["filename"].(string)
|
||||
s.taoLogger.Log.Warn("Log file name: ", zap.String("filename", log_name))
|
||||
|
||||
if log_name == "" {
|
||||
s.taoLogger.Log.Fatal("Empty log file name")
|
||||
}
|
||||
|
||||
// log.Println("Log file ext: ", file_ext)
|
||||
default_changelog_path := "cofffeemachineConfig/" + param + "/"
|
||||
s.taoLogger.Log.Debug("Default changelog path: ", zap.String("default_changelog_path", default_changelog_path))
|
||||
|
||||
changelog_path := default_changelog_path + log_name + file_ext
|
||||
s.taoLogger.Log.Debug("Changelog path: ", zap.String("changelog_path", changelog_path))
|
||||
if strings.Contains(log_name, "cofffeemachineConfig") && strings.Contains(log_name, ".json") {
|
||||
changelog_path = log_name
|
||||
}
|
||||
|
||||
logFile, err := os.Open(changelog_path)
|
||||
if err != nil {
|
||||
s.taoLogger.Log.Fatal("Log request failed: ", zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
defer logFile.Close()
|
||||
|
||||
if file_ext == ".json" {
|
||||
|
||||
var logFileJson map[string]interface{}
|
||||
err = json.NewDecoder(logFile).Decode(&logFileJson)
|
||||
if err != nil {
|
||||
s.taoLogger.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)
|
||||
s.taoLogger.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 {
|
||||
s.taoLogger.Log.Fatal("Could not send blob", zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
//spl
|
||||
spl_path := strings.Split(r.RequestURI, "/")
|
||||
if len(spl_path) > 3 {
|
||||
s.taoLogger.Log.Warn("Unexpected depth: ",
|
||||
zap.String("path", r.RequestURI),
|
||||
zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
|
||||
}
|
||||
|
||||
if spl_path[2] == "" {
|
||||
s.taoLogger.Log.Error("Empty target dir", zap.String("path", r.RequestURI))
|
||||
}
|
||||
|
||||
s.taoLogger.Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
||||
|
||||
// s.taoLogger.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 {
|
||||
s.taoLogger.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
|
||||
}
|
||||
s.taoLogger.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) {
|
||||
s.taoLogger.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})
|
||||
s.taoLogger.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 {
|
||||
s.taoLogger.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 {
|
||||
s.taoLogger.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")
|
||||
|
||||
s.taoLogger.Log.Debug("Command: ", zap.String("command", cmd.String()))
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
||||
s.taoLogger.Log.Debug("Output: ", zap.String("output", string(out)))
|
||||
|
||||
if err != nil {
|
||||
s.taoLogger.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) {
|
||||
s.taoLogger.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 {
|
||||
|
|
@ -429,28 +162,3 @@ func (s *Server) createHandler() {
|
|||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
return s.server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
func pyAPIhandler(w http.ResponseWriter, r *http.Request) bool {
|
||||
timeout := 10 * time.Second
|
||||
|
||||
if !lockThenTimeout(&pythonApiLock, timeout) {
|
||||
http.Error(w, "API is busy", http.StatusServiceUnavailable)
|
||||
return false
|
||||
}
|
||||
defer pythonApiLock.Unlock()
|
||||
return true
|
||||
}
|
||||
|
||||
func lockThenTimeout(mutex *sync.Mutex, timeout time.Duration) bool {
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
mutex.Lock()
|
||||
close(ch)
|
||||
}()
|
||||
select {
|
||||
case <-ch:
|
||||
return true
|
||||
case <-time.After(timeout):
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"recipe-manager/data"
|
||||
"recipe-manager/models"
|
||||
"recipe-manager/services/logger"
|
||||
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue