Skip to content

Commit

Permalink
fixed static_data not being included into the binary
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 24, 2023
1 parent 2b5cb7e commit 4b04e75
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
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
}
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
google.golang.org/protobuf v1.31.0
gorm.io/driver/mysql v1.4.7
gorm.io/gorm v1.25.0
gorm.io/gorm v1.25.4
)

require (
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,5 @@ gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

0 comments on commit 4b04e75

Please sign in to comment.