Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug:static_data not included into the binary #194

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions server/backend/cafeteriaRatingDBInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package backend

import (
"database/sql"
"embed"
"encoding/json"
"os"
"path/filepath"

"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/guregu/null"
log "github.com/sirupsen/logrus"
Expand All @@ -30,13 +28,16 @@ type nameTag struct {
CanBeIncluded []string `json:"canbeincluded"`
}

//go:embed static_data
var staticData embed.FS

/*
Writes all available tags from the json file into tables in order to make them easier to use.
Will be executed once while the server is started.
*/
func initTagRatingOptions(db *gorm.DB) {
updateTagTable("backend/static_data/dishRatingTags.json", db, DISH)
updateTagTable("backend/static_data/cafeteriaRatingTags.json", db, CAFETERIA)
updateTagTable("static_data/dishRatingTags.json", db, DISH)
updateTagTable("static_data/cafeteriaRatingTags.json", db, CAFETERIA)
updateNameTagOptions(db)
addEntriesForCronJob(db, "averageRatingComputation", 300)
addEntriesForCronJob(db, "dishNameDownload", 302400) //run twice every week
Expand Down Expand Up @@ -70,8 +71,7 @@ If a tag with the exact german and english name does not exist yet, it will be c
Old tags won't be removed to prevent problems with foreign keys.
*/
func updateNameTagOptions(db *gorm.DB) {
absPathDishNames, _ := filepath.Abs("backend/static_data/dishNameTags.json")
tagsNames := generateNameTagListFromFile(absPathDishNames)
tagsNames := generateNameTagListFromFile("static_data/dishNameTags.json")
for _, v := range tagsNames.MultiLanguageNameTags {
var parentId int32
res := db.Model(&model.DishNameTagOption{}).
Expand Down Expand Up @@ -154,8 +154,7 @@ If an entry with the same German and English name exists, the entry won't be add
The TagType is used to identify the corresponding model
*/
func updateTagTable(path string, db *gorm.DB, tagType modelType) {
absPathDish, _ := filepath.Abs(path)
tagsDish := generateRatingTagListFromFile(absPathDish)
tagsDish := generateRatingTagListFromFile(path)
insertModel := getTagModel(tagType, db)
for _, v := range tagsDish.MultiLanguageTags {
var count int64
Expand Down Expand Up @@ -200,44 +199,29 @@ func getTagModel(tagType modelType, db *gorm.DB) *gorm.DB {
}

func generateNameTagListFromFile(path string) multiLanguageNameTags {
file := readFromFile(path)
file, err := staticData.ReadFile(path)
if err != nil {
log.WithError(err).Error("Error including json.")
}

var tags multiLanguageNameTags
errjson := json.NewDecoder(file).Decode(&tags)
errjson := json.Unmarshal(file, &tags)
if errjson != nil {
log.WithError(errjson).Error("Error while reading the file.")
log.WithError(errjson).Error("Error parsing nameTagList to json.")
}
defer func(jsonFile *os.File) {
err := jsonFile.Close()
if err != nil {
log.WithError(err).Error("Error in parsing json.")
}
}(file)
return tags
}

func generateRatingTagListFromFile(path string) multiLanguageTags {
file := readFromFile(path)
file, err := staticData.ReadFile(path)
if err != nil {
log.WithError(err).Error("Error including json.")
}

var tags multiLanguageTags
errjson := json.NewDecoder(file).Decode(&tags)
errjson := json.Unmarshal(file, &tags)
if errjson != nil {
log.WithError(errjson).Error("Error while reading or parsing the file.")
log.WithError(errjson).Error("Error parsing ratingTagList to json.")
}
defer func(jsonFile *os.File) {
err := jsonFile.Close()
if err != nil {
log.WithError(err).Error("Error in parsing json.")
}
}(file)
return tags
}

func readFromFile(path string) *os.File {
jsonFile, err := os.Open(path)

if err != nil {
log.WithError(err).Error("Unable to open file with path: ", path)
}

return jsonFile
}