package routers import ( "encoding/json" "fmt" "image/png" "net/http" "os" "path" "recipe-manager/contracts" "recipe-manager/data" "recipe-manager/enums/permissions" "recipe-manager/helpers" "recipe-manager/models" "recipe-manager/services/logger" "recipe-manager/services/recipe" "recipe-manager/services/sheet" "strconv" "strings" "sync" "time" "github.com/go-chi/chi/v5" "github.com/pkg/errors" "go.uber.org/zap" ) type RecipeRouter struct { data *data.Data sheetService sheet.SheetService recipeService recipe.RecipeService taoLogger *logger.TaoLogger cache_db *data.RedisCli } var ( binaryApiLock sync.Mutex updateMutex = sync.Mutex{} ) func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger, cache *data.RedisCli) *RecipeRouter { return &RecipeRouter{ data, sheetService, recipeService, taoLogger, cache, } } func (rr *RecipeRouter) Route(r chi.Router) { r.Route("/recipes", func(r chi.Router) { r.Get("/dashboard", rr.dashBoard) r.Get("/{country}/{filename}/all", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") country := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") rr.taoLogger.Log.Debug("RecipeRouter.GetAll", zap.Any("country", country), zap.Any("filename", filename)) result, err := rr.recipeService.GetAllRecipe(&contracts.GetAllRecipeRequest{ Country: country, Filename: filename, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetAll", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Get("/overview", rr.overview) r.Get("/{product_code}", rr.getRecipeByProductCode) r.Get("/{product_code}/mat", rr.getRecipeMatByProductCode) r.Get("/{country}/{filename}/toppings", rr.getToppings) r.Get("/{country}/{filename}/{product_code}/toppings", rr.getToppingsOfRecipe) r.Get("/{country}/{filename}/{product_code}/submenus", rr.getSubmenusOfRecipe) // fetch raw recipe json r.Get("/{country}/{filename}/{product_code}/raw_full", rr.getRawRecipeOfProductCode) // fetch image r.Get("/{country}/{filename}/{product_code}/image", rr.getImageOfProductCode) r.Get("/{country}/{filename}/json", rr.getRecipeJson) r.Post("/edit/{country}/{filename}", rr.updateRecipe) r.Get("/saved/{country}/{filename_version_only}", rr.getSavedRecipes) r.Get("/patch/get/{country}/{filename}", rr.getSavedAsPatches) r.Get("/departments", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") // return departments that user can access u := r.Context().Value("user").(*models.User) var result []string // if u.Permissions.IsHavePermission(permissions.ThaiPermission) { // result = append(result, "tha") // } // if u.Permissions.IsHavePermission(permissions.MalayPermission) { // result = append(result, "mys") // } // if u.Permissions.IsHavePermission(permissions.AusPermission) { // result = append(result, "aus") // } // if u.Permissions.IsHavePermission(permissions.Alpha3Permission) { // result = append(result, "alpha") // } for _, v := range helpers.LoadCountrySettingsWithPermissions() { fmt.Println(u.Email, "can access ", v.CountryID, " ? ") if u.Permissions.IsHavePermission(permissions.Permission(v.CountryPermission)) { result = append(result, v.CountryID) fmt.Println("yes") } } if err := json.NewEncoder(w).Encode(&contracts.Response[[]string]{ Result: result, }); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetDepartments", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Get("/countries", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") // get key from map var keys []string for k := range rr.data.AllRecipeFiles { countryName, err := rr.data.GetCountryNameByID(k) if err != nil { continue } keys = append(keys, countryName) } if err := json.NewEncoder(w).Encode(keys); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetCountryRecipe", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Get("/{country}/versions", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") countryName := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(countryName) if err != nil { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound) return } var files []string for _, v := range rr.data.AllRecipeFiles[countryID] { files = append(files, v.Name) } if err := json.NewEncoder(w).Encode(files); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetVersionsCountryRecipe", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) { result := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE") var mapResult []map[string]string for _, v := range result { mapResult = append(mapResult, map[string]string{ "productCode": v[0].(string), "name": v[1].(string), "otherName": v[2].(string), "description": v[3].(string), "otherDescription": v[4].(string), "picture": v[5].(string), }) } if err := json.NewEncoder(w).Encode(mapResult); err != nil { rr.taoLogger.Log.Error("RecipeRouter.TestSheet", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } }) r.Post("/sort/{country}/{filename}", rr.sortRecipe) r.Post("/upgrade/{country}/{filename}", rr.upgradeVersion) }) } // ====================== Handler ================================= func (rr *RecipeRouter) dashBoard(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") country := r.URL.Query().Get("country") filename := r.URL.Query().Get("filename") rr.taoLogger.Log.Debug("RecipeRouter.Dashboard", zap.Any("country", country), zap.Any("filename", filename)) result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{ Country: country, Filename: filename, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.Dashboard", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) overview(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") var take, offset uint64 = 10, 0 if newOffset, err := strconv.ParseUint(r.URL.Query().Get("offset"), 10, 64); err == nil { offset = newOffset } if newTake, err := strconv.ParseUint(r.URL.Query().Get("take"), 10, 64); err == nil { take = newTake } country := r.URL.Query().Get("country") filename := r.URL.Query().Get("filename") materialIds := r.URL.Query().Get("materialIds") rr.taoLogger.Log.Debug("RecipeRouter.Overview", zap.Any("take", take), zap.Any("offset", offset), zap.Any("country", country), zap.Any("filename", filename), zap.Any("materialIds", materialIds)) var materialIdsUint []int for _, v := range strings.Split(materialIds, ",") { materialIdUint, err := strconv.ParseUint(v, 10, 64) if err != nil || materialIdUint == 0 { continue } materialIdsUint = append(materialIdsUint, int(materialIdUint)) } result, err := rr.recipeService.GetRecipeOverview(&contracts.RecipeOverviewRequest{ Take: int(take), Skip: int(offset), Search: r.URL.Query().Get("search"), Country: country, Filename: filename, MatIds: materialIdsUint, }) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.Overview", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeByProductCode(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") fileName := r.URL.Query().Get("filename") country := r.URL.Query().Get("country") // recipe := rr.data.GetRecipe01() // recipeMetaData := rr.sheetService.GetSheet(r.Context(), "1rSUKcc5POR1KeZFGoeAZIoVoI7LPGztBhPw5Z_ConDE") // var recipeResult *models.Recipe01 // recipeMetaDataResult := map[string]string{} // for _, v := range recipe { // if v.ProductCode == productCode { // recipeResult = &v // break // } // } // for _, v := range recipeMetaData { // if v[0].(string) == productCode { // recipeMetaDataResult = map[string]string{ // "productCode": v[0].(string), // "name": v[1].(string), // "otherName": v[2].(string), // "description": v[3].(string), // "otherDescription": v[4].(string), // "picture": v[5].(string), // } // break // } // } // if recipeResult == nil { // http.Error(w, "Not Found", http.StatusNotFound) // return // } // json.NewEncoder(w).Encode(map[string]interface{}{ // "recipe": recipeResult, // "recipeMetaData": recipeMetaDataResult, // }) result, err := rr.recipeService.GetRecipeDetail(&contracts.RecipeDetailRequest{ Filename: fileName, Country: country, ProductCode: productCode, }) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err)) http.Error(w, fmt.Sprintf("Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeByProductCode", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeMatByProductCode(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") productCode := chi.URLParam(r, "product_code") fileName := r.URL.Query().Get("filename") country := r.URL.Query().Get("country") result, err := rr.recipeService.GetRecipeDetailMat(&contracts.RecipeDetailRequest{ Filename: fileName, Country: country, ProductCode: productCode, }) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err)) http.Error(w, fmt.Sprintf("Material Recipe file: %s with productCode: %s not found", fileName, productCode), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(result); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeMatByProductCode", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) getRecipeJson(w http.ResponseWriter, r *http.Request) { country := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") w.Header().Add("Content-Type", "application/json") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err)) http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } if err := json.NewEncoder(w).Encode(rr.data.GetRecipe(countryID, filename)); err != nil { rr.taoLogger.Log.Error("RecipeRouter.GetRecipeJson", zap.Error(err)) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) { rr.taoLogger.Log.Debug("Edit: ", zap.String("path", r.RequestURI)) filename := chi.URLParam(r, "filename") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(err)) http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } // Lock updateMutex.Lock() defer updateMutex.Unlock() targetRecipe := rr.data.GetRecipe(countryID, filename) rr.taoLogger.Log.Debug("Target => ", zap.Any("target", targetRecipe.MachineSetting.ConfigNumber)) // Body var ch_map map[string]interface{} var changes models.Recipe01 err = json.NewDecoder(r.Body).Decode(&ch_map) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Decode in request failed"))) http.Error(w, "Can't Decode Recipe request body.", http.StatusBadRequest) return } // commit request editor := ch_map["edit_by"].(string) commit_msg := ch_map["commit_msg"].(string) changes = changes.FromMap(ch_map) rr.taoLogger.Log.Debug("Changes: ", zap.Any("changes", changes)) // find the matched pd _, err = rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when get recipe by product code"))) http.Error(w, fmt.Sprintf("Recipe country: %s file: %s productCode: %s not found.", country, filename, changes.ProductCode), http.StatusNotFound) return } // menuMap := targetMenu.ToMap() changeMap := changes.ToMap() // Find changes // for key, val := range menuMap { // // rr.taoLogger.Log.Debug("..DynamicCompare.key", zap.Any("key", key)) // testBool, err := helpers.DynamicCompare(val, changeMap[key]) // // if err != nil { // // rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "DynamicCompare in request failed"))) // // http.Error(w, "Internal Error", http.StatusInternalServerError) // // return // // } // if !testBool && err == nil { // menuMap[key] = changeMap[key] // } // } // Apply changes tempRecipe := models.Recipe01{} tempRecipe = tempRecipe.FromMap(changeMap) // Disable temp! Was updated the master by patch. Too fast rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe) rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed")) // check if changed // Log.Debug("Check if changed", zap.Any("result", rr.data.GetRecipe01ByProductCode(changes.ProductCode))) // target saved filename saved_filename := path.Join("./cofffeemachineConfig", countryID, filename) // store @ temporary filex temp_file_name := helpers.GetTempFile(saved_filename, editor, 0) // push this change, editor, commit_msg into db // gen hash commit_hash, err := data.HashCommit(8) // rr.cache_db.SetToKey(commit_hash, targetRecipe) commit := data.CommitLog{ Id: commit_hash, Msg: commit_msg, Created_at: time.Now().Local().Format("2006-01-02 15:04:05"), Editor: editor, Change_file: temp_file_name, Relation: filename, } // ------------------------ SKIP THIS ------------------------ // save only changes. // get new change // newChangeRecipe, err := rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode) // if err != nil { // // error missing // rr.taoLogger.Log.Debug("RecipeRouter.UpdateRecipe.MissingNewChange", zap.Any("error", err)) // } // get new tempfile name if redis is connected; productCodeNoSpl := strings.ReplaceAll(changes.ProductCode, "-", "") patchName := "Recipe_" + productCodeNoSpl + "-" + commit_hash + "_" + filename + ".patch" // if cache service online if rr.cache_db.HealthCheck() == nil { // do change mode commit.Change_file = patchName commit.Relation = commit.Relation + "/patch" // add to patch list of that filename // filename:patchlist err := rr.cache_db.Add(countryID+"."+filename+":patchList", commit.Id) // add failed if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to add to patch list"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } } // this following codes do need users to pass update to each other // otherwise, the changes will diverge file, _ := os.Create(temp_file_name) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to create file"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } // write to local if cannot connect encoder := json.NewEncoder(file) encoder.SetIndent("", " ") // full err = encoder.Encode(tempRecipe) // ------------------------------------------------------------- // partial // err = encoder.Encode(changes) // put changes to redis if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when write file"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } // add to commit err = data.Insert(&commit) err = rr.cache_db.SetToKey(patchName, changes) // ^----- change this to patch if err != nil { rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipeCache", zap.Error(errors.WithMessage(err, "Error when write file"))) http.Error(w, "Internal Error", http.StatusInternalServerError) return } w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "status": "OK", "commit_id": commit_hash, }) } func (rr *RecipeRouter) updateMaterialSetting(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") country := r.URL.Query().Get("country") filename := r.URL.Query().Get("filename") rr.taoLogger.Log.Debug("RecipeRouter.updateMaterialSetting", zap.Any("country", country), zap.Any("filename", filename)) countryID, err := rr.data.GetCountryIDByName(country) if err != nil { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } updateMutex.Lock() defer updateMutex.Unlock() // get material setting materialSetting := rr.data.GetMaterialSetting(countryID, filename) if len(materialSetting) == 0 { http.Error(w, fmt.Sprintf("Recipe country: %s file: %s found empty settings.", country, filename), http.StatusNotFound) return } // create commit and set change } func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request) { file_version := chi.URLParam(r, "filename_version_only") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } commits, err := data.GetCommitLogOfFilename(countryID, file_version) // //fmt.Println("commits", commits) rr.taoLogger.Log.Debug("RecipeRouter.getSavedRecipes", zap.Any("commits", commits)) if err != nil || len(commits) == 0 { return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]interface{}{"files": commits}) } func (rr *RecipeRouter) getSavedAsPatches(w http.ResponseWriter, r *http.Request) { filename := chi.URLParam(r, "filename") country := chi.URLParam(r, "country") countryID, err := rr.data.GetCountryIDByName(country) if err != nil { http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound) return } patchList, err := rr.cache_db.GetList(countryID + "." + filename + ":patchList") if err != nil { // silent return, no patch return } // rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("targetPatchOf", countryID+"."+filename+":patchList"), zap.Any("patchList", patchList)) // find patch content from patch list keys, err := rr.cache_db.KeyList() if err != nil { // keys found nothing http.Error(w, "Internal Error", http.StatusInternalServerError) return } // loop through keys, if contain patch id patchMap := map[string]models.Recipe01{} for _, key := range keys { if strings.Contains(key, filename) && strings.Contains(key, "patch") { // check if legit saved file from patchList for _, patchID := range patchList { if strings.Contains(key, patchID) { // get patch content var recipePatch models.Recipe01 err := rr.cache_db.GetKeyTo(key, &recipePatch) if err != nil { // silent return, no patch return } // append to patch list // patchContents = append(patchContents, recipePatch) patchMap[patchID] = recipePatch } } } } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(patchMap) // The above code is using the Zap logging library in Go to log a debug message with two fields. The // first field is "targetPatchOf" with the value of countryID + "." + filename + ":patchList", and the // second field is "patchList" with the value of the variable patchList. This log message is related // to the "RecipeRouter.getSavedAsPatches" method. // rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("patchMap", patchMap)) } func (rr *RecipeRouter) getToppings(w http.ResponseWriter, r *http.Request) { countryID := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rr.data.GetToppings(countryID, filename)) } func (rr *RecipeRouter) getToppingsOfRecipe(w http.ResponseWriter, r *http.Request) { countryID := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") productCode := chi.URLParam(r, "product_code") w.Header().Add("Content-Type", "application/json") // all toppings // allToppings := rr.data.GetToppings(countryID, filename) topps, err := rr.data.GetToppingsOfRecipe(countryID, filename, productCode) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } json.NewEncoder(w).Encode(topps) } func (rr *RecipeRouter) getSubmenusOfRecipe(w http.ResponseWriter, r *http.Request) { countryID := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") productCode := chi.URLParam(r, "product_code") w.Header().Add("Content-Type", "application/json") submenus, err := rr.data.GetSubmenusOfRecipe(countryID, filename, productCode) rr.taoLogger.Log.Debug("RecipeRouter.getSubmenusOfRecipe", zap.Any("submenus", len(submenus))) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } json.NewEncoder(w).Encode(submenus) } // get raw recipe func (rr *RecipeRouter) getRawRecipeOfProductCode(w http.ResponseWriter, r *http.Request) { countryID := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") productCode := chi.URLParam(r, "product_code") // debug // rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("countryID", countryID), zap.Any("filename", filename), zap.Any("productCode", productCode)) w.Header().Add("Content-Type", "application/json") recipe, err := rr.data.GetRecipe01ByProductCode(filename, countryID, productCode) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } // return recipe // rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("recipe", recipe)) json.NewEncoder(w).Encode(recipe) } func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Request) { countryID := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") productCode := chi.URLParam(r, "product_code") w.Header().Add("Content-Type", "image/png") // get image recipe, err := rr.data.GetRecipe01ByProductCode(filename, countryID, productCode) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } // check if image string is not empty if recipe.UriData == "" { http.Error(w, "Image not found", http.StatusNotFound) return } // clean image uri name clean1 := strings.Replace(recipe.UriData, "\u003d", "=", -1) uriName := strings.Split(clean1, "=")[1] // img_dir := "taobin_project/image/page_drink_picture2_n/" // new img dir img_dir := "cofffeemachineConfig/" + countryID + "/.img/" fullPath := img_dir + uriName rr.taoLogger.Log.Debug("RecipeRouter.getImageOfProductCode", zap.Any("fullPath", fullPath)) // check if image file exists if _, err := os.Stat(fullPath); os.IsNotExist(err) { http.Error(w, "Image not found", http.StatusNotFound) return } // read image imgFile, err := os.Open(fullPath) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } defer imgFile.Close() thisImage, err := png.Decode(imgFile) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } // write image png.Encode(w, thisImage) } func APIhandler(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 } } // ------------------ Sorting ------------------\ // FIXME: sorting not working, will look into it later. func (rr *RecipeRouter) sortRecipe(w http.ResponseWriter, r *http.Request) { country := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") // get sort type from body var sortConfig map[string]interface{} err := json.NewDecoder(r.Body).Decode(&sortConfig) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } sortKey := sortConfig["sortKey"].(string) ascending := sortConfig["ascending"].(bool) // store cache rr.cache_db.SetKeyTimeout(country+"_"+filename+"_latestSortKey", sortKey, 3600) rr.cache_db.SetKeyTimeout(country+"_"+filename+"_"+sortKey, ascending, 3600) // sort recipe rr.data.SortRecipe(country, filename, sortKey, ascending) w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "status": "OK", "key": sortKey, "result": rr.data.GetRecipe(country, filename).Recipe01, }) } // TODO: apply contents from commit given by id // this do match at server func (rr *RecipeRouter) upgradeVersion(w http.ResponseWriter, r *http.Request) { country := chi.URLParam(r, "country") filename := chi.URLParam(r, "filename") // get short version country countryID, err := rr.data.GetCountryIDByName(country) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) return } // get commit id and patch source from body var commitInfo map[string]interface{} err = json.NewDecoder(r.Body).Decode(&commitInfo) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err)) http.Error(w, err.Error(), http.StatusBadRequest) return } mode := "" rr.taoLogger.Log.Debug("RecipeRouter.upgradeVersion", zap.Any("commitInfo", commitInfo)) changeKey, ok := commitInfo["changeKey"].(string) if !ok { rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err)) http.Error(w, err.Error(), http.StatusBadRequest) return } // optional AppliedMachineRecipe: if provided, use this instead // this does mean recipe from machine has been applied on the client website // appliedMachineRecipe := commitInfo["appliedMachineRecipe"] if appliedMachineRecipe != nil { mode = "NoCache" } else { mode = "Recipe" } // upgrade version // merge from website result, err := rr.data.Merge(countryID, filename, mode, changeKey, appliedMachineRecipe) if err != nil { rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) return } // refresh recipe files so the running server can see the new file without restart itself rr.data.AllRecipeFiles = helpers.ScanRecipeFiles(rr.data.Countries) w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "result": result, }) }