better api

This commit is contained in:
2025-08-17 01:08:22 +02:00
parent 882a6e2c14
commit bbbb760afd
8 changed files with 38 additions and 39 deletions

View File

@@ -16,4 +16,8 @@ DELETE_FILES_AFTER=1d
MAX_STORAGE_GB=10 MAX_STORAGE_GB=10
# Rybbit API Key # Rybbit API Key
ANALYTICS_KEY= ANALYTICS_KEY=
# Rybbit configuration
RYBBIT_API_KEY=
RYBBIT_SITE_ID=

View File

@@ -5,8 +5,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/gin-gonic/gin"
"fitra-backend/utils" "fitra-backend/utils"
"github.com/gin-gonic/gin"
) )
func HandleFileDownload(c *gin.Context) { func HandleFileDownload(c *gin.Context) {
@@ -25,7 +26,7 @@ func HandleFileDownload(c *gin.Context) {
} }
// Send analytics event // Send analytics event
utils.SendAnalyticsEvent("file_download", "/api/download/"+fileID+"/"+filename, "File Download", c.GetHeader("User-Agent"), c.ClientIP()) utils.SendAnalyticsEvent("pageview", "/api/download/"+fileID+"/"+filename)
c.Header("Content-Description", "File Transfer") c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary") c.Header("Content-Transfer-Encoding", "binary")

View File

@@ -9,8 +9,9 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/gin-gonic/gin"
"fitra-backend/utils" "fitra-backend/utils"
"github.com/gin-gonic/gin"
) )
func generateID() (string, error) { func generateID() (string, error) {
@@ -101,7 +102,7 @@ func HandleFileUpload(c *gin.Context) {
} }
// Send analytics event // Send analytics event
utils.SendAnalyticsEvent("file_upload", "/api/upload", "File Upload", c.GetHeader("User-Agent"), c.ClientIP()) utils.SendAnalyticsEvent("pageview", "/api/upload")
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"message": "File uploaded successfully", "message": "File uploaded successfully",

Binary file not shown.

Binary file not shown.

BIN
dist/fitra-linux-amd64 vendored

Binary file not shown.

BIN
dist/fitra-linux-arm64 vendored

Binary file not shown.

View File

@@ -7,52 +7,45 @@ import (
"os" "os"
) )
type AnalyticsEvent struct { type TrackingEvent struct {
APIKey string `json:"api_key"` APIKey string `json:"api_key"`
SiteID string `json:"site_id"` SiteID string `json:"site_id"`
Type string `json:"type"` Type string `json:"type"`
Pathname string `json:"pathname"` Pathname string `json:"pathname"`
Hostname string `json:"hostname"` Hostname string `json:"hostname,omitempty"`
PageTitle string `json:"page_title"` EventName string `json:"event_name,omitempty"`
UserAgent string `json:"user_agent"` Properties string `json:"properties,omitempty"`
IPAddress string `json:"ip_address"`
} }
func SendAnalyticsEvent(eventType, pathname, pageTitle, userAgent, ipAddress string) error { func trackEvent(event TrackingEvent) error {
apiKey := os.Getenv("ANALYTICS_KEY") event.APIKey = os.Getenv("RYBBIT_API_KEY")
if apiKey == "" { event.SiteID = os.Getenv("RYBBIT_SITE_ID")
if event.APIKey == "" {
return nil // Skip if no API key configured return nil // Skip if no API key configured
} }
hostname := os.Getenv("BASE_URL")
if hostname == "" {
hostname = "localhost:8080"
}
event := AnalyticsEvent{
APIKey: apiKey,
SiteID: "fitra-backend",
Type: eventType,
Pathname: pathname,
Hostname: "a.adhd.sh",
PageTitle: pageTitle,
UserAgent: userAgent,
IPAddress: ipAddress,
}
jsonData, err := json.Marshal(event) jsonData, err := json.Marshal(event)
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest("POST", "https://a.adhd.sh/api/track", bytes.NewBuffer(jsonData)) resp, err := http.Post(
"https://a.adhd.sh/api/track",
"application/json",
bytes.NewBuffer(jsonData),
)
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close()
req.Header.Set("Content-Type", "application/json") return nil
}
client := &http.Client{}
_, err = client.Do(req) func SendAnalyticsEvent(eventType, pathname string) error {
return err return trackEvent(TrackingEvent{
Type: eventType,
Pathname: pathname,
})
} }