first commit
This commit is contained in:
78
utils/config.go
Normal file
78
utils/config.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
Password string
|
||||
Database string
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Name string
|
||||
}
|
||||
|
||||
func LoadEnv() error {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load .env file: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func GetEnv(key string, defaultValue string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func DatabaseUrl() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", GetEnv("DB_USERNAME", "postgres"),
|
||||
GetEnv("DB_PASSWORD", "postgres"), GetEnv("DB_HOST", "localhost"), GetEnv("DB_PORT", "5432"), GetEnv("DB_NAME", "postgres"))
|
||||
}
|
||||
|
||||
func RedisUrl() string {
|
||||
return fmt.Sprintf("%s:%s", GetEnv("REDIS_HOST", "localhost"), GetEnv("REDIS_PORT", "6379"))
|
||||
}
|
||||
|
||||
func LoadRedisConfig() (RedisConfig, error) {
|
||||
err := LoadEnv()
|
||||
if err != nil {
|
||||
return RedisConfig{}, err
|
||||
}
|
||||
|
||||
return RedisConfig{
|
||||
Host: GetEnv("REDIS_HOST", "localhost"),
|
||||
Port: GetEnv("REDIS_PORT", "6379"),
|
||||
Password: GetEnv("REDIS_PASSWORD", ""),
|
||||
Database: GetEnv("REDIS_DB", "0"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadDbConfig() (DatabaseConfig, error) {
|
||||
err := LoadEnv()
|
||||
if err != nil {
|
||||
return DatabaseConfig{}, err
|
||||
}
|
||||
|
||||
return DatabaseConfig{
|
||||
Host: GetEnv("POSTGRES_HOST", "localhost"),
|
||||
Port: GetEnv("POSTGRES_PORT", "5432"),
|
||||
Username: GetEnv("POSTGRES_USER", "postgres"),
|
||||
Password: GetEnv("POSTGRES_PASSWORD", "password"),
|
||||
Name: GetEnv("POSTGRES_DB", "ferurl"),
|
||||
}, nil
|
||||
}
|
||||
29
utils/hash.go
Normal file
29
utils/hash.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
func GenerateUrlHash(url string) string {
|
||||
encoded := EncodeToBase62(url)
|
||||
hash := sha256.Sum256([]byte(encoded))
|
||||
|
||||
return fmt.Sprintf("%x", hash)
|
||||
}
|
||||
|
||||
func EncodeToBase62(url string) string {
|
||||
hash := sha256.Sum256([]byte(url))
|
||||
|
||||
encoded := ""
|
||||
for _, b := range hash[:] {
|
||||
encoded += string(base62[int(b)%62])
|
||||
}
|
||||
if len(encoded) > 6 {
|
||||
encoded = encoded[:6]
|
||||
}
|
||||
|
||||
return encoded
|
||||
}
|
||||
25
utils/utils.go
Normal file
25
utils/utils.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func IsValidUrl(s string) bool {
|
||||
if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
|
||||
return false
|
||||
}
|
||||
domainRegex := `^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`
|
||||
parsedUrl, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
matched, err := regexp.MatchString(domainRegex, parsedUrl.Host)
|
||||
if err != nil || !matched {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user