migration to chi

This commit is contained in:
2025-03-12 16:30:40 +03:00
parent ae81fbcce9
commit 45bb32e7a6
46 changed files with 1352 additions and 1323 deletions
+31
View File
@@ -0,0 +1,31 @@
package utils
import (
"bytes"
"encoding/json"
"net/http"
"time"
)
type Embed struct {
Title string `json:"title"`
Type string `json:"type"`
Description string `json:"description"`
URL string `json:"url"`
Timestamp time.Time `json:"timestamp"`
Color uint32 `json:"color"`
}
type WebhookContent struct {
Content string `json:"content"`
Embeds []Embed `json:"embeds"`
}
func ExecuteWebhook(url string, content WebhookContent) error {
body, err := json.Marshal(content)
if err != nil {
return err
}
_, err = http.Post(url, "application/json", bytes.NewReader(body))
return err
}
+27
View File
@@ -0,0 +1,27 @@
package utils
import (
"os"
)
func GetFrontendURL() string {
if IsDevelopment() {
return os.Getenv("DEV_FRONTEND_URL")
}
return os.Getenv("FRONTEND_URL")
}
func IsDevelopment() bool {
return os.Getenv("DEVELOPMENT") == "true"
}
func IsDebug() bool {
return os.Getenv("DEBUG") == "true"
}
func Getenv(key, def string) string {
value := os.Getenv(key)
if value == "" {
return def
}
return value
}
+46
View File
@@ -0,0 +1,46 @@
package utils
import (
"bytes"
"fmt"
"html/template"
"log"
"net/smtp"
"os"
)
const (
format = "From: %v\r\n" +
"To: %v\r\n" +
"Subject: %v\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n" +
"MIME-version: 1.0;\r\n\r\n" +
"%v\r\n"
)
var mailAuth = smtp.PlainAuth("", os.Getenv("MAIL_USERNAME"), os.Getenv("MAIL_PASSWORD"), os.Getenv("MAIL_HOST"))
func SendMail(to string, subject string, body string) {
msg := []byte(fmt.Sprintf(format, os.Getenv("MAIL_USERNAME"), to, subject, body))
err := smtp.SendMail(os.Getenv("MAIL_HOST")+":587", mailAuth, os.Getenv("MAIL_USERNAME"), []string{to}, msg)
if err != nil {
log.Println(err)
}
}
func SendMailTemplate(to string, subject string, name string, data interface{}) {
wd, err := os.Getwd()
if err != nil {
log.Println(err)
}
t, err := template.ParseFiles(wd + `/mail_templates/` + name)
if err != nil {
log.Println(err)
}
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
log.Println(err)
}
body := buf.String()
SendMail(to, subject, body)
}
+72
View File
@@ -0,0 +1,72 @@
package utils
import (
"crypto/md5"
"encoding/hex"
"reflect"
"strings"
)
func StringOrDefault(a, b string) string {
if len(a) == 0 {
return b
}
return a
}
func GetStructForDatabase(s interface{}) []string {
out := make([]string, 0)
el := reflect.ValueOf(s).Elem()
for i := 0; i < el.NumField(); i++ {
t := el.Type().Field(i)
tag := t.Tag
db := tag.Get("db")
if len(db) == 0 {
out = append(out, strings.ToLower(t.Name))
} else {
out = append(out, db)
}
}
return out
}
func GetNamedForDatabase(s interface{}) []string {
out := make([]string, 0)
el := reflect.ValueOf(s).Elem()
for i := 0; i < el.NumField(); i++ {
t := el.Type().Field(i)
tag := t.Tag
db := tag.Get("db")
if len(db) == 0 {
out = append(out, ":"+strings.ToLower(t.Name))
} else {
out = append(out, ":"+db)
}
}
return out
}
func GetStructAndNamedForDatabase(s interface{}) [][]string {
out := make([][]string, 0)
el := reflect.ValueOf(s).Elem()
for i := 0; i < el.NumField(); i++ {
t := el.Type().Field(i)
tag := t.Tag
ignore := tag.Get("ignore")
if ignore != "1" {
db := tag.Get("db")
if len(db) == 0 {
out = append(out, []string{strings.ToLower(t.Name), ":" + strings.ToLower(t.Name)})
} else {
out = append(out, []string{db, ":" + db})
}
}
}
return out
}
func MD5(s string) string {
hash := md5.New()
hash.Write([]byte(s))
return hex.EncodeToString(hash.Sum(nil))
}