integrated Gorilla session support

This commit is contained in:
2025-09-17 22:50:30 -06:00
parent 522afc33ed
commit 877f364e71
8 changed files with 108 additions and 12 deletions
+38 -2
View File
@@ -15,6 +15,8 @@ import (
"net/http"
"github.com/CloudyKit/jet/v6"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
@@ -23,7 +25,9 @@ type AmContext interface {
RC() int
OutputType() string
Render(string) error
Scratchpad() map[any]any
SubRender(string) ([]byte, error)
Session() *sessions.Session
SetOutputType(string)
SetRC(int)
URLPath() string
@@ -36,6 +40,8 @@ type amContext struct {
httprc int
rendervars jet.VarMap
outputType string
scratchpad map[any]any
session *sessions.Session
}
// RC returns the HTTP result code for the current operation.
@@ -58,6 +64,14 @@ func (c *amContext) Render(name string) error {
return c.echoContext.Render(c.httprc, name, c)
}
// Scratchpad returns the per-request scratchpad for values.
func (c *amContext) Scratchpad() map[any]any {
if c.scratchpad == nil {
c.scratchpad = make(map[any]any)
}
return c.scratchpad
}
/* SubRender renders a subtemplate to the output.
* Parameters:
* name = The name of the template to be rendered.
@@ -75,6 +89,11 @@ func (c *amContext) SubRender(name string) ([]byte, error) {
return buf.Bytes(), err
}
// Session returns the HTTP session.
func (c *amContext) Session() *sessions.Session {
return c.session
}
// SetOutputType sets the MIME output type for the current operation.
func (c *amContext) SetOutputType(typ string) {
c.outputType = typ
@@ -95,21 +114,38 @@ func (c *amContext) VarMap() jet.VarMap {
return c.rendervars
}
// defoptions is the default options for the HTTP session.
var defoptions *sessions.Options = &sessions.Options{
Path: "/",
MaxAge: 86400,
HttpOnly: true,
}
/* NewAmContext creates a new AmContext wrapping the Echo context.
* Parameters:
* ctxt - The Echo context to be wrapped.
* Returns:
* A new Amsterdam context wrapping that context.
* Standard Go error status.
*/
func NewAmContext(ctxt echo.Context) AmContext {
func NewAmContext(ctxt echo.Context) (AmContext, error) {
rc := amContext{
echoContext: ctxt,
httprc: http.StatusOK,
rendervars: make(jet.VarMap),
outputType: "",
scratchpad: nil,
}
ctxt.Set("amsterdam_context", &rc)
return &rc
sess, err := session.Get("amsterdam_session", ctxt)
if err == nil {
rc.session = sess
sess.Options = defoptions
if sess.IsNew {
SetupAmSession(sess)
}
}
return &rc, err
}
/* AmContextFromEchoContext returns the AmContext associated with an Echo context.
+9 -1
View File
@@ -25,9 +25,17 @@ import (
*/
func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
return func(ctxt echo.Context) error {
amctxt := NewAmContext(ctxt)
amctxt, aerr := NewAmContext(ctxt)
if aerr != nil {
ctxt.Logger().Errorf("Session creation error: %v", aerr)
return aerr
}
what, rc, err := myfunc(amctxt)
if err == nil {
if err = amctxt.Session().Save(ctxt.Request(), ctxt.Response()); err != nil {
ctxt.Logger().Errorf("Session save error: %v", err)
return err
}
switch what {
case "bytes":
err = ctxt.Blob(amctxt.RC(), amctxt.OutputType(), rc.([]byte))
+31
View File
@@ -0,0 +1,31 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package ui holds the support for the Amsterdam user interface, wrapping Echo and Jet templates.
package ui
import (
"git.erbosoft.com/amy/amsterdam/config"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
)
// SessionStore is the Gorilla session store used by Amsterdam.
var SessionStore sessions.Store
// SetupSessionManager sets up the session manager.
func SetupSessionManager() {
log.Infof("Cookie key is %s", config.GlobalConfig.Rendering.CookieKey)
SessionStore = sessions.NewCookieStore([]byte(config.GlobalConfig.Rendering.CookieKey))
}
// SetupAmSession sets up a newly created Amsterdam session.
func SetupAmSession(session *sessions.Session) {
session.Values["temp"] = "Active"
}