utils

package module
v0.0.0-...-d236baf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 32 Imported by: 27

README

stablecog/sc-go/utils

Various shared helpers and utilities used by the other modules in this workspace.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	UsernameLengthError           = errors.New("username_length_error")
	UsernameStartsWithLetterError = errors.New("username_must_start_with_a_letter")
	UsernameCharError             = errors.New("username_can_only_contain_letters_numbers_hyphens")
	UsernameProfaneError          = errors.New("username_profanity")
	UsernameBlacklistedError      = errors.New("username_blacklisted")
)

Validate username

View Source
var AmountAmbiguousError = fmt.Errorf("amount_ambiguous")

Extract integer from a string (ie. !tip 500 @bbedward -> 500)

View Source
var AmountMissingError = fmt.Errorf("amount_not_found")
View Source
var AmountNotIntegerError = fmt.Errorf("amount_not_integer")
View Source
var RuntimeCaller = runtime.Caller

Functions

func AddQueryParam

func AddQueryParam(rawURL string, queryParam ...QueryParam) (string, error)

func CalculateVoiceoverCredits

func CalculateVoiceoverCredits(prompt string) int32

func EnsureTrailingSlash

func EnsureTrailingSlash(s string) string

Ensure trailing slash in string

func ExtractAmountsFromString

func ExtractAmountsFromString(str string) (int, error)

Not actually using regex here, but it's a good place to put this function

func FormatPrompt

func FormatPrompt(s string) string

FormatPrompt applies formatting to a prompt string e.g. " hello world " -> "hello world"

func GenerateRandomHex

func GenerateRandomHex(rand io.Reader, nBytes int) (string, error)

Generates random N bytes and returns them as a hex string

func GenerateUsername

func GenerateUsername(randReader RandReader) string

func GetCountryCode

func GetCountryCode(r *http.Request) string

Retrieves a country code via request header, prefer cloudflare, then vercel

func GetIPAddress

func GetIPAddress(r *http.Request) string

Get a clients real user IP Address

func GetImageSizeFromUrl

func GetImageSizeFromUrl(imageUrl string) (bytes int64, err error)

Retrieves the download size of an image via headers

func GetImageWidthHeightFromUrl

func GetImageWidthHeightFromUrl(imageUrl string, headUrl string, maxSizeBytes int64) (width, height int32, err error)

func GetPathFromS3URL

func GetPathFromS3URL(s3UrlStr string) (string, error)

func GetThumbmarkID

func GetThumbmarkID(r *http.Request) string

func IsSha256Hash

func IsSha256Hash(str string) bool

func IsValidHTTPURL

func IsValidHTTPURL(urlStr string) bool

Validates that a URL is valid with http or https scheme

func IsValidUsername

func IsValidUsername(username string) error

func Max

func Max[T constraints.Ordered](a, b T) T

Define the Max function that works for any type T that is comparable.

func NormalizeEmail

func NormalizeEmail(email string) string

func ParseIsoTime

func ParseIsoTime(isoTime string) (time.Time, error)

Parse an iso string into a time.Time e.g. 2023-01-27T14:40:53.858Z represents javascript toISOString()

func RelativeTimeStr

func RelativeTimeStr(t time.Time) string

Create a relative time string from now to past (e.g., "1h ago")

func RemoveLineBreaks

func RemoveLineBreaks(s string) string

RemoveLineBreaks removes all line breaks from a string e.g. "hello\nworld" -> "hello world"

func RemovePlusFromEmail

func RemovePlusFromEmail(email string) string

Remove + from email addresses

func RemoveRedundantSpaces

func RemoveRedundantSpaces(s string) string

RemoveRedundantSpaces removes all redundant spaces from a string e.g. " hello world " -> " hello world "

func RootDir

func RootDir() string

Returns the root directory of the project

func SecondsSinceEpochToTime

func SecondsSinceEpochToTime(seconds int64) time.Time

func Sha256

func Sha256(s string) string

func TimeToIsoString

func TimeToIsoString(ts time.Time) string

func ToPtr

func ToPtr[T interface{}](value T) *T

Convert value to pointer with generic interface

Types

type AESCrypt

type AESCrypt struct {
	SecretKey string
}

func NewAesCrypt

func NewAesCrypt(key string) *AESCrypt

func (*AESCrypt) Decrypt

func (a *AESCrypt) Decrypt(encryptedString string) (string, error)

func (*AESCrypt) Encrypt

func (a *AESCrypt) Encrypt(input string) (string, error)

type ClientDeviceInfo

type ClientDeviceInfo struct {
	DeviceType           ClientDeviceType `json:"device_type"`
	DeviceOs             string           `json:"device_os"`
	DeviceBrowser        string           `json:"device_browser"`
	DeviceBrowserVersion string           `json:"device_browser_version"`
}

func GetClientDeviceInfo

func GetClientDeviceInfo(r *http.Request) ClientDeviceInfo

type ClientDeviceType

type ClientDeviceType string

Parses user agent to return device type, os, and browser

const (
	Desktop ClientDeviceType = "desktop"
	Mobile  ClientDeviceType = "mobile"
	Tablet  ClientDeviceType = "tablet"
	Bot     ClientDeviceType = "bot"
	Unknown ClientDeviceType = "unknown"
)

type CryptoRandReader

type CryptoRandReader struct{}

func (*CryptoRandReader) Read

func (crr *CryptoRandReader) Read(b []byte) (int, error)

type QueryParam

type QueryParam struct {
	Key   string
	Value string
}

type RandReader

type RandReader interface {
	Read([]byte) (int, error)
}

type SCEnv

type SCEnv struct {
	Production    bool   `env:"PRODUCTION" envDefault:"false"`
	Port          int    `env:"PORT" envDefault:"8000"`
	PublicApiUrl  string `env:"PUBLIC_API_URL" envDefault:"http://localhost:8000"` // Used for thing such as, building the webhook URL for sc-worker to send results to
	PrivateApiUrl string `env:"PRIVATE_API_URL" envDefault:"http://localhost:13337"`
	// Content moderation and translator
	OpenAIApiKey        string `env:"OPENAI_API_KEY"`
	PrivateLinguaAPIUrl string `env:"PRIVATE_LINGUA_API_URL"` // Corresponds to sc-go/language server
	// Shared secret between sc-worker and sc-server
	ScWorkerWebhookSecret string `env:"SC_WORKER_WEBHOOK_SECRET" envDefault:"invalid"`
	// Whether to run DB migrations on startup, can only be done in the local environment (not on a supabase database)
	RunMigrations bool `env:"RUN_MIGRATIONS" envDefault:"false"`
	// CDN URLs for assets
	BucketBaseUrl     string `env:"BUCKET_BASE_URL" envDefault:"https://b.stablecog.com/"` // Public CDN URL
	BucketVoiceverUrl string `env:"BUCKET_VOICEOVER_URL" envDefault:"https://bvoi.stablecog.com/"`
	// Stripe
	StripeSecretKey                 string `env:"STRIPE_SECRET_KEY"`                  // required
	StripeEndpointSecret            string `env:"STRIPE_ENDPOINT_SECRET"`             // Required for stripe webhooks
	StripeWebhookSubscriptionSecret string `env:"STRIPE_WEBHOOK_SUBSCRIPTION_SECRET"` // Required for stripe webhooks
	// Stripe subscription price + product IDs
	StripeUltimatePriceID   string `env:"STRIPE_ULTIMATE_PRICE_ID" envDefault:"price_1Mf591ATa0ehBYTA6ggpEEkA"`
	StripeProPriceID        string `env:"STRIPE_PRO_PRICE_ID" envDefault:"price_1Mf50bATa0ehBYTAPOcfnOjG"`
	StripeStarterPriceID    string `env:"STRIPE_STARTER_PRICE_ID" envDefault:"price_1Mf56NATa0ehBYTAHkCUablG"`
	StripeUltimateProductID string `env:"STRIPE_ULTIMATE_PRODUCT_ID" envDefault:"prod_NTzE0C8bEuIv6F"`
	StripeProProductID      string `env:"STRIPE_PRO_PRODUCT_ID" envDefault:"prod_NTzCojAHPw6tbX"`
	StripeStarterProductID  string `env:"STRIPE_STARTER_PRODUCT_ID" envDefault:"prod_NPuwbni7ZNkHDO"`
	// Annual price IDss
	StripeUltimateAnnualPriceID string `env:"STRIPE_ULTIMATE_ANNUAL_PRICE_ID" envDefault:"price_1Mf5BjATa0ehBYTA5Z3Z2Z6v"`
	StripeProAnnualPriceID      string `env:"STRIPE_PRO_ANNUAL_PRICE_ID" envDefault:"price_1Mf5BjATa0ehBYTA5Z3Z2Z6v"`
	StripeStarterAnnualPriceID  string `env:"STRIPE_STARTER_ANNUAL_PRICE_ID" envDefault:"price_1Mf5BjATa0ehBYTA5Z3Z2Z6v"`
	// Stripe ad-hoc purchase price + product IDs
	StripeLargePackPriceID    string `env:"STRIPE_LARGE_PACK_PRICE_ID" envDefault:"1"`
	StripeMediumPackPriceID   string `env:"STRIPE_MEDIUM_PACK_PRICE_ID" envDefault:"2"`
	StripeMegaPackPriceID     string `env:"STRIPE_MEGA_PACK_PRICE_ID" envDefault:"3"`
	StripeLargePackProductID  string `env:"STRIPE_LARGE_PACK_PRODUCT_ID" envDefault:"1"`
	StripeMediumPackProductID string `env:"STRIPE_MEDIUM_PACK_PRODUCT_ID" envDefault:"2"`
	StripeMegaPackProductID   string `env:"STRIPE_MEGA_PACK_PRODUCT_ID" envDefault:"3"`
	// Discord webhooks
	DiscordWebhookUrl          string `env:"DISCORD_WEBHOOK_URL"`           // For health notifications in cron, Optional
	DiscordWebhookUrlDeploy    string `env:"DISCORD_WEBHOOK_URL_DEPLOY"`    // For deploy notifications in server, Optional
	DiscordWebhookUrlNewSub    string `env:"DISCORD_WEBHOOK_URL_NEWSUB"`    // For new sub notifications in server, Optional
	DiscordWebhookUrlUserclean string `env:"DISCORD_WEBHOOK_URL_USERCLEAN"` // Sent when cron wipes users or has an error
	GeoIpWebhook               string `env:"GEOIP_WEBHOOK"`                 // For geoip notifications in server, Optional
	// Whether running in github actions, basically whether to use Postgres or not in tests (will use SQLite if false)
	GithubActions bool `env:"GITHUB_ACTIONS" envDefault:"false"` // Whether we're running in Github Actions
	// PostgreSQL
	PostgresDB       string `env:"POSTGRES_DB" envDefault:"postgres"`    // Postgres DB Name
	PostgresUser     string `env:"POSTGRES_USER" envDefault:"postgres"`  // Postgres DB User
	PostgresPassword string `env:"POSTGRES_PASSWORD"`                    // Postgres DB Password, required
	PostgresHost     string `env:"POSTGRES_HOST" envDefault:"127.0.0.1"` // Postgres DB Host
	PostgresPort     int    `env:"POSTGRES_PORT" envDefault:"5432"`      // Postgres DB Port
	// Redis
	RedisConnectionString string `env:"REDIS_CONNECTION_STRING" envDefault:"redis://localhost:6379/0"` // Redis connection string, required
	MockRedis             bool   `env:"MOCK_REDIS" envDefault:"false"`                                 // Whether to mock redis for tests
	// Qdrant
	QdrantUrl            string `env:"QDRANT_URL"`                                    // Qdrant URL, required
	QdrantUsername       string `env:"QDRANT_USERNAME"`                               // Qdrant Username, Optional
	QdrantPassword       string `env:"QDRANT_PASSWORD"`                               // Qdrant Password, Optional
	QdrantCollectionName string `env:"QDRANT_COLLECTION_NAME" envDefault:"stablecog"` // Qdrant Collection Name
	// Supabase
	PublicSupabaseReferenceID string `env:"PUBLIC_SUPABASE_REFERENCE_ID"` // Supabase reference ID, required
	SupabaseAdminKey          string `env:"SUPABASE_ADMIN_KEY"`           // Supabase admin key, required
	GotrueURL                 string `env:"GOTRUE_URL"`                   // Gotrue URL, Optional (Only for self-hosted supabase)
	// RabbitMQ
	RabbitMQQueueName string `env:"RABBITMQ_QUEUE_NAME" envDefault:"TEST.Q"`                           // RabbitMQ queue name
	RabbitMQAMQPUrl   string `env:"RABBITMQ_AMQP_URL" envDefault:"amqp://guest:guest@localhost:5672/"` // RabbitMQ AMQP URL
	// S3 img2img bucket, for user-uploaded images
	S3Img2ImgRegion     string `env:"S3_IMG2IMG_REGION" envDefault:"us-east-1"`    // S3 region
	S3Img2ImgBucketName string `env:"S3_IMG2IMG_BUCKET_NAME" envDefault:"img2img"` // S3 bucket
	S3Img2ImgAccessKey  string `env:"S3_IMG2IMG_ACCESS_KEY" envDefault:""`         // S3 access key
	S3Img2ImgSecretKey  string `env:"S3_IMG2IMG_SECRET_KEY" envDefault:""`         // S3 secret key
	S3Img2ImgEndpoint   string `env:"S3_IMG2IMG_ENDPOINT" envDefault:""`           // S3 endpoint
	// S3 bucket, for all sc-worker uploaded assets. Primarily used for deleting user data in cron CLI
	S3Region     string `env:"S3_REGION" envDefault:"us-east-1"`      // S3 region
	S3BucketName string `env:"S3_BUCKET_NAME" envDefault:"stablecog"` // S3 bucket
	S3AccessKey  string `env:"S3_ACCESS_KEY"`                         // S3 access key
	S3SecretKey  string `env:"S3_SECRET_KEY"`
	S3Endpoint   string `env:"S3_ENDPOINT"` // S3 endpoint
	// Analytics (Optional group)
	PosthogApiKey   string `env:"POSTHOG_API_KEY"`  // Posthog API Key, Optional
	PosthogEndpoint string `env:"POSTHOG_ENDPOINT"` // Posthog Endpoint, Optional
	// Discord bot
	DiscordBotToken        string `env:"DISCORD_BOT_TOKEN"`                        // Discord bot token, only required for discobot
	DiscordUserIdsToNotify string `env:"DISCORD_USER_IDS_TO_NOTIFY" envDefault:""` // Discord user IDs to notify
	// Oauth service, used by services such as Raycast
	DataEncryptionPassword string `env:"DATA_ENCRYPTION_PASSWORD" envDefault:"insecurePassword"` // Data encryption password
	OauthRedirectBase      string `env:"OAUTH_REDIRECT_BASE" envDefault:"http://localhost:3000"` // Oauth redirect base
	// Og Service Token for bypassing rate limits
	OgPreviewServiceToken string `env:"OG_PREVIEW_SERVICE_TOKEN" envDefault:""` // Og Service Token for preview service
	// Vast.ai API Key
	VastAiKey string `env:"VASTAI_KEY"` // Vast.ai Key
	// SC Worker Tester API Key
	ScWorkerTesterApiKey string `env:"SC_WORKER_TESTER_API_KEY" envDefault:""` // SC Worker Tester API Key
	ClipApiUrl           string `env:"CLIP_API_URL"`                           // Clip API URL
	ClipApiAuthToken     string `env:"CLIP_API_AUTH_TOKEN"`                    // Clip API Auth Token
	// Runpod Serverless
	RunpodApiToken string `env:"RUNPOD_API_TOKEN"` // Runpod API Token
}

func GetEnv

func GetEnv() *SCEnv

GetEnv provides a thread-safe way to get the environment.

func (*SCEnv) GetCorsOrigins

func (e *SCEnv) GetCorsOrigins() []string

func (*SCEnv) GetDiscordUserIdsToNotify

func (e *SCEnv) GetDiscordUserIdsToNotify() []string

func (*SCEnv) GetURLFromAudioFilePath

func (e *SCEnv) GetURLFromAudioFilePath(s3UrlStr string) string

func (*SCEnv) GetURLFromImagePath

func (e *SCEnv) GetURLFromImagePath(s3UrlStr string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL