Files
account-manager/main.go
2026-04-02 01:55:44 -04:00

78 lines
1.8 KiB
Go

package main
import (
"crypto/rand"
"errors"
"libshared"
"log"
"math/big"
"net/http"
"regexp"
"strings"
)
type NewAccountRequest struct {
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Address string `json:"address"`
CountryCode string `json:"country_code"`
Password string `json:"password"`
}
type APIResponse struct {
Success bool `json:"success"`
AccountID int64 `json:"account_id,omitempty"`
Error string `json:"error,omitempty"`
}
func generateSecureNumber(digits int) (int64, error) {
upperBound := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(digits)), nil)
n, err := rand.Int(rand.Reader, upperBound)
if err != nil {
return 0, err
}
return n.Int64(), nil
}
func validateRequest(req *NewAccountRequest) error {
if strings.TrimSpace(req.Email) == "" {
return errors.New("email is required")
}
if strings.TrimSpace(req.FirstName) == "" {
return errors.New("first_name is required")
}
if strings.TrimSpace(req.LastName) == "" {
return errors.New("last_name is required")
}
if strings.TrimSpace(req.Address) == "" {
return errors.New("address is required")
}
if strings.TrimSpace(req.CountryCode) == "" {
return errors.New("country_code is required")
}
// Basic email validation
emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
if !emailRegex.MatchString(req.Email) {
return errors.New("invalid email format")
}
// Optional: enforce ISO country code length (2 letters)
if len(req.CountryCode) != 2 {
return errors.New("country_code must be 2 characters (ISO code)")
}
return nil
}
func main() {
libshared.Pool = libshared.GetDbPool()
http.HandleFunc("/account/new", accountNew)
log.Println("Server running on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}