From 877f364e71224ca8b298cb31f0e63372f5f9d830 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Wed, 17 Sep 2025 22:50:30 -0600 Subject: [PATCH] integrated Gorilla session support --- config/config.go | 19 +++++++++++-------- config/default.yaml | 1 + go.mod | 4 ++++ go.sum | 8 ++++++++ main.go | 7 ++++++- ui/amcontext.go | 40 ++++++++++++++++++++++++++++++++++++++-- ui/render_wrap.go | 10 +++++++++- ui/session_mgr.go | 31 +++++++++++++++++++++++++++++++ 8 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 ui/session_mgr.go diff --git a/config/config.go b/config/config.go index 43b81ed..af8e475 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ import ( "os" argparse "github.com/alexflint/go-arg" + "github.com/labstack/gommon/log" "gopkg.in/yaml.v3" ) @@ -27,6 +28,9 @@ type AmCLI struct { ConfigFile string `arg:"-C,--config" help:"Location of the configuration file."` } +// CommandLine is the command-line arguments passed to Amsterdam. +var CommandLine AmCLI + // Description (from argparse.Described) returns the description string for the application. func (*AmCLI) Description() string { return "Amsterdam Web Communities System Server" @@ -44,6 +48,7 @@ type AmConfig struct { } `yaml:"site"` Rendering struct { TemplateDir string `yaml:"templatedir"` + CookieKey string `yaml:"cookiekey"` } `yaml:"rendering"` } @@ -58,11 +63,9 @@ var GlobalConfig AmConfig // init prepares the default configuration for the application. func init() { - var defaultConfig AmConfig if err := yaml.Unmarshal(defaultConfigData, &defaultConfig); err != nil { panic(err) // can't happen } - GlobalConfig = defaultConfig } /* overlayString is a helper that takes a loaded or defaulted string and returns it. @@ -92,21 +95,21 @@ func overlayConfig(dest *AmConfig, loaded *AmConfig, defaults *AmConfig) { // SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig. func SetupConfig() { - var args AmCLI - argparse.MustParse(&args) + argparse.MustParse(&CommandLine) - if args.ConfigFile != "" { + if CommandLine.ConfigFile != "" { // load the data and use it to unmarshal the loaded configuration - data, err := os.ReadFile(args.ConfigFile) + data, err := os.ReadFile(CommandLine.ConfigFile) if err != nil { - panic(fmt.Sprintf("unable to load configuration file %s: %v", args.ConfigFile, err)) + panic(fmt.Sprintf("unable to load configuration file %s: %v", CommandLine.ConfigFile, err)) } var loadedConfig AmConfig if err = yaml.Unmarshal(data, &loadedConfig); err != nil { - panic(fmt.Sprintf("unable to load configuration file %s: %v", args.ConfigFile, err)) + panic(fmt.Sprintf("unable to load configuration file %s: %v", CommandLine.ConfigFile, err)) } overlayConfig(&GlobalConfig, &loadedConfig, &defaultConfig) } else { GlobalConfig = defaultConfig // just copy over the defaults } + log.Infof("Global config: %v", GlobalConfig) } diff --git a/config/default.yaml b/config/default.yaml index a793fe2..950ac38 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -10,3 +10,4 @@ site: title: "Amsterdam Web Communities System" rendering: templatedir: custom_templates + cookiekey: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz diff --git a/go.mod b/go.mod index 651f73c..89236ee 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,10 @@ require ( github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect github.com/alexflint/go-arg v1.6.0 // indirect github.com/alexflint/go-scalar v1.2.0 // indirect + github.com/gorilla/context v1.1.2 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.4.0 // indirect + github.com/labstack/echo-contrib v0.17.4 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 36dba7c..801d3f1 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,14 @@ github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oy github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o= +github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM= +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ= +github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik= +github.com/labstack/echo-contrib v0.17.4 h1:g5mfsrJfJTKv+F5uNKCyrjLK7js+ZW6HTjg4FnDxxgk= +github.com/labstack/echo-contrib v0.17.4/go.mod h1:9O7ZPAHUeMGTOAfg80YqQduHzt0CzLak36PZRldYrZ0= github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= diff --git a/main.go b/main.go index 56e1c4c..8646bf9 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ package main import ( "git.erbosoft.com/amy/amsterdam/config" "git.erbosoft.com/amy/amsterdam/ui" + "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) @@ -24,6 +25,7 @@ func setupEcho() *echo.Echo { e.Renderer = &ui.TemplateRenderer{} e.Use(middleware.Recover()) e.Use(LogrusMiddleware) + e.Use(session.Middleware(ui.SessionStore)) e.GET("/img/*", ui.AmWrap(ui.AmServeImage)) e.GET("/", ui.AmWrap(func(ctxt ui.AmContext) (string, any, error) { @@ -36,9 +38,12 @@ func setupEcho() *echo.Echo { // main is Ye Olde Main Function. func main() { + // Configure the system. config.SetupConfig() ui.SetupTemplates() - e := setupEcho() + ui.SetupSessionManager() + // Set up Echo and start it. Won't return. + e := setupEcho() e.Logger.Fatal(e.Start(":1323")) } diff --git a/ui/amcontext.go b/ui/amcontext.go index 1927966..8e0ff6c 100644 --- a/ui/amcontext.go +++ b/ui/amcontext.go @@ -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. diff --git a/ui/render_wrap.go b/ui/render_wrap.go index d083947..a67b010 100644 --- a/ui/render_wrap.go +++ b/ui/render_wrap.go @@ -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)) diff --git a/ui/session_mgr.go b/ui/session_mgr.go new file mode 100644 index 0000000..e3db50c --- /dev/null +++ b/ui/session_mgr.go @@ -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" +}