From 6471f979c1e2cd39870ee8ce2dbd42f57b6e28dc Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 20 Feb 2023 20:37:19 +0100 Subject: [PATCH] added globals --- cnjglobals/cnjglobals.go | 53 ++++++++++++++++++++++++++++++++++++++-- rabbitmqclient.go | 2 -- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/cnjglobals/cnjglobals.go b/cnjglobals/cnjglobals.go index 4505258..5bee500 100644 --- a/cnjglobals/cnjglobals.go +++ b/cnjglobals/cnjglobals.go @@ -1,14 +1,63 @@ package cnjglobals -import "github.com/gofiber/fiber/v2" +import ( + "log" + "regexp" + + "github.com/go-playground/validator/v10" + "github.com/gofiber/fiber/v2" +) const ( RequestHeaderXAuthorization = "X-Authorization" LenUserId = 36 - LenXAuthorizationHeader = 36 // used for api calls + LenUserIdStr = "36" // same as LenUserId but as string + LenXAuthorizationHeader = 36 // used for api calls + LenSessionEncryptionKey = "32" + + MinUsername = "2" + MaxUsername = "24" + MinAccountName = "4" + MaxAccountName = "24" + MinEmail = "3" + MaxEmail = "48" + MinPassword = "6" + MaxPassword = "64" + MinFcmToken = "116" + MaxFcmToken = "164" + + AccountNameRegex = "^[a-zA-Z0-9_.]+$" ) +func init() { + log.Println("init test") +} + +var ( + GeneralRules = map[string]string{ + "Username": "required,min=" + MinUsername + ",max=" + MaxUsername, + "AccountName": "required,min=" + MinAccountName + ",max=" + MaxAccountName + ",regexp=" + AccountNameRegex, + "Email": "required,email,min=" + MinEmail + ",max=" + MaxEmail, + "Password": "required", // password length is checked later because it is sent in base64 format + "UserId": "required,len=" + LenUserIdStr, + "TargetUserId": "required,len=" + LenUserIdStr, + "SessionEncryptionKey": "required,len=" + LenSessionEncryptionKey, + "Token": "required,min=" + MinFcmToken + ",max=" + MaxFcmToken, // fcm token + } +) + +func RegexTag(fl validator.FieldLevel) bool { + field := fl.Field().String() + if field == "" { + return true + } + regexString := fl.Param() + regex := regexp.MustCompile(regexString) + match := regex.MatchString(field) + return match +} + func GetAuhorizationToken(c *fiber.Ctx) string { return c.GetReqHeaders()[RequestHeaderXAuthorization] } diff --git a/rabbitmqclient.go b/rabbitmqclient.go index c256128..4414378 100644 --- a/rabbitmqclient.go +++ b/rabbitmqclient.go @@ -24,8 +24,6 @@ const ( ContentTypeJson = "application/json" SetQueueNameToAutomaticallyAssignedQueueName = "SetQueueNameToAutomaticallyAssignedQueueName" - Test123 = "test1234" - TestHaha = "testhaha" ) type Config struct {