From 34b6abb92e8adafc2a7b4a07aa3e4fcad2a6c624 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Wed, 17 Sep 2025 23:07:42 -0600 Subject: [PATCH] beginnings of page functions, and server shutdown gracefully --- main.go | 33 +++++++++++++++++++++++++++------ top.go | 17 +++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 top.go diff --git a/main.go b/main.go index 8646bf9..1ff7203 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,12 @@ package main import ( + "context" + "net/http" + "os" + "os/signal" + "time" + "git.erbosoft.com/amy/amsterdam/config" "git.erbosoft.com/amy/amsterdam/ui" "github.com/labstack/echo-contrib/session" @@ -28,10 +34,7 @@ func setupEcho() *echo.Echo { e.Use(session.Middleware(ui.SessionStore)) e.GET("/img/*", ui.AmWrap(ui.AmServeImage)) - e.GET("/", ui.AmWrap(func(ctxt ui.AmContext) (string, any, error) { - ctxt.VarMap().Set("amsterdam_pageTitle", "My Front Page") - return "framed_template", "top.jet", nil - })) + e.GET("/", ui.AmWrap(TopPage)) return e } @@ -43,7 +46,25 @@ func main() { ui.SetupTemplates() ui.SetupSessionManager() - // Set up Echo and start it. Won't return. + // Set up Echo. e := setupEcho() - e.Logger.Fatal(e.Start(":1323")) + + // Set up to trap SIGINT and shut down gracefully + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + + // Start server + go func() { + if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed { + e.Logger.Fatalf("shutting down the server: %v", err) + } + }() + + // Wait for the interrupt signal and then gracefully shut the server down. + <-ctx.Done() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := e.Shutdown(ctx); err != nil { + e.Logger.Fatal(err) + } } diff --git a/top.go b/top.go new file mode 100644 index 0000000..cfa8ac5 --- /dev/null +++ b/top.go @@ -0,0 +1,17 @@ +/* + * 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 main contains the high-level Amsterdam logic. +package main + +import "git.erbosoft.com/amy/amsterdam/ui" + +func TopPage(ctxt ui.AmContext) (string, any, error) { + ctxt.VarMap().Set("amsterdam_pageTitle", "My Front Page") + return "framed_template", "top.jet", nil +}