Add basic Session impl
This commit is contained in:
@@ -2,4 +2,7 @@ module git.gdulai.com/gdulai/rndr
|
|||||||
|
|
||||||
go 1.27.1
|
go 1.27.1
|
||||||
|
|
||||||
require gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31 h1:vLRJpOsFrdXFjRHyhw2BluMslKv63jVvngc5HE/u/xM=
|
gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31 h1:vLRJpOsFrdXFjRHyhw2BluMslKv63jVvngc5HE/u/xM=
|
||||||
gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31/go.mod h1:H7XPunUrSyAvPa9nx8UbKnThEQJDmj3mdt+9ZtqDth4=
|
gitlab.com/gdulai/simpleloglvl v0.0.0-20260811205046-ece654ff6b31/go.mod h1:H7XPunUrSyAvPa9nx8UbKnThEQJDmj3mdt+9ZtqDth4=
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
log "gitlab.com/gdulai/simpleloglvl"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sessions map[uuid.UUID]*Session
|
||||||
|
|
||||||
|
func OpenSession() *Session {
|
||||||
|
newSessId, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
log.LogError("Failed to create new session id: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &Session{sessionID: newSessId}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSession(r *http.Request) *Session {
|
||||||
|
sessionIdCookie, err := r.Cookie("session_id")
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sessId, err := uuid.Parse(sessionIdCookie.Value)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return sessions[sessId]
|
||||||
|
}
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
sessionID uuid.UUID
|
||||||
|
components map[uuid.UUID]any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sess *Session) Close() {
|
||||||
|
delete(sessions, sess.sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sess *Session) MangeComponent(component any) (uuid.UUID, error) {
|
||||||
|
newComponentId, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
return newComponentId, err
|
||||||
|
}
|
||||||
|
sess.components[newComponentId] = component
|
||||||
|
return newComponentId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sess *Session) RemoveComponent(componentId uuid.UUID) {
|
||||||
|
delete(sess.components, componentId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sess *Session) ID() uuid.UUID {
|
||||||
|
return sess.sessionID
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestContext struct {
|
||||||
|
SessionID uuid.UUID
|
||||||
|
RequiestID uuid.UUID
|
||||||
|
Logger *log.GenericLogger
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user