refactor: Simplify session management and browser information extraction

This commit is contained in:
Prad Nukala 2024-12-08 19:46:19 -05:00
parent d9f2b21812
commit 38ca7b246b
3 changed files with 81 additions and 82 deletions

2
.gitignore vendored
View File

@ -41,7 +41,7 @@ go.work.sum
# Environment files # Environment files
.env .env
**/*.env **/*.env
sonr.log **/sonr.log
# Terraform # Terraform

View File

@ -1,44 +1,15 @@
package session package session
import ( import (
"net/http" "regexp"
"strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/onsonr/sonr/pkg/common" "github.com/onsonr/sonr/pkg/common"
"github.com/onsonr/sonr/pkg/gateway/internal/database" "github.com/onsonr/sonr/pkg/gateway/internal/database"
"github.com/segmentio/ksuid" "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 // InitSession initializes or loads an existing session
func (s *HTTPContext) InitSession() error { func (s *HTTPContext) InitSession() error {
sessionID := s.getOrCreateSessionID() sessionID := s.getOrCreateSessionID()
@ -81,3 +52,52 @@ func (s *HTTPContext) getOrCreateSessionID() string {
} }
return sessionID 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
}

View File

@ -1,11 +1,10 @@
package session package session
import ( import (
"regexp" "net/http"
"strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/onsonr/sonr/pkg/common" "github.com/onsonr/sonr/pkg/gateway/internal/database"
"gorm.io/gorm" "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) { // HTTPContext is the context for HTTP endpoints.
// Extract all relevant headers type HTTPContext struct {
browserName := common.HeaderRead(c, common.UserAgent) echo.Context
arch := common.HeaderRead(c, common.Architecture) db *gorm.DB
platform := common.HeaderRead(c, common.Platform) sess *database.Session
platformVer := common.HeaderRead(c, common.PlatformVersion) }
model := common.HeaderRead(c, common.Model)
fullVersionList := common.HeaderRead(c, common.FullVersionList) // Get returns the HTTPContext from the echo context
func Get(c echo.Context) (*HTTPContext, error) {
// Default values if headers are empty ctx, ok := c.(*HTTPContext)
if browserName == "" { if !ok {
browserName = "N/A" return nil, echo.NewHTTPError(http.StatusInternalServerError, "Session Context not found")
} }
if arch == "" { return ctx, nil
arch = "unknown" }
}
if platform == "" { // NewHTTPContext creates a new session context
platform = "unknown" func NewHTTPContext(c echo.Context, db *gorm.DB) *HTTPContext {
} return &HTTPContext{
if platformVer == "" { Context: c,
platformVer = "unknown" db: db,
} }
if model == "" { }
model = "unknown"
} // Session returns the current session
func (s *HTTPContext) Session() *database.Session {
// Extract browser version from full version list return s.sess
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
} }