Files
amsterdam/main.go
T

81 lines
2.0 KiB
Go
Raw Normal View History

/*
* 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/.
*/
2025-09-14 22:05:17 -06:00
// Package main contains the high-level Amsterdam logic.
2025-09-05 21:46:10 -06:00
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"git.erbosoft.com/amy/amsterdam/config"
"git.erbosoft.com/amy/amsterdam/database"
"git.erbosoft.com/amy/amsterdam/ui"
2025-09-17 22:50:30 -06:00
"github.com/labstack/echo-contrib/session"
2025-09-05 21:46:10 -06:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2025-09-05 21:46:10 -06:00
)
2025-09-14 22:05:17 -06:00
// setupEcho creates, configures, and returns a new Echo instance.
func setupEcho() *echo.Echo {
2025-09-05 21:46:10 -06:00
e := echo.New()
2025-09-14 16:45:29 -06:00
e.Logger = &EchoLogrusAdapter{}
e.Renderer = &ui.TemplateRenderer{}
e.Use(middleware.Recover())
e.Use(LogrusMiddleware)
2025-09-17 22:50:30 -06:00
e.Use(session.Middleware(ui.SessionStore))
2025-09-14 16:57:52 -06:00
e.GET("/TODO/*", ui.AmWrap(NotImplPage))
e.GET("/img/*", ui.AmWrap(ui.AmServeImage))
2025-09-20 23:18:18 -06:00
e.GET("/about", ui.AmWrap(AboutPage))
e.GET("/", ui.AmWrap(TopPage))
2025-09-14 16:57:52 -06:00
return e
}
2025-09-14 22:05:17 -06:00
// main is Ye Olde Main Function.
func main() {
2025-09-17 22:50:30 -06:00
// Configure the system.
config.SetupConfig()
err := database.SetupDb()
if err != nil {
panic(fmt.Sprintf("Database open failure: %v", err))
}
defer database.ClosedownDb()
ui.SetupTemplates()
2025-09-17 22:50:30 -06:00
ui.SetupSessionManager()
2025-09-22 17:01:39 -06:00
ui.SetupLeftMenus()
// Set up Echo.
2025-09-17 22:50:30 -06:00
e := setupEcho()
// 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)
}
2025-09-05 21:46:10 -06:00
}