From 0604a3b77f6c96fe6ad59e0cebc5ae9617ef88ff Mon Sep 17 00:00:00 2001 From: "pakintada@gmail.com" Date: Mon, 5 Feb 2024 17:07:04 +0700 Subject: [PATCH] Add redis. Prep for new save file --- server/data/data.go | 215 +++++++++++++++++++++++-------------- server/data/redis.go | 137 +++++++++++++++++++++++ server/routers/material.go | 6 ++ server/routers/recipe.go | 5 +- server/server.go | 19 ++-- 5 files changed, 295 insertions(+), 87 deletions(-) create mode 100644 server/data/redis.go diff --git a/server/data/data.go b/server/data/data.go index 102ae8a..2091222 100644 --- a/server/data/data.go +++ b/server/data/data.go @@ -27,10 +27,11 @@ type Data struct { CurrentCountryID map[string]string DefaultCountryMap []DefaultByCountry AllRecipeFiles map[string][]helpers.RecipePath - currentRecipe map[string]*models.Recipe + CurrentRecipe map[string]*models.Recipe recipeMap map[string]RecipeWithTimeStamps Countries []helpers.CountryName taoLogger *logger.TaoLogger + redisClient *RedisCli } type DefaultByCountry struct { @@ -53,7 +54,7 @@ var ( } ) -func NewData(taoLogger *logger.TaoLogger) *Data { +func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data { allRecipeFiles := helpers.ScanRecipeFiles(countries) @@ -131,6 +132,8 @@ func NewData(taoLogger *logger.TaoLogger) *Data { log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err) } + redisClient.SetToKey(v2.Name, defaultRecipe) + currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe break } @@ -169,7 +172,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data { CurrentFile: currentFileMap, CurrentCountryID: CurrentCountryIDMap, AllRecipeFiles: allRecipeFiles, - currentRecipe: currentDefaultFileForEachCountry, + CurrentRecipe: currentDefaultFileForEachCountry, recipeMap: map[string]RecipeWithTimeStamps{ defaultFile: { Recipe: currentDefaultFileForEachCountry, @@ -179,6 +182,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data { Countries: countries, taoLogger: taoLogger, DefaultCountryMap: defaultForEachCountry, + redisClient: redisClient, } } @@ -189,17 +193,47 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { // TODO: concat submenu into recipe if countryID == "" { - return d.currentRecipe["tha"] + d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyCountryId", "return default country = tha")) + return d.CurrentRecipe["tha"] } - if filename == "" || filename == d.CurrentFile[countryID] { - return d.currentRecipe[countryID] + if filename == "" { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("EmptyFilename", filename)) + return d.CurrentRecipe[countryID] } - if recipe, ok := d.recipeMap[filename]; ok { + // do check if match the current pointer + if d.CurrentFile[countryID] == filename { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("FileMatchCurrent", "return from stored "+filename), zap.Any("CurrentFile", d.CurrentFile)) + + // make sure recipe vesion is equal + currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber + // get requested version + requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0]) + if currentConfig != requestedConfig { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("ActualFileNotMatch", "Skip this!")) + } else { + // if equal, OK + return d.CurrentRecipe[countryID] + } + + } + + if recipe, ok := d.recipeMap[filename]; ok && d.redisClient.HealthCheck() != nil { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("ValidOnStored", "return from stored "+filename)) d.CurrentFile[countryID] = filename // d.CurrentCountryID[countryID] = countryID - return recipe.Recipe[countryID] + + // make sure recipe vesion is equal + currentConfig := d.CurrentRecipe[countryID].MachineSetting.ConfigNumber + // get requested version + requestedConfig, _ := strconv.Atoi(strings.Split(strings.Split(filename, "_")[1], ".")[0]) + if currentConfig != requestedConfig { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("InvalidOnStored", "Skip this!")) + } else { + // if equal, OK + return recipe.Recipe[countryID] + } } // change current version and read new recipe @@ -208,17 +242,49 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { filename = d.CurrentFile[countryID] } - // d.CurrentFile[countryID] = filename - d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID)) - // d.CurrentCountryID[countryID] = countryID + // var recipe *models.Recipe = &models.Recipe{} + + // do check if redis contains the recipe + var cached_recipe *models.Recipe = &models.Recipe{} + + if err := d.redisClient.GetKeyTo(filename, cached_recipe); err != nil { + d.taoLogger.Log.Debug("GetRecipe.Cached", zap.Any("GetCacheRecipeError", err)) + d.taoLogger.Log.Debug("GetRecipe", zap.String("filename", filename), zap.String("countryID", countryID)) + // d.CurrentCountryID[countryID] = countryID + cached_recipe = nil + } + + if cached_recipe != nil { + d.taoLogger.Log.Debug("GetRecipe", zap.Any("Check on cached recipe invalid", cached_recipe == nil), zap.Any("test config number", cached_recipe.MachineSetting.ConfigNumber)) + } + recipe, err := helpers.ReadRecipeFile(countryID, filename) if err != nil { - d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err)) - return d.currentRecipe[countryID] + d.taoLogger.Log.Debug("GetRecipe", zap.Any("ReadRecipeError -> return default", err)) + return d.CurrentRecipe[countryID] } - d.currentRecipe[countryID] = recipe + // cache to redis + d.redisClient.SetToKey(filename, recipe) + + if err != nil { + d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err)) + return d.CurrentRecipe[countryID] + } + + //. service is connected. Use from cache + + // check healthcheck redis + err = d.redisClient.HealthCheck() + d.taoLogger.Log.Info("GetRecipe: HealthCheck", zap.Any("result", err)) + if d.redisClient.HealthCheck() == nil && cached_recipe != nil { + d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "yes")) + d.CurrentRecipe[countryID] = cached_recipe + } else { + d.taoLogger.Log.Debug("GetRecipeCached", zap.Any("cached_recipe", "no")) + d.CurrentRecipe[countryID] = recipe + } // save to map if len(d.recipeMap) > 5 { // limit keep in memory 5 version @@ -235,11 +301,11 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } - return d.currentRecipe[countryID] + return d.CurrentRecipe[countryID] } // func (d *Data) GetRecipe01() []models.Recipe01 { @@ -272,7 +338,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) // fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile) // fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID) - for _, v := range d.currentRecipe[countryID].Recipe01 { + for _, v := range d.CurrentRecipe[countryID].Recipe01 { if v.ProductCode == productCode { return v, nil } else if len(v.SubMenu) > 0 { @@ -320,26 +386,26 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) } } - recipe, err := helpers.ReadRecipeFile(countryID, filename) + recipe := d.GetRecipe(countryID, filename) - if err != nil { - d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err)) - for _, v := range d.currentRecipe[countryID].Recipe01 { - if v.ProductCode == productCode { - return v, fmt.Errorf("[DEFAULT]-ERR") - } else if len(v.SubMenu) > 0 { - for _, subMenu := range v.SubMenu { - if subMenu.ProductCode == productCode { - return subMenu, fmt.Errorf("[DEFAULT]-ERR") - } - } - } - } - } + // if err != nil { + // d.taoLogger.Log.Error("GetRecipe01ByProductCode: Error when read recipe file, Return default recipe", zap.Error(err)) + // for _, v := range d.CurrentRecipe[countryID].Recipe01 { + // if v.ProductCode == productCode { + // return v, fmt.Errorf("[DEFAULT]-ERR") + // } else if len(v.SubMenu) > 0 { + // for _, subMenu := range v.SubMenu { + // if subMenu.ProductCode == productCode { + // return subMenu, fmt.Errorf("[DEFAULT]-ERR") + // } + // } + // } + // } + // } d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber)) - d.currentRecipe[countryID] = recipe + d.CurrentRecipe[countryID] = recipe // save to map if len(d.recipeMap) > 5 { // limit keep in memory 5 version @@ -356,11 +422,11 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } - for _, v := range d.currentRecipe[countryID].Recipe01 { + for _, v := range d.CurrentRecipe[countryID].Recipe01 { if v.ProductCode == productCode { // d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v)) return v, nil @@ -438,21 +504,21 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS if countryID == "" { // copy(result, d.currentRecipe[countryID].MaterialSetting) - return d.currentRecipe[countryID].MaterialSetting + return d.CurrentRecipe[countryID].MaterialSetting } if !strings.Contains(filename, "tmp") { if filename == "" || filename == d.CurrentFile[countryID] { // copy(result, d.currentRecipe[countryID].MaterialSetting) // d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result)) - return d.currentRecipe[countryID].MaterialSetting + return d.CurrentRecipe[countryID].MaterialSetting } if recipe, ok := d.recipeMap[filename]; ok { copy(result, recipe.Recipe[countryID].MaterialSetting) d.CurrentFile[countryID] = filename // d.CurrentCountryID[countryID] = countryID - return d.currentRecipe[countryID].MaterialSetting + return d.CurrentRecipe[countryID].MaterialSetting } } @@ -464,17 +530,17 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS // d.CurrentFile[countryID] = filename // d.CurrentCountryID[countryID] = countryID - recipe, err := helpers.ReadRecipeFile(countryID, filename) + recipe := d.GetRecipe(countryID, filename) - if err != nil { - d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err)) - copy(result, d.currentRecipe[countryID].MaterialSetting) - return d.currentRecipe[countryID].MaterialSetting - } + // if err != nil { + // d.taoLogger.Log.Error("GetMaterialSetting: Error when read recipe file, Return default recipe", zap.Error(err)) + // copy(result, d.CurrentRecipe[countryID].MaterialSetting) + // return d.CurrentRecipe[countryID].MaterialSetting + // } // d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting)) - d.currentRecipe[countryID] = recipe + d.CurrentRecipe[countryID] = recipe // save to map if len(d.recipeMap) > 5 { // limit keep in memory 5 version @@ -491,7 +557,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } @@ -501,29 +567,26 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingGroup { if countryID == "" { - return d.currentRecipe[countryID].Topping.ToppingGroup + return d.CurrentRecipe[countryID].Topping.ToppingGroup } if !strings.Contains(filename, ".tmp") { if filename == "" || filename == d.CurrentFile[countryID] { - return d.currentRecipe[countryID].Topping.ToppingGroup + return d.CurrentRecipe[countryID].Topping.ToppingGroup } if _, ok := d.recipeMap[countryID]; ok { d.CurrentFile[countryID] = filename - return d.currentRecipe[countryID].Topping.ToppingGroup + return d.CurrentRecipe[countryID].Topping.ToppingGroup } } if filename == "default" { filename = d.CurrentFile[countryID] } - recipe, err := helpers.ReadRecipeFile(countryID, filename) - if err != nil { - return d.currentRecipe[countryID].Topping.ToppingGroup - } + recipe := d.GetRecipe(countryID, filename) - d.currentRecipe[countryID] = recipe + d.CurrentRecipe[countryID] = recipe if len(d.recipeMap) > 5 { // limit keep in memory 5 version // remove oldest version @@ -539,7 +602,7 @@ func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingG } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } @@ -549,17 +612,17 @@ func (d *Data) GetAllToppingGroups(countryID, filename string) []models.ToppingG func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList { // do return default if countryID == "" { - return d.currentRecipe[countryID].Topping.ToppingList + return d.CurrentRecipe[countryID].Topping.ToppingList } // handle temporary file if !strings.Contains(filename, ".tmp") { if filename == "" || filename == d.CurrentFile[countryID] { - return d.currentRecipe[countryID].Topping.ToppingList + return d.CurrentRecipe[countryID].Topping.ToppingList } if _, ok := d.recipeMap[countryID]; ok { d.CurrentFile[countryID] = filename - return d.currentRecipe[countryID].Topping.ToppingList + return d.CurrentRecipe[countryID].Topping.ToppingList } } @@ -567,10 +630,7 @@ func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList filename = d.CurrentFile[countryID] } - recipe, err := helpers.ReadRecipeFile(countryID, filename) - if err != nil { - return d.currentRecipe[countryID].Topping.ToppingList - } + recipe := d.GetRecipe(countryID, filename) if len(d.recipeMap) > 5 { // limit keep in memory 5 version // remove oldest version @@ -586,7 +646,7 @@ func (d *Data) GetToppingsList(countryID, filename string) []models.ToppingList } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } @@ -597,7 +657,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model var result []models.MaterialCode if filename == "" || filename == d.CurrentFile[countryID] { - result = d.currentRecipe[countryID].MaterialCode + result = d.CurrentRecipe[countryID].MaterialCode } else if recipe, ok := d.recipeMap[filename]; ok { d.CurrentFile[countryID] = filename return recipe.Recipe[countryID].MaterialCode @@ -609,14 +669,14 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model // d.CurrentFile[countryID] = filename // d.CurrentCountryID[countryID] = countryID - recipe, err := helpers.ReadRecipeFile(countryID, filename) + recipe := d.GetRecipe(countryID, filename) - if err != nil { - d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err)) - return d.currentRecipe[countryID].MaterialCode - } + // if err != nil { + // d.taoLogger.Log.Error("GetMaterialCode: Error when read recipe file, Return default recipe", zap.Error(err)) + // return d.CurrentRecipe[countryID].MaterialCode + // } - d.currentRecipe[countryID] = recipe + d.CurrentRecipe[countryID] = recipe // save to map if len(d.recipeMap) > 5 { // limit keep in memory 5 version @@ -633,11 +693,11 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model } d.recipeMap[filename] = RecipeWithTimeStamps{ - Recipe: d.currentRecipe, + Recipe: d.CurrentRecipe, TimeStamps: time.Now().Unix(), } - result = d.currentRecipe[countryID].MaterialCode + result = d.CurrentRecipe[countryID].MaterialCode } if len(ids) == 0 { @@ -664,7 +724,7 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model func (d *Data) GetToppings(countryID, filename string) models.Topping { if filename == "" || filename == d.CurrentFile[countryID] { - return d.currentRecipe[countryID].Topping + return d.CurrentRecipe[countryID].Topping } else if recipe, ok := d.recipeMap[filename]; ok { d.CurrentFile[countryID] = filename return recipe.Recipe[countryID].Topping @@ -676,14 +736,9 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping { // d.CurrentFile[countryID] = filename // d.CurrentCountryID[countryID] = countryID - recipe, err := helpers.ReadRecipeFile(countryID, filename) + recipe := d.GetRecipe(countryID, filename) - if err != nil { - d.taoLogger.Log.Error("GetToppings: Error when read recipe file, Return default recipe", zap.Error(err)) - return d.currentRecipe[countryID].Topping - } - - d.currentRecipe[countryID] = recipe + d.CurrentRecipe[countryID] = recipe return recipe.Topping } diff --git a/server/data/redis.go b/server/data/redis.go new file mode 100644 index 0000000..865de1f --- /dev/null +++ b/server/data/redis.go @@ -0,0 +1,137 @@ +package data + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "time" + + "github.com/redis/go-redis/v9" +) + +type RedisCli struct { + Client *redis.Client +} + +func NewRedisClient(address, password string) *RedisCli { + + // option, err := redis.ParseURL("redis://" + username + ":" + password + "@localhost:6379/" + db) + + options := redis.Options{ + Addr: address, + Password: password, + DB: 0, + } + + client := redis.NewClient(&options) + + if err := client.Ping(context.Background()); err != nil { + fmt.Println("trying localhost ...", err) + client = redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: password, + DB: 0, + }) + + if err_local := client.Ping(context.Background()); err_local != nil { + fmt.Println("> result ====> ", err_local) + // do as warning + } else { + fmt.Println("\n> Localhost Redis OK!\n") + } + } else { + fmt.Println("\n> Redis OK! \n") + } + + return &RedisCli{ + Client: client, + } +} + +func (r *RedisCli) HealthCheck() error { + return r.Client.Ping(context.Background()).Err() +} + +func (r *RedisCli) GetKeyTo(source string, dest interface{}) error { + fmt.Println("Redis> GET ", source) + + // if cannot pass healthcheck, return err + if err := r.HealthCheck(); err != nil { + fmt.Println("HS> GET error ", err) + return err + } + + saved, err := r.Client.Get(context.Background(), source).Result() + + // chcek EOF + // fmt.Println("GET last char ", saved[len(saved)-1:]) + + if saved == "" || err != nil { + fmt.Println("GET error (empty on error)|", err, "|while saved=", saved) + return err + } + + // fmt.Println("GET ", saved) + + // if err != nil { + // fmt.Println("GET error ", err) + // return err + // } + err = json.NewDecoder(bytes.NewBufferString(saved)).Decode(dest) + + if err != nil { + fmt.Println("GET error ", err) + } + + return err +} + +func (r *RedisCli) SetToKey(key string, value interface{}) error { + fmt.Println("Redis> SET ", key) + + // if cannot pass healthcheck, return err + if err := r.HealthCheck(); err != nil { + fmt.Println("HS> SET error ", err) + return err + } + + saved, err := json.Marshal(value) + if err != nil { + fmt.Println("SET error ", err) + return err + } + + // late error + err = r.Client.Set(context.Background(), key, saved, redis.KeepTTL).Err() + + if err != nil { + fmt.Println("error on SET ", err) + } + + return err +} + +func (r *RedisCli) ExpireKey(key string) error { + + // if cannot pass healthcheck, return err + if err := r.HealthCheck(); err != nil { + fmt.Println("HS> EXPIRE error ", err) + return err + } + + return r.Client.Expire(context.Background(), key, 0).Err() +} + +func (r *RedisCli) SetKeyTimeout(key string, value interface{}, timeout int) error { + + // if cannot pass healthcheck, return err + if err := r.HealthCheck(); err != nil { + fmt.Println("HS> EXPIRE error ", err) + return err + } + + err := r.Client.Expire(context.Background(), key, time.Duration(timeout)*time.Second).Err() + fmt.Println("error on EXPIRE ", err) + return err +} diff --git a/server/routers/material.go b/server/routers/material.go index f8cf3af..87b8ec0 100644 --- a/server/routers/material.go +++ b/server/routers/material.go @@ -48,6 +48,12 @@ func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.R matSettings := mr.data.GetMaterialSetting(country, filename) matCodes := mr.data.GetRecipe(country, filename).MaterialCode + if len(matSettings) == 0 { + // mr.taoLogger.Log.Error("MaterialRouter.GetFullMaterialDetail", zap.Error(err)) + http.Error(w, "Material not found", http.StatusNotFound) + return + } + // combine materialDetails := []map[string]interface{}{} diff --git a/server/routers/recipe.go b/server/routers/recipe.go index 0903aca..186fe32 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -30,6 +30,7 @@ type RecipeRouter struct { sheetService sheet.SheetService recipeService recipe.RecipeService taoLogger *logger.TaoLogger + cache_db *data.RedisCli } var ( @@ -37,12 +38,13 @@ var ( updateMutex = sync.Mutex{} ) -func NewRecipeRouter(data *data.Data, recipeService recipe.RecipeService, sheetService sheet.SheetService, taoLogger *logger.TaoLogger) *RecipeRouter { +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, } } @@ -460,6 +462,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) { // gen hash commit_hash, err := data.HashCommit(8) + rr.cache_db.SetToKey(commit_hash, targetRecipe) commit := data.CommitLog{ diff --git a/server/server.go b/server/server.go index 5b2853b..95de08e 100644 --- a/server/server.go +++ b/server/server.go @@ -20,7 +20,6 @@ import ( "strings" "github.com/jmoiron/sqlx" - "github.com/redis/go-redis/v9" "github.com/go-chi/chi/v5" "github.com/go-chi/cors" @@ -31,7 +30,7 @@ type Server struct { server *http.Server data *data.Data database *sqlx.DB - cache_db *redis.Client + cache_db *data.RedisCli cfg *config.ServerConfig oauth oauth.OAuthService taoLogger *logger.TaoLogger @@ -42,13 +41,15 @@ func NewServer(cfg *config.ServerConfig, oauthService oauth.OAuthService) *Serve taoLogger := logger.NewTaoLogger(cfg) taoLogger.Log = taoLogger.Log.Named("Server") + redisClient := data.NewRedisClient("redis:6379", "") + return &Server{ server: &http.Server{Addr: fmt.Sprintf(":%d", cfg.ServerPort)}, - data: data.NewData(taoLogger), + data: data.NewData(taoLogger, redisClient), database: data.NewSqliteDatabase(), - cache_db: data.NewRedisClient("redis:6379", "", ""), + cache_db: redisClient, cfg: cfg, - oauth: oauth.NewOAuthService(serverCfg), + oauth: oauth.NewOAuthService(cfg), taoLogger: taoLogger, } } @@ -101,6 +102,12 @@ func (s *Server) createHandler() { ar.Route(r) }) + // Initial redis + for k, v := range s.data.CurrentRecipe { + s.taoLogger.Log.Debug("Caching", zap.Any("Recipe", k)) + s.cache_db.SetToKey(k, v) + } + // Protected Group r.Group(func(r chi.Router) { @@ -116,7 +123,7 @@ func (s *Server) createHandler() { } // Recipe Router - rr := routers.NewRecipeRouter(s.data, recipeService, sheetService, s.taoLogger) + rr := routers.NewRecipeRouter(s.data, recipeService, sheetService, s.taoLogger, s.cache_db) rr.Route(r) // Material Router