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
.env
**/*.env
sonr.log
**/sonr.log
# Terraform

View File

@ -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
}

View File

@ -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
}