move merge fn
This commit is contained in:
parent
baa5382c8b
commit
49017ab39a
4 changed files with 48 additions and 319 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue