From 38ca7b246bfc995ce65d79aba9b116ee6d76089c Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Sun, 8 Dec 2024 19:46:19 -0500 Subject: [PATCH] refactor: Simplify session management and browser information extraction --- .gitignore | 2 +- pkg/gateway/internal/session/context.go | 82 ++++++++++++++-------- pkg/gateway/internal/session/middleware.go | 79 ++++++++------------- 3 files changed, 81 insertions(+), 82 deletions(-) diff --git a/.gitignore b/.gitignore index 98323dccc..fb5218d21 100644 --- a/.gitignore +++ b/.gitignore @@ -41,7 +41,7 @@ go.work.sum # Environment files .env **/*.env -sonr.log +**/sonr.log # Terraform diff --git a/pkg/gateway/internal/session/context.go b/pkg/gateway/internal/session/context.go index 85d02bba0..ace6e4b15 100644 --- a/pkg/gateway/internal/session/context.go +++ b/pkg/gateway/internal/session/context.go @@ -1,44 +1,15 @@ package session import ( - "net/http" + "regexp" + "strings" "github.com/labstack/echo/v4" "github.com/onsonr/sonr/pkg/common" "github.com/onsonr/sonr/pkg/gateway/internal/database" "github.com/segmentio/ksuid" - "gorm.io/gorm" ) -// HTTPContext is the context for HTTP endpoints. -type HTTPContext struct { - echo.Context - db *gorm.DB - sess *database.Session -} - -// Get returns the HTTPContext from the echo context -func Get(c echo.Context) (*HTTPContext, error) { - ctx, ok := c.(*HTTPContext) - if !ok { - return nil, echo.NewHTTPError(http.StatusInternalServerError, "Session Context not found") - } - return ctx, nil -} - -// NewHTTPContext creates a new session context -func NewHTTPContext(c echo.Context, db *gorm.DB) *HTTPContext { - return &HTTPContext{ - Context: c, - db: db, - } -} - -// Session returns the current session -func (s *HTTPContext) Session() *database.Session { - return s.sess -} - // InitSession initializes or loads an existing session func (s *HTTPContext) InitSession() error { sessionID := s.getOrCreateSessionID() @@ -81,3 +52,52 @@ func (s *HTTPContext) getOrCreateSessionID() string { } return sessionID } + +func extractBrowserInfo(c echo.Context) (string, string, string, string, string, string) { + // Extract all relevant headers + browserName := common.HeaderRead(c, common.UserAgent) + arch := common.HeaderRead(c, common.Architecture) + platform := common.HeaderRead(c, common.Platform) + platformVer := common.HeaderRead(c, common.PlatformVersion) + model := common.HeaderRead(c, common.Model) + fullVersionList := common.HeaderRead(c, common.FullVersionList) + + // Default values if headers are empty + if browserName == "" { + browserName = "N/A" + } + if arch == "" { + arch = "unknown" + } + if platform == "" { + platform = "unknown" + } + if platformVer == "" { + platformVer = "unknown" + } + if model == "" { + model = "unknown" + } + + // Extract browser version from full version list + version := "-1" + if fullVersionList != "" { + entries := strings.Split(strings.TrimSpace(fullVersionList), ",") + for _, entry := range entries { + entry = strings.TrimSpace(entry) + re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`) + matches := re.FindStringSubmatch(entry) + + if len(matches) == 3 { + browserName = matches[1] + version = matches[2] + if browserName != "Not.A/Brand" && + browserName != "Chromium" { + break + } + } + } + } + + return browserName, version, arch, platform, platformVer, model +} diff --git a/pkg/gateway/internal/session/middleware.go b/pkg/gateway/internal/session/middleware.go index 3b4308726..84bff013f 100644 --- a/pkg/gateway/internal/session/middleware.go +++ b/pkg/gateway/internal/session/middleware.go @@ -1,11 +1,10 @@ package session import ( - "regexp" - "strings" + "net/http" "github.com/labstack/echo/v4" - "github.com/onsonr/sonr/pkg/common" + "github.com/onsonr/sonr/pkg/gateway/internal/database" "gorm.io/gorm" ) @@ -22,51 +21,31 @@ func Middleware(db *gorm.DB) echo.MiddlewareFunc { } } -func extractBrowserInfo(c echo.Context) (string, string, string, string, string, string) { - // Extract all relevant headers - browserName := common.HeaderRead(c, common.UserAgent) - arch := common.HeaderRead(c, common.Architecture) - platform := common.HeaderRead(c, common.Platform) - platformVer := common.HeaderRead(c, common.PlatformVersion) - model := common.HeaderRead(c, common.Model) - fullVersionList := common.HeaderRead(c, common.FullVersionList) - - // Default values if headers are empty - if browserName == "" { - browserName = "N/A" - } - if arch == "" { - arch = "unknown" - } - if platform == "" { - platform = "unknown" - } - if platformVer == "" { - platformVer = "unknown" - } - if model == "" { - model = "unknown" - } - - // Extract browser version from full version list - version := "-1" - if fullVersionList != "" { - entries := strings.Split(strings.TrimSpace(fullVersionList), ",") - for _, entry := range entries { - entry = strings.TrimSpace(entry) - re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`) - matches := re.FindStringSubmatch(entry) - - if len(matches) == 3 { - browserName = matches[1] - version = matches[2] - if browserName != "Not.A/Brand" && - browserName != "Chromium" { - break - } - } - } - } - - return browserName, version, arch, platform, platformVer, model +// HTTPContext is the context for HTTP endpoints. +type HTTPContext struct { + echo.Context + db *gorm.DB + sess *database.Session +} + +// Get returns the HTTPContext from the echo context +func Get(c echo.Context) (*HTTPContext, error) { + ctx, ok := c.(*HTTPContext) + if !ok { + return nil, echo.NewHTTPError(http.StatusInternalServerError, "Session Context not found") + } + return ctx, nil +} + +// NewHTTPContext creates a new session context +func NewHTTPContext(c echo.Context, db *gorm.DB) *HTTPContext { + return &HTTPContext{ + Context: c, + db: db, + } +} + +// Session returns the current session +func (s *HTTPContext) Session() *database.Session { + return s.sess }