first commit
This commit is contained in:
53
services/email_service.go
Normal file
53
services/email_service.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
configs "ares/config"
|
||||
)
|
||||
|
||||
type EmailService struct{}
|
||||
|
||||
func NewEmailService() *EmailService {
|
||||
return &EmailService{}
|
||||
}
|
||||
|
||||
func (s *EmailService) Send(to, subject, body string) error {
|
||||
host := strings.TrimSpace(configs.AppConfig.EmailHost)
|
||||
port := strings.TrimSpace(configs.AppConfig.EmailPort)
|
||||
from := strings.TrimSpace(configs.AppConfig.EmailFrom)
|
||||
|
||||
if host == "" || port == "" || from == "" {
|
||||
return fmt.Errorf("email configuration is incomplete")
|
||||
}
|
||||
|
||||
addr := host + ":" + port
|
||||
username := strings.TrimSpace(configs.AppConfig.EmailHostUser)
|
||||
password := strings.TrimSpace(configs.AppConfig.EmailHostPassword)
|
||||
|
||||
var auth smtp.Auth
|
||||
if username != "" && password != "" {
|
||||
auth = smtp.PlainAuth("", username, password, host)
|
||||
}
|
||||
|
||||
message := "From: " + from + "\r\n" +
|
||||
"To: " + to + "\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
"MIME-Version: 1.0\r\n" +
|
||||
"Content-Type: text/plain; charset=UTF-8\r\n\r\n" +
|
||||
body
|
||||
|
||||
return smtp.SendMail(addr, auth, from, []string{to}, []byte(message))
|
||||
}
|
||||
|
||||
func (s *EmailService) SendVerificationEmail(to, firstName, verifyURL string) error {
|
||||
subject := "Email verification"
|
||||
body := fmt.Sprintf(
|
||||
"Hi %s,\n\nPlease verify your email by opening this link:\n%s\n\nIf you did not create this account, you can ignore this email.",
|
||||
firstName,
|
||||
verifyURL,
|
||||
)
|
||||
return s.Send(to, subject, body)
|
||||
}
|
||||
238
services/image_service.go
Normal file
238
services/image_service.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
configs "ares/config"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ImageOptions defines the parameters for image processing
|
||||
type ImageOptions struct {
|
||||
Width int
|
||||
Height int
|
||||
Quality int
|
||||
Format string // "avif", "webp", "png", "jpg"
|
||||
Folder string // e.g. "settings", "heroes"
|
||||
}
|
||||
|
||||
// --- Token cache for external image API ---
|
||||
var (
|
||||
imageAPIToken string
|
||||
imageAPITokenExp time.Time
|
||||
imageAPITokenMu sync.Mutex
|
||||
)
|
||||
|
||||
func getImageAPIToken() (string, error) {
|
||||
if apiKey := strings.TrimSpace(configs.AppConfig.ImageAPIKey); apiKey != "" {
|
||||
configs.Logger.Debug("using image API key from config")
|
||||
return apiKey, nil
|
||||
}
|
||||
|
||||
imageAPITokenMu.Lock()
|
||||
defer imageAPITokenMu.Unlock()
|
||||
|
||||
if imageAPIToken != "" && time.Now().Before(imageAPITokenExp) {
|
||||
configs.Logger.Debug("using cached image API token", zap.Time("expires_at", imageAPITokenExp))
|
||||
return imageAPIToken, nil
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(map[string]string{
|
||||
"email": configs.AppConfig.ImageAPIEmail,
|
||||
"password": configs.AppConfig.ImageAPIPassword,
|
||||
})
|
||||
|
||||
resp, err := http.Post(
|
||||
configs.AppConfig.ImageAPIURL+"/api/v1/auth/login",
|
||||
"application/json",
|
||||
bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image API login failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image API login body read error: %v", err)
|
||||
}
|
||||
configs.Logger.Info(
|
||||
"image API login response",
|
||||
zap.Int("status_code", resp.StatusCode),
|
||||
zap.String("body", string(respBody)),
|
||||
)
|
||||
|
||||
var result struct {
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return "", fmt.Errorf("image API login decode error: %v", err)
|
||||
}
|
||||
if result.Data.AccessToken == "" {
|
||||
return "", fmt.Errorf("image API returned empty token")
|
||||
}
|
||||
|
||||
imageAPIToken = result.Data.AccessToken
|
||||
imageAPITokenExp = time.Now().Add(6 * 24 * time.Hour) // 6 days (token valid for 7)
|
||||
return imageAPIToken, nil
|
||||
}
|
||||
|
||||
func callImageAPI(buffer []byte, filename string, opts ImageOptions) (string, error) {
|
||||
token, err := getImageAPIToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
configs.Logger.Debug(
|
||||
"callImageAPI started",
|
||||
zap.String("filename", filename),
|
||||
zap.Int("bytes", len(buffer)),
|
||||
zap.Int("width", opts.Width),
|
||||
zap.Int("height", opts.Height),
|
||||
zap.Int("quality", opts.Quality),
|
||||
zap.String("format", opts.Format),
|
||||
)
|
||||
|
||||
var body bytes.Buffer
|
||||
w := multipart.NewWriter(&body)
|
||||
|
||||
mimeType := http.DetectContentType(buffer)
|
||||
h := make(textproto.MIMEHeader)
|
||||
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, filename))
|
||||
h.Set("Content-Type", mimeType)
|
||||
part, err := w.CreatePart(h)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := part.Write(buffer); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if opts.Width > 0 {
|
||||
_ = w.WriteField("width", strconv.Itoa(opts.Width))
|
||||
}
|
||||
if opts.Height > 0 {
|
||||
_ = w.WriteField("height", strconv.Itoa(opts.Height))
|
||||
}
|
||||
if opts.Quality > 0 {
|
||||
_ = w.WriteField("quality", strconv.Itoa(opts.Quality))
|
||||
}
|
||||
format := strings.ToLower(strings.TrimSpace(opts.Format))
|
||||
if format == "" {
|
||||
format = "avif"
|
||||
}
|
||||
_ = w.WriteField("format", format)
|
||||
w.Close()
|
||||
|
||||
req, err := http.NewRequest("POST", configs.AppConfig.ImageAPIURL+"/api/v1/images/upload", &body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{Timeout: 60 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image API upload failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image API upload body read error: %v", err)
|
||||
}
|
||||
configs.Logger.Info(
|
||||
"image API upload response",
|
||||
zap.Int("status_code", resp.StatusCode),
|
||||
zap.String("body", string(respBody)),
|
||||
)
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return "", fmt.Errorf("image API key gecersiz veya suresi dolmus (HTTP 401)")
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return "", fmt.Errorf("image API upload basarisiz (HTTP %d): %s", resp.StatusCode, strings.TrimSpace(string(respBody)))
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Data struct {
|
||||
Image struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"image"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return "", fmt.Errorf("image API response decode error: %v", err)
|
||||
}
|
||||
if result.Data.Image.URL == "" {
|
||||
return "", fmt.Errorf("image API returned empty URL (HTTP %d)", resp.StatusCode)
|
||||
}
|
||||
return result.Data.Image.URL, nil
|
||||
}
|
||||
|
||||
// ProcessAndSaveImage handles the file upload, processing, and saving
|
||||
func ProcessAndSaveImage(c fiber.Ctx, fieldName string, opts ImageOptions) (string, error) {
|
||||
configs.Logger.Info(
|
||||
"ProcessAndSaveImage called",
|
||||
zap.String("field", fieldName),
|
||||
zap.Int("width", opts.Width),
|
||||
zap.Int("height", opts.Height),
|
||||
zap.Int("quality", opts.Quality),
|
||||
zap.String("format", opts.Format),
|
||||
)
|
||||
|
||||
file, err := c.FormFile(fieldName)
|
||||
if err != nil {
|
||||
configs.Logger.Warn("no file uploaded for field", zap.Error(err), zap.String("field", fieldName))
|
||||
return "", nil
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
configs.Logger.Error("failed to open uploaded file", zap.Error(err), zap.String("filename", file.Filename))
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buffer, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
configs.Logger.Error("failed to read uploaded file bytes", zap.Error(err), zap.String("filename", file.Filename))
|
||||
return "", err
|
||||
}
|
||||
configs.Logger.Debug("read uploaded file bytes", zap.String("filename", file.Filename), zap.Int("bytes", len(buffer)))
|
||||
|
||||
remoteURL, err := callImageAPI(buffer, file.Filename, opts)
|
||||
if err != nil {
|
||||
configs.Logger.Error("image API call failed", zap.Error(err))
|
||||
return "", fmt.Errorf("resim işleme hatası: %v", err)
|
||||
}
|
||||
|
||||
configs.Logger.Info("image saved", zap.String("url", remoteURL), zap.Int("width", opts.Width), zap.Int("height", opts.Height), zap.String("format", opts.Format), zap.Int("quality", opts.Quality))
|
||||
return remoteURL, nil
|
||||
}
|
||||
|
||||
// ProcessAndSaveImageFromBytes processes raw image bytes and saves the file
|
||||
func ProcessAndSaveImageFromBytes(buffer []byte, opts ImageOptions) (string, error) {
|
||||
configs.Logger.Debug("ProcessAndSaveImageFromBytes called", zap.Int("input_bytes", len(buffer)), zap.Any("options", opts))
|
||||
|
||||
remoteURL, err := callImageAPI(buffer, "image.jpg", opts)
|
||||
if err != nil {
|
||||
configs.Logger.Error("image API call failed (from bytes)", zap.Error(err))
|
||||
return "", fmt.Errorf("resim işleme hatası: %v", err)
|
||||
}
|
||||
|
||||
configs.Logger.Info("image saved from bytes", zap.String("url", remoteURL), zap.Int("width", opts.Width), zap.Int("height", opts.Height), zap.String("format", opts.Format), zap.Int("quality", opts.Quality))
|
||||
return remoteURL, nil
|
||||
}
|
||||
144
services/jwt_service.go
Normal file
144
services/jwt_service.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
configs "ares/config"
|
||||
utils "ares/pkg/utis"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
const (
|
||||
TokenTypeAccess = "access"
|
||||
TokenTypeRefresh = "refresh"
|
||||
)
|
||||
|
||||
type JWTClaim struct {
|
||||
UserID uint `json:"user_id"`
|
||||
Email string `json:"email"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
TokenType string `json:"token_type"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
type JWTService struct{}
|
||||
|
||||
func NewJWTService() *JWTService {
|
||||
return &JWTService{}
|
||||
}
|
||||
|
||||
func (s *JWTService) GenerateToken(
|
||||
userID uint,
|
||||
email string,
|
||||
isAdmin bool,
|
||||
firstName string,
|
||||
lastName string,
|
||||
tokenType string,
|
||||
expiration time.Duration,
|
||||
) (string, error) {
|
||||
now := time.Now()
|
||||
|
||||
jti, err := utils.GenerateSecureToken(16)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
claims := &JWTClaim{
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
IsAdmin: isAdmin,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
TokenType: tokenType,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: strconv.FormatUint(uint64(userID), 10),
|
||||
ID: jti,
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(expiration)),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(configs.AppConfig.JWTSecret))
|
||||
}
|
||||
|
||||
func (s *JWTService) GenerateTokenPair(
|
||||
userID uint,
|
||||
email string,
|
||||
isAdmin bool,
|
||||
firstName string,
|
||||
lastName string,
|
||||
) (string, string, error) {
|
||||
access, err := s.GenerateToken(
|
||||
userID,
|
||||
email,
|
||||
isAdmin,
|
||||
firstName,
|
||||
lastName,
|
||||
TokenTypeAccess,
|
||||
time.Duration(configs.AppConfig.AccessTokenExpireMinutes)*time.Minute,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
refresh, err := s.GenerateToken(
|
||||
userID,
|
||||
email,
|
||||
isAdmin,
|
||||
firstName,
|
||||
lastName,
|
||||
TokenTypeRefresh,
|
||||
time.Duration(configs.AppConfig.RefreshTokenExpireDays)*24*time.Hour,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Log generated tokens only in development and prefer project logger if available
|
||||
if configs.AppConfig != nil && configs.AppConfig.Env == "development" {
|
||||
msg := "Generated token pair for user=%d email=%s access_exp=%dm refresh_exp=%dd"
|
||||
if configs.Logger != nil {
|
||||
configs.Logger.Sugar().Debugf(msg, userID, email, configs.AppConfig.AccessTokenExpireMinutes, configs.AppConfig.RefreshTokenExpireDays)
|
||||
configs.Logger.Sugar().Debugf("access (masked): %s", maskToken(access))
|
||||
configs.Logger.Sugar().Debugf("refresh (masked): %s", maskToken(refresh))
|
||||
}
|
||||
}
|
||||
|
||||
return access, refresh, nil
|
||||
}
|
||||
|
||||
// maskToken returns a partially masked representation of a token string
|
||||
// to avoid logging full sensitive values.
|
||||
func maskToken(token string) string {
|
||||
if len(token) <= 10 {
|
||||
return "****"
|
||||
}
|
||||
prefix := token[:6]
|
||||
suffix := token[len(token)-4:]
|
||||
return prefix + "..." + suffix
|
||||
}
|
||||
|
||||
func (s *JWTService) ValidateToken(signedToken string) (*JWTClaim, error) {
|
||||
token, err := jwt.ParseWithClaims(signedToken, &JWTClaim{}, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, errors.New("unexpected signing method")
|
||||
}
|
||||
return []byte(configs.AppConfig.JWTSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*JWTClaim)
|
||||
if !ok || !token.Valid {
|
||||
return nil, errors.New("invalid token claims")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Reference in New Issue
Block a user