diff --git a/README.md b/README.md index 7f2dd65c..2c29a84b 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ To start the server there are environment variables, as well as command line opt ```bash cd server export DB_DSN="Your gorm DB connection string for example: gorm:GORM_USER_PASSWORD@tcp(localhost:3306)/campus_backend" -go run ./main.go [-MensaCron 0] +go run ./main.go ``` #### Environment Variables @@ -72,10 +72,6 @@ There are a few environment variables available: * [REQUIRED] `DB_DSN`: The [GORM](https://gorm.io/) [DB connection string](https://gorm.io/docs/connecting_to_the_database.html#MySQL) for connecting to the MySQL DB. Example: `gorm@tcp(localhost:3306)/campus_backend` * [OPTIONAL] `SENTRY_DSN`: The Sentry [Data Source Name](https://sentry-docs-git-patch-1.sentry.dev/product/sentry-basics/dsn-explainer/) for reporting issues and crashes. -#### Command Line Arguments - -* [OPTIONAL] `-MensaCron 0`: Providing this argument deactivates the Mensa Rating cronjobs if not needed in a local setup. Be aware, this option will change in a future version ([#117](https://github.com/TUM-Dev/Campus-Backend/issues/117) and [#115](https://github.com/TUM-Dev/Campus-Backend/issues/115)). - ## Running the Server (Docker) ```bash docker compose up -d diff --git a/client/go.mod b/client/go.mod index dac32653..2dd88216 100644 --- a/client/go.mod +++ b/client/go.mod @@ -3,23 +3,19 @@ module github.com/TUM-Dev/Campus-Backend/client go 1.21 require ( - github.com/TUM-Dev/Campus-Backend/api v0.0.0-20221212204029-68b05b451617 - github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230111021245-cd17e382bda3 + github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230913203125-6bbe13013fa5 github.com/sirupsen/logrus v1.9.3 - google.golang.org/grpc v1.57.0 + google.golang.org/grpc v1.58.0 google.golang.org/protobuf v1.31.0 ) require ( - github.com/golang/glog v1.1.0 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect - golang.org/x/net v0.9.0 // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/text v0.9.0 // indirect - google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect - google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/sys v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect + google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect ) diff --git a/client/go.sum b/client/go.sum index 21378bde..128161a1 100644 --- a/client/go.sum +++ b/client/go.sum @@ -1,7 +1,5 @@ -github.com/TUM-Dev/Campus-Backend/api v0.0.0-20221212204029-68b05b451617 h1:/o27aY5kfd4JnrqtbsMLIGwe+VBqS7j3ovCG/cXtAWk= -github.com/TUM-Dev/Campus-Backend/api v0.0.0-20221212204029-68b05b451617/go.mod h1:hDLh5lxHD53m8/eDGsXJDqc9ONXqbJPa9b6uzpfl8Ow= -github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230111021245-cd17e382bda3 h1:b585GRp+ZPA9j2EM0KQSSFiknR2SVF18FcRHRHnIIA4= -github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230111021245-cd17e382bda3/go.mod h1:1gTg0lAImFNZiYKrJ/5OLM2UWoj7zeLzTX1WLJhF7LY= +github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230913203125-6bbe13013fa5 h1:/yMB5hJjFjEPB/HVP6wLTJWGqoCqQksXke+DiV2xUWY= +github.com/TUM-Dev/Campus-Backend/server v0.0.0-20230913203125-6bbe13013fa5/go.mod h1:7XoRdXmxK852GxcyraiovgyS5z/eZ9wsofV9on7wvGQ= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -13,45 +11,37 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 h1:LSsiG61v9IzzxMkqEr6nrix4miJI62xlRjwT7BYD2SM= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1/go.mod h1:Hbb13e3/WtqQ8U5hLGkek9gJvBLasHuPFI0UEGfnQ10= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= -golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 h1:9NWlQfY2ePejTmfwUH1OWwmznFa+0kKcHGPDvcPza9M= -google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54/go.mod h1:zqTuNwFlFRsw5zIts5VnzLQxSRqh+CGOTVMlYbY0Eyk= -google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 h1:m8v1xLLLzMe1m5P+gCTF8nJB9epwZQUBERm20Oy1poQ= -google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw= -google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0 h1:TLkBREm4nIsEcexnCjgQd5GQWaHcqMzwQV0TX9pq8S0= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0/go.mod h1:DNq5QpG7LJqD2AamLZ7zvKE0DEpVl2BSEVjFycAAjRY= +google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 h1:L6iMMGrtzgHsWofoFcihmDEMYeDR9KN/ThbPWGrh++g= +google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5/go.mod h1:oH/ZOT02u4kWEp7oYBGYFFkCdKS/uYR9Z7+0/xuuFp8= +google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q= +google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/grpc v1.58.0 h1:32JY8YpPMSR45K+c3o6b8VL73V+rR8k+DeMIr4vRH8o= +google.golang.org/grpc v1.58.0/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/client/localServer/localTestClient.go b/client/localServer/client.go similarity index 100% rename from client/localServer/localTestClient.go rename to client/localServer/client.go diff --git a/deployment/charts/backend/templates/deployments/backend-v2.yaml b/deployment/charts/backend/templates/deployments/backend-v2.yaml index 310387e7..f037f8f9 100644 --- a/deployment/charts/backend/templates/deployments/backend-v2.yaml +++ b/deployment/charts/backend/templates/deployments/backend-v2.yaml @@ -39,6 +39,8 @@ spec: env: - name: ENVIRONMENT value: prod + - name: MensaCronDisabled + value: "false" - name: APNS_P8_FILE_PATH value: /etc/apns_auth_key.p8 - name: SENTRY_DSN diff --git a/docker-compose.yaml b/docker-compose.yaml index 3e4f3727..4a4a9174 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -16,8 +16,11 @@ services: - APNS_KEY_ID=${APNS_KEY_ID} - APNS_TEAM_ID=${APNS_TEAM_ID} - APNS_P8_FILE_PATH=${APNS_P8_FILE_PATH} + - MensaCronDisabled=false volumes: + - backend-storage:/Storage - ./apns_auth_key.p8:${APNS_P8_FILE_PATH} + user: 1000:3000 depends_on: db: condition: service_healthy @@ -41,4 +44,6 @@ services: volumes: campus-db-data: + driver: local + backend-storage: driver: local \ No newline at end of file diff --git a/server/Dockerfile b/server/Dockerfile index 1074263e..66da46dd 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -30,6 +30,11 @@ COPY --from=builder /backend /backend COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /etc/passwd /etc/passwd +# make sure that /Storage is mounted with the correct access permissions even in docker-compose +RUN mkdir /Storage +RUN chown appuser /Storage +VOLUME /Storage + # Use an unprivileged user USER appuser diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index fc6858b9..b09fc17a 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -2,6 +2,7 @@ package cron import ( "github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns" + "github.com/TUM-Dev/Campus-Backend/server/env" "time" "github.com/TUM-Dev/Campus-Backend/server/model" @@ -12,10 +13,9 @@ import ( ) type CronService struct { - db *gorm.DB - gf *gofeed.Parser - useMensa bool - APNs *ios_apns.Service + db *gorm.DB + gf *gofeed.Parser + APNs *ios_apns.Service } const StorageDir = "/Storage/" // target location of files @@ -36,21 +36,22 @@ const ( AlarmType = "alarm" */ ) -func New(db *gorm.DB, mensaCronActivated bool) *CronService { +func New(db *gorm.DB) *CronService { return &CronService{ - db: db, - gf: gofeed.NewParser(), - APNs: ios_apns.NewCronService(db), - useMensa: mensaCronActivated, + db: db, + gf: gofeed.NewParser(), + APNs: ios_apns.NewCronService(db), } } func (c *CronService) Run() error { - log.WithField("MensaCronsRunning", c.useMensa).Trace("running cron service") + log.WithField("MensaCronActive", env.IsMensaCronActive()).Debug("running cron service") g := new(errgroup.Group) - g.Go(func() error { return c.dishNameDownloadCron() }) - g.Go(func() error { return c.averageRatingComputation() }) + if env.IsMensaCronActive() { + g.Go(func() error { return c.dishNameDownloadCron() }) + g.Go(func() error { return c.averageRatingComputation() }) + } for { log.Trace("Cron: checking for pending") @@ -72,7 +73,7 @@ func (c *CronService) Run() error { for _, cronjob := range res { // Persist run to DB right away var offset int32 = 0 - if c.useMensa { + if env.IsMensaCronActive() { if cronjob.Type.String == AverageRatingComputation { if time.Now().Hour() == 16 { offset = 18 * 3600 // fast-forward 18 Hours to the next day + does not need to be computed overnight @@ -95,11 +96,11 @@ func (c *CronService) Run() error { case FileDownloadType: g.Go(func() error { return c.fileDownloadCron() }) case DishNameDownload: - if c.useMensa { + if env.IsMensaCronActive() { g.Go(c.dishNameDownloadCron) } case AverageRatingComputation: //call every five minutes between 11AM and 4 PM on weekdays - if c.useMensa { + if env.IsMensaCronActive() { g.Go(c.averageRatingComputation) } /* diff --git a/server/env/environment.go b/server/env/environment.go index cea605a8..0526d478 100644 --- a/server/env/environment.go +++ b/server/env/environment.go @@ -13,3 +13,7 @@ func IsDev() bool { func IsProd() bool { return GetEnvironment() == "prod" } + +func IsMensaCronActive() bool { + return os.Getenv("MensaCronDisabled") != "true" +} diff --git a/server/main.go b/server/main.go index d3eb3997..f6d74a78 100644 --- a/server/main.go +++ b/server/main.go @@ -80,13 +80,8 @@ func main() { return } - var mensaCronActivated = true - if len(os.Args) > 2 && os.Args[1] == "-MensaCron" && os.Args[2] == "0" { - mensaCronActivated = false - log.Info("Cronjobs for the cafeteria rating are deactivated. Remove commandline argument <-MensaCron 0> or set it to 1.", len(os.Args)) - } - // Create any other background services (these shouldn't do any long running work here) - cronService := cron.New(db, mensaCronActivated) + // Create any other background services (these shouldn't do any long-running work here) + cronService := cron.New(db) campusService := backend.New(db) // Listen to our configured ports