Files
top-wallpaper/main.go
Ron Rise 2e2f9f7fee
Some checks failed
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Has been cancelled
🚨 Test Code Base / 🧹 Lint (push) Has been cancelled
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Has been cancelled
Refactor context key for logger in main.go
2025-08-27 00:28:36 -04:00

67 lines
1.3 KiB
Go

package main
import (
"context"
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
tphttp "github.com/siteworxpro/top-wallpaper/http"
"github.com/siteworxpro/top-wallpaper/reddit"
"github.com/siteworxpro/top-wallpaper/redis"
)
func main() {
ctx, fn := context.WithCancel(context.Background())
defer fn()
rc, err := redis.NewRedis()
if err != nil {
log.Error("Could not initialize Redis client: ", err)
return
}
e := echo.New()
e.AcquireContext().Set("redisClient", rc)
e.Logger.SetOutput(os.Stdout)
e.Logger.SetLevel(log.INFO)
e.HideBanner = true
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: strings.Split(os.Getenv("ALLOWED_ORIGINS"), ","),
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
AllowMethods: []string{http.MethodGet, http.MethodOptions},
}))
path := os.Getenv("PATH_PREFIX")
if path == "" {
path = "/"
}
e.GET(path, tphttp.Get)
e.Logger.Info("Starting server at path " + path)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
ctx = redis.WithContext(ctx, rc)
ctx = context.WithValue(ctx, "logger", e.Logger) //nolint:staticcheck
go reddit.Fetch(ctx)
e.Logger.Fatal(e.Start(":" + port))
}