
Reviewed-on: #1 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
70 lines
1.4 KiB
Go
70 lines
1.4 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"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const ContextKeyLogger contextKey = "logger"
|
|
|
|
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, ContextKeyLogger, e.Logger)
|
|
|
|
go reddit.Fetch(ctx)
|
|
|
|
e.Logger.Fatal(e.Start(":" + port))
|
|
}
|