infoalloggi-monorepo/apps/backend/server.go
2026-05-26 12:04:53 +02:00

89 lines
2.2 KiB
Go

package main
import (
"backend/config"
"backend/queries"
"context"
"fmt"
"net/http"
"os"
"strconv"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
var Cfg *config.Config
var u *Updater
func main() {
Cfg = config.Load()
u = NewUpdater()
e := echo.New()
e.Use(middleware.RequestLogger())
e.GET("/test", func(c *echo.Context) error {
return c.JSONPretty(http.StatusOK,
map[string]any{
"files": "",
}, " ")
})
e.POST("/testpost", func(c *echo.Context) error {
// Get the form value
test := c.FormValue("test")
fmt.Println("test:", test)
return c.String(http.StatusOK, "OK")
})
e.GET("/health", func(c *echo.Context) error {
h := queries.HealthCheck()
return c.JSONPretty(http.StatusOK, fmt.Sprintf("DB Connection: %v", h), " ")
})
e.GET("/update", func(c *echo.Context) error {
updateParams := UpdateParams{
isDryRun: c.QueryParam("dryrun") == "true",
codFilter: c.QueryParam("cod"),
forceMediaRefresh: c.QueryParam("forcemedia") == "true",
}
bgCtx := context.Background()
go func() {
err := u.Update(bgCtx, updateParams)
if err != nil {
e.Logger.Error(fmt.Sprintf("failed update, %v", err))
}
}()
return c.JSON(http.StatusAccepted, map[string]string{
"status": "Accepted",
"message": "Your task is being processed in the background.",
})
})
//IMAGE ROUTES
e.GET("/initcwebp", func(c *echo.Context) error {
Conversion("test.jpg")
return c.String(http.StatusOK, "Cwebp initialized")
})
e.POST("/image-option-toggle", func(c *echo.Context) error {
// Toggle the imageOption value
Cfg.Images.Enabled = !Cfg.Images.Enabled
// Convert the boolean value to a string and set the environment variable
os.Setenv("IMAGEOPTION", strconv.FormatBool(Cfg.Images.Enabled))
return c.String(http.StatusOK, "OK")
})
Conversion("test.jpg")
fmt.Println("Image Option: ", Cfg.Images.Enabled)
fmt.Println("Concurrent Images: ", Cfg.Images.ConcurrentLimit)
port := fmt.Sprintf(":%s", strconv.Itoa(Cfg.Server.Port))
fmt.Println("Server url: http://localhost" + port)
if err := e.Start(port); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}