diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml new file mode 120000 index 0000000..03a5712 --- /dev/null +++ b/.forgejo/workflows/build.yaml @@ -0,0 +1 @@ +../../build/ci/build.yaml \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8d92b4e..f6a6737 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -17,7 +17,7 @@ { "label": "run project", "args": ["run", "./cmd/stbackend/main.go"], - "group":"build", + "group": "build", "options":{ "env":{ "DATABASE_URL": "test", @@ -26,6 +26,11 @@ } }, }, + { + "label": "build project", + "args": ["build", "-x", "-v", "-o=stbackend", "-buildvcs=true", "./cmd/stbackend/main.go"], + "group": "build" + }, { "label": "test", "args": ["test", "-v", "./..."], diff --git a/build/ci/build.yaml b/build/ci/build.yaml new file mode 100644 index 0000000..37b7b18 --- /dev/null +++ b/build/ci/build.yaml @@ -0,0 +1,12 @@ +on: [push] + +jobs: + build: + runs-on: docker + steps: + - run: go build -x -v -o=stbackend -buildvcs=true ./cmd/stbackend/main. + + - uses: actions/upload-artifact@v3 + with: + name: build-artifact + path: ./stbackend \ No newline at end of file diff --git a/internal/api/handlers/schedule_handler.go b/internal/api/handlers/schedule_handler.go new file mode 100644 index 0000000..f187cad --- /dev/null +++ b/internal/api/handlers/schedule_handler.go @@ -0,0 +1,43 @@ +/* +ScheduleTogether Backend +Copyright (C) 2024, Zervó Zadachin + +DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License version 3 +as published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License version 3 for more details. + +This program incorporates external libraries for certain functionalities. +These libraries are covered by their respective licenses, and their usage +agreements are as outlined in their respective documentation or source +code. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +*/ + +package handlers + +import ( + "github.com/kataras/iris/v12" + + "git.zervo.org/scheduletogether/backend/internal/api/middlewares" + perms "git.zervo.org/scheduletogether/backend/pkg/permissions" +) + +// ScheduleHandler_RegisterRoutes registers routes for the ScheduleHandler +func ScheduleHandler_RegisterRoutes(party iris.Party) { + party.Use(middlewares.Authenticate()) // only allow authenticated users for the following endpoints + party.Post("/", middlewares.Authorize(perms.CreateSchedule)) +} + +// ScheduleCreate_Handler handles the POST /schedules endpoint +func ScheduleCreate_Handler(ctx iris.Context) { + +} diff --git a/internal/api/services/users/users_get.go b/internal/api/services/users/users_get.go index 55d8869..94c9e06 100755 --- a/internal/api/services/users/users_get.go +++ b/internal/api/services/users/users_get.go @@ -38,7 +38,7 @@ func UsersGet_Service(ctx iris.Context) { if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ - Error: "Failed to retrieve users from the database", + Error: "Failed to retrieve users", }) logboi.Warn("Failed to retrieve users from the database: " + err.Error()) return diff --git a/internal/api/services/users/users_getbyid.go b/internal/api/services/users/users_getbyid.go index f783842..b23fee0 100755 --- a/internal/api/services/users/users_getbyid.go +++ b/internal/api/services/users/users_getbyid.go @@ -49,7 +49,7 @@ func UsersGetById_Service(ctx iris.Context) { if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ - Error: "Failed to retrieve user from the database", + Error: "Failed to retrieve user", }) logboi.Warn("Failed to retrieve user from the database: " + err.Error()) return diff --git a/internal/api/services/users/users_getbyuuid.go b/internal/api/services/users/users_getbyuuid.go index 249102e..d167ab0 100755 --- a/internal/api/services/users/users_getbyuuid.go +++ b/internal/api/services/users/users_getbyuuid.go @@ -49,7 +49,7 @@ func UsersGetByUuid_Service(ctx iris.Context) { if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ - Error: "Failed to retrieve user from the database", + Error: "Failed to retrieve user", }) logboi.Warn("Failed to retrieve user from the database: " + err.Error()) return diff --git a/internal/api/services/users/users_getself.go b/internal/api/services/users/users_getself.go index 1b0ed24..d774d83 100755 --- a/internal/api/services/users/users_getself.go +++ b/internal/api/services/users/users_getself.go @@ -44,12 +44,12 @@ func UsersGetSelf_Service(ctx iris.Context) { if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ - Error: "Failed to retrieve user from the database", + Error: "Failed to retrieve user", }) logboi.Warn("Failed to retrieve user from the database: " + err.Error()) return } - // Return users + // Return user ctx.JSON(user) } diff --git a/internal/workers/coordinator.go b/internal/workers/coordinator.go index 8ca711f..f353ddb 100755 --- a/internal/workers/coordinator.go +++ b/internal/workers/coordinator.go @@ -61,7 +61,7 @@ func (l *Reporter) Fail(msg string) { // Finish wraps logboi.Info and announces end of a job func (l *Reporter) Finish() { logboi.Info("<" + l.workerName + "/" + l.jobName + "> " + "[ " + logging.Green + logging.Bold + "OK " + logging.Reset + " ]") - logboi.Info("Job took \"" + l.jobName + "\" " + time.Since(l.startTime).String() + " to complete.") + logboi.Info("Job \"" + l.jobName + "\" " + " took " + time.Since(l.startTime).String() + " to complete.") } // Schedule workers with cron @@ -71,7 +71,7 @@ func ScheduleWorkers() { // Add functions / jobs c.AddFunc("@every 30m", workerFriends_PurgeAbandonedRequests) - c.AddFunc("@every 30m", workerSchedules_PurgeAbandonedData) + c.AddFunc("@every 60m", workerSchedules_PurgeAbandonedData) c.Start() logboi.Info("Worker jobs scheduled") diff --git a/pkg/helpers/cryptography/cryptography.go b/pkg/helpers/cryptography/cryptography.go index a651f14..579fb8e 100755 --- a/pkg/helpers/cryptography/cryptography.go +++ b/pkg/helpers/cryptography/cryptography.go @@ -34,11 +34,11 @@ import ( ) var logboi = logging.NewLogger("crypto") -var lettersAndNumbers = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") +var tokenComponents = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") // HashString hashes a string func HashString(raw string) (string, error) { - hashed, err := bcrypt.GenerateFromPassword([]byte(raw), bcrypt.DefaultCost) // replace with safer seed? + hashed, err := bcrypt.GenerateFromPassword([]byte(raw), bcrypt.DefaultCost) // TODO: replace with safer seed? if err != nil { return "", err } @@ -54,7 +54,7 @@ func GenerateToken(length int) (string, error) { } for k, v := range bytes { - bytes[k] = lettersAndNumbers[v%byte(len(lettersAndNumbers))] + bytes[k] = tokenComponents[v%byte(len(tokenComponents))] } return string(bytes), nil diff --git a/pkg/permissions/core.go b/pkg/permissions/core.go index feca93f..87ca045 100755 --- a/pkg/permissions/core.go +++ b/pkg/permissions/core.go @@ -19,6 +19,7 @@ const ( GetOwnFriendRequests GetFriendRequestsById OverrideInteractionPreferences + CreateSchedule ) var Permissions = map[types.PermissionKey]types.Permission{ @@ -106,4 +107,10 @@ var Permissions = map[types.PermissionKey]types.Permission{ Description: "Allows user to interact with other users no matter their interaction preferences", Roles: []string{"admin"}, }, + CreateSchedule: { + Key: "create_schedule", + Name: "Create Schedule", + Description: "Allows user to create a schedule on their account", + Roles: []string{"admin", "user", "member"}, + }, }