infoalloggi-monorepo/apps/backend/server.go

90 lines
2.2 KiB
Go
Raw Normal View History

2025-08-04 17:45:44 +02:00
package main
import (
"backend/config"
"backend/queries"
2026-05-26 12:04:53 +02:00
"context"
2025-08-04 17:45:44 +02:00
"fmt"
"net/http"
"os"
"strconv"
2026-05-26 12:04:53 +02:00
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
2025-08-04 17:45:44 +02:00
)
2026-05-26 12:04:53 +02:00
var Cfg *config.Config
var u *Updater
2025-08-04 17:45:44 +02:00
2026-05-26 12:04:53 +02:00
func main() {
Cfg = config.Load()
u = NewUpdater()
2025-08-04 17:45:44 +02:00
e := echo.New()
2026-05-26 12:04:53 +02:00
e.Use(middleware.RequestLogger())
2025-08-04 17:45:44 +02:00
2026-05-26 12:04:53 +02:00
e.GET("/test", func(c *echo.Context) error {
return c.JSONPretty(http.StatusOK,
map[string]any{
2026-05-19 18:42:34 +02:00
"files": "",
}, " ")
2025-08-04 17:45:44 +02:00
})
2026-05-26 12:04:53 +02:00
e.POST("/testpost", func(c *echo.Context) error {
2025-08-04 17:45:44 +02:00
// Get the form value
test := c.FormValue("test")
fmt.Println("test:", test)
return c.String(http.StatusOK, "OK")
})
2026-05-26 12:04:53 +02:00
e.GET("/health", func(c *echo.Context) error {
h := queries.HealthCheck()
2026-05-19 18:42:34 +02:00
return c.JSONPretty(http.StatusOK, fmt.Sprintf("DB Connection: %v", h), " ")
2025-08-04 17:45:44 +02:00
})
2026-05-26 12:04:53 +02:00
e.GET("/update", func(c *echo.Context) error {
updateParams := UpdateParams{
2026-05-22 15:49:28 +02:00
isDryRun: c.QueryParam("dryrun") == "true",
codFilter: c.QueryParam("cod"),
forceMediaRefresh: c.QueryParam("forcemedia") == "true",
}
2026-05-26 12:04:53 +02:00
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.",
})
})
2025-08-04 17:45:44 +02:00
//IMAGE ROUTES
2026-05-26 12:04:53 +02:00
e.GET("/initcwebp", func(c *echo.Context) error {
Conversion("test.jpg")
return c.String(http.StatusOK, "Cwebp initialized")
})
2026-05-26 12:04:53 +02:00
e.POST("/image-option-toggle", func(c *echo.Context) error {
// Toggle the imageOption value
2026-05-26 12:04:53 +02:00
Cfg.Images.Enabled = !Cfg.Images.Enabled
// Convert the boolean value to a string and set the environment variable
2026-05-26 12:04:53 +02:00
os.Setenv("IMAGEOPTION", strconv.FormatBool(Cfg.Images.Enabled))
return c.String(http.StatusOK, "OK")
2025-08-04 17:45:44 +02:00
})
Conversion("test.jpg")
2026-05-26 12:04:53 +02:00
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)
}
2025-08-04 17:45:44 +02:00
}