forked from mtusi2026/mtusi2026lab3
Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json/v2"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func encodeCursor[C any](cursor C) (*string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encoded := base64.RawURLEncoding.EncodeToString(data)
|
||||
|
||||
return &encoded, nil
|
||||
|
||||
}
|
||||
|
||||
func decodeCursor[C any](cursor string) (*C, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
var decoded C
|
||||
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
return &decoded, nil
|
||||
}
|
||||
|
||||
var usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func checkUsername(username string) bool {
|
||||
return usernameRegex.MatchString(username)
|
||||
}
|
||||
|
||||
func checkPassword(password string) bool {
|
||||
|
||||
if !utf8.ValidString(password) {
|
||||
return false
|
||||
}
|
||||
|
||||
if strings.TrimSpace(password) == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
passwordLength := utf8.RuneCountInString(password)
|
||||
if passwordLength < 12 || passwordLength > 128 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user