grouping community and conference routes and making more functionality implemented by middleware
This commit is contained in:
+31
-36
@@ -28,6 +28,11 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* AmContext interface
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// AmContext is the interface for Amsterdam's wrapper context that exposes the required functionality.
|
||||
type AmContext interface {
|
||||
ClearCommunityContext()
|
||||
@@ -36,7 +41,6 @@ type AmContext interface {
|
||||
CurrentCommunity() *database.Community
|
||||
CurrentUser() *database.User
|
||||
CurrentUserId() int32
|
||||
Done()
|
||||
EffectiveLevel() uint16
|
||||
FormField(string) string
|
||||
FormFieldInt(string) (int, error)
|
||||
@@ -72,13 +76,17 @@ type AmContext interface {
|
||||
VarMap() jet.VarMap
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* AmContext implementation
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// amContext is the internal structure that implements AmContext.
|
||||
type amContext struct {
|
||||
echoContext echo.Context
|
||||
httprc int
|
||||
rendervars jet.VarMap
|
||||
outputType string
|
||||
scratchpad map[string]any
|
||||
session *sessions.Session
|
||||
globals *database.Globals
|
||||
globalFlags *util.OptionSet
|
||||
@@ -143,11 +151,6 @@ func (c *amContext) CurrentUserId() int32 {
|
||||
return AmSessionUid(c.session)
|
||||
}
|
||||
|
||||
// Done signals that we're done with this context and it can be recycled.
|
||||
func (c *amContext) Done() {
|
||||
amContextRecycleBin <- c
|
||||
}
|
||||
|
||||
// EffectiveLevel returns the user's effective access level (in terms of current community, if any).
|
||||
func (c *amContext) EffectiveLevel() uint16 {
|
||||
return c.effectiveLevel
|
||||
@@ -275,14 +278,6 @@ func (c *amContext) SaveSession() error {
|
||||
return c.session.Save(c.echoContext.Request(), c.echoContext.Response())
|
||||
}
|
||||
|
||||
// Scratchpad returns the per-request scratchpad for values.
|
||||
func (c *amContext) Scratchpad() map[string]any {
|
||||
if c.scratchpad == nil {
|
||||
c.scratchpad = make(map[string]any)
|
||||
}
|
||||
return c.scratchpad
|
||||
}
|
||||
|
||||
/* SubRender renders a subtemplate to the output.
|
||||
* Parameters:
|
||||
* name = The name of the template to be rendered.
|
||||
@@ -363,18 +358,12 @@ func (c *amContext) SetRC(rc int) {
|
||||
|
||||
// GetScratch returns a value in the per-request scratchpad.
|
||||
func (c *amContext) GetScratch(name string) any {
|
||||
if c.scratchpad == nil {
|
||||
return nil
|
||||
}
|
||||
return c.scratchpad[name]
|
||||
return c.echoContext.Get("am." + name)
|
||||
}
|
||||
|
||||
// SetScratch sets a value in the per-request scratchpad.
|
||||
func (c *amContext) SetScratch(name string, val any) {
|
||||
if c.scratchpad == nil {
|
||||
c.scratchpad = make(map[string]any)
|
||||
}
|
||||
c.scratchpad[name] = val
|
||||
c.echoContext.Set("am."+name, val)
|
||||
}
|
||||
|
||||
// GetSession returns a session variable.
|
||||
@@ -431,21 +420,20 @@ var freeContext util.FreeList[amContext]
|
||||
// amContextRecycleBin is the channel we put contexts on to be recycled.
|
||||
var amContextRecycleBin chan *amContext
|
||||
|
||||
/* AmCreateContext creates a new AmContext wrapping the Echo context.
|
||||
/* newContext creates a new AmContext wrapping the Echo context.
|
||||
* Parameters:
|
||||
* ctxt - The Echo context to be wrapped.
|
||||
* Returns:
|
||||
* A new Amsterdam context wrapping that context.
|
||||
* Internal Amsterdam context structure pointer, or nil.
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func AmCreateContext(ctxt echo.Context) (AmContext, error) {
|
||||
func newContext(ctxt echo.Context) (*amContext, error) {
|
||||
rc := freeContext.Get()
|
||||
if rc == nil {
|
||||
rc = &amContext{
|
||||
httprc: http.StatusOK,
|
||||
rendervars: make(jet.VarMap),
|
||||
outputType: "",
|
||||
scratchpad: nil,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,7 +448,7 @@ func AmCreateContext(ctxt echo.Context) (AmContext, error) {
|
||||
}
|
||||
|
||||
rc.echoContext = ctxt
|
||||
ctxt.Set("amsterdam_context", rc)
|
||||
ctxt.Set("__amsterdam_context", rc)
|
||||
sess, err := session.Get("AMSTERDAM_SESSION", ctxt)
|
||||
if err == nil {
|
||||
rc.session = sess
|
||||
@@ -491,17 +479,17 @@ func AmCreateContext(ctxt echo.Context) (AmContext, error) {
|
||||
* Parameters:
|
||||
* ctxt - The Echo context to have the AmContext extracted.
|
||||
* Returns:
|
||||
* The associated AmContext, or nil if there is none.
|
||||
* The associated AmContext.
|
||||
*/
|
||||
func AmContextFromEchoContext(ctxt echo.Context) AmContext {
|
||||
myctxt := ctxt.Get("amsterdam_context")
|
||||
myctxt := ctxt.Get("__amsterdam_context")
|
||||
if myctxt != nil {
|
||||
rc, ok := myctxt.(AmContext)
|
||||
if ok {
|
||||
return rc
|
||||
}
|
||||
}
|
||||
return nil
|
||||
panic("Failed to find AmContext when required")
|
||||
}
|
||||
|
||||
// contextRecycler is the task that recycles context blocks.
|
||||
@@ -513,11 +501,6 @@ func contextRecycler(incoming chan *amContext, done chan bool) {
|
||||
delete(c.rendervars, k)
|
||||
}
|
||||
c.outputType = ""
|
||||
if c.scratchpad != nil {
|
||||
for k := range c.scratchpad {
|
||||
delete(c.scratchpad, k)
|
||||
}
|
||||
}
|
||||
c.session = nil
|
||||
c.globals = nil
|
||||
c.globalFlags = nil
|
||||
@@ -541,3 +524,15 @@ func SetupAmContext() func() {
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
// ContextCreator is middleware that creates and recycles the AmContext.
|
||||
func ContextCreator(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
myctxt, err := newContext(c)
|
||||
if err == nil {
|
||||
err = next(c)
|
||||
amContextRecycleBin <- myctxt
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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 (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/labstack/echo/v4"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// IPBanTest is middleware that handles the IP banning.
|
||||
func IPBanTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// Check IP banning.
|
||||
banmsg, banerr := database.AmTestIPBan(c.RealIP())
|
||||
if banerr != nil {
|
||||
c.Logger().Warnf("address %s could not be tested: %v", c.RealIP(), banerr)
|
||||
// but let the request pass anyway
|
||||
} else if banmsg != "" {
|
||||
amctxt := AmContextFromEchoContext(c)
|
||||
amctxt.VarMap().Set("amsterdam_pageTitle", "IP Address Banned")
|
||||
amctxt.VarMap().Set("message", banmsg)
|
||||
amctxt.SetRC(http.StatusForbidden)
|
||||
return AmSendPageData(c, amctxt, "framed_template", "ipban.jet")
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
// CookieLoginTest is middleware that handles cookie logins.
|
||||
func CookieLoginTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
amctxt := AmContextFromEchoContext(c)
|
||||
// Check for cookie login.
|
||||
if amctxt.CurrentUser().IsAnon {
|
||||
cookie, err := c.Cookie(config.GlobalConfig.Site.LoginCookieName)
|
||||
if err == nil {
|
||||
var user *database.User
|
||||
user, err = database.AmAuthenticateUserByToken(cookie.Value, c.RealIP())
|
||||
if err == nil {
|
||||
// log the user in and rotate login cookie
|
||||
amctxt.ReplaceUser(user)
|
||||
var newToken string
|
||||
if newToken, err = user.NewAuthToken(); err == nil {
|
||||
amctxt.SetLoginCookie(newToken)
|
||||
} else {
|
||||
log.Warnf("unable to rotate login cookie: %v", err)
|
||||
}
|
||||
if !user.VerifyEMail {
|
||||
// bounce to E-mail verification before we go anywhere
|
||||
return AmSendPageData(c, amctxt, "redirect",
|
||||
"/verify?tgt="+url.QueryEscape(c.Request().URL.Path))
|
||||
}
|
||||
} else {
|
||||
log.Errorf("login cookie bogus, do not use: %v", err)
|
||||
amctxt.ClearLoginCookie()
|
||||
}
|
||||
}
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
// SetCommunity is middleware that sets the community context based on the URL.
|
||||
func SetCommunity(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
err := ctxt.SetCommunityContext(ctxt.URLParam("cid"))
|
||||
if err != nil {
|
||||
ctxt.SetRC(http.StatusNotFound)
|
||||
cmd, data, _ := ErrorPage(ctxt, err)
|
||||
return AmSendPageData(c, ctxt, cmd, data)
|
||||
}
|
||||
ctxt.SetLeftMenu("community")
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
+18
-52
@@ -13,14 +13,26 @@ package ui
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/labstack/echo/v4"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
/* AmSendPageData sends page data to the output based on the command string.
|
||||
* Parameters:
|
||||
* ctxt - The Echo context from the request.
|
||||
* amctxt - The associated AmContext.
|
||||
* command - The type of rendering to be done. Known values are:
|
||||
* "bytes" - Output "data" as a byte array.
|
||||
* "redirect" - Treat "data" as a URL to be redirected to and send a 302 Redirect.
|
||||
* "string" - Output "data" as a string.
|
||||
* "template" - Treat "data" as a template name, and output that template.
|
||||
* "framed_template" - Treat "data" as an inner template name, and output that template rendered
|
||||
* within the outer "frame.jet" template.
|
||||
* data - The data to be output, as determined by the command.
|
||||
* Returns:
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func AmSendPageData(ctxt echo.Context, amctxt AmContext, command string, data any) error {
|
||||
var err error
|
||||
switch command {
|
||||
@@ -29,9 +41,9 @@ func AmSendPageData(ctxt echo.Context, amctxt AmContext, command string, data an
|
||||
case "redirect":
|
||||
err = ctxt.Redirect(http.StatusFound, data.(string))
|
||||
case "string":
|
||||
err = ctxt.String(amctxt.RC(), fmt.Sprintf("%v", data))
|
||||
err = ctxt.String(amctxt.RC(), data.(string))
|
||||
case "template":
|
||||
err = ctxt.Render(amctxt.RC(), fmt.Sprintf("%v", data), amctxt)
|
||||
err = ctxt.Render(amctxt.RC(), data.(string), amctxt)
|
||||
case "framed_template":
|
||||
amctxt.VarMap().Set("amsterdam_innerPage", data)
|
||||
menus := make([]*MenuDefinition, 2)
|
||||
@@ -86,53 +98,7 @@ func ErrorPage(ctxt AmContext, input_err error) (string, any, error) {
|
||||
*/
|
||||
func AmWrap(myfunc func(AmContext) (string, any, error)) echo.HandlerFunc {
|
||||
return func(ctxt echo.Context) error {
|
||||
// Create the AmContext.
|
||||
amctxt, aerr := AmCreateContext(ctxt)
|
||||
if aerr != nil {
|
||||
ctxt.Logger().Errorf("Session creation error: %v", aerr)
|
||||
return aerr
|
||||
}
|
||||
defer amctxt.Done()
|
||||
|
||||
// Check IP banning.
|
||||
banmsg, banerr := database.AmTestIPBan(ctxt.RealIP())
|
||||
if banerr != nil {
|
||||
ctxt.Logger().Warnf("address %s could not be tested: %v", ctxt.RealIP(), banerr)
|
||||
// but let the request pass anyway
|
||||
} else if banmsg != "" {
|
||||
amctxt.VarMap().Set("amsterdam_pageTitle", "IP Address Banned")
|
||||
amctxt.VarMap().Set("message", banmsg)
|
||||
amctxt.SetRC(http.StatusForbidden)
|
||||
return AmSendPageData(ctxt, amctxt, "framed_template", "ipban.jet")
|
||||
}
|
||||
|
||||
// Check for cookie login.
|
||||
if amctxt.CurrentUser().IsAnon {
|
||||
cookie, cerr := ctxt.Cookie(config.GlobalConfig.Site.LoginCookieName)
|
||||
if cerr == nil {
|
||||
var user *database.User
|
||||
user, cerr = database.AmAuthenticateUserByToken(cookie.Value, ctxt.RealIP())
|
||||
if cerr == nil {
|
||||
// log the user in and rotate login cookie
|
||||
amctxt.ReplaceUser(user)
|
||||
var newToken string
|
||||
if newToken, cerr = user.NewAuthToken(); cerr == nil {
|
||||
amctxt.SetLoginCookie(newToken)
|
||||
} else {
|
||||
log.Warnf("unable to rotate login cookie: %v", cerr)
|
||||
}
|
||||
if !user.VerifyEMail {
|
||||
// bounce to E-mail verification before we go anywhere
|
||||
return AmSendPageData(ctxt, amctxt, "redirect",
|
||||
"/verify?tgt="+url.QueryEscape(ctxt.Request().URL.Path))
|
||||
}
|
||||
} else {
|
||||
log.Errorf("login cookie bogus, do not use: %v", cerr)
|
||||
amctxt.ClearLoginCookie()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
amctxt := AmContextFromEchoContext(ctxt)
|
||||
// Exec the wrapped function.
|
||||
what, rc, err := myfunc(amctxt)
|
||||
if err == nil {
|
||||
|
||||
Reference in New Issue
Block a user