backend update

This commit is contained in:
Marco Pedone 2026-05-26 12:04:53 +02:00
parent c092239ed5
commit a37eaac2a3
5 changed files with 65 additions and 97 deletions

View file

@ -1,6 +1,6 @@
module backend
go 1.25.0
go 1.25.10
require (
github.com/go-jet/jet/v2 v2.14.1
@ -25,12 +25,14 @@ require (
golang.org/x/mod v0.36.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/telemetry v0.0.0-20260508192327-42602be52be6 // indirect
golang.org/x/time v0.15.0 // indirect
golang.org/x/tools v0.45.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require (
github.com/jackc/pgx-gofrs-uuid v0.0.0-20230224015001-1d428863c2e2
github.com/labstack/echo/v5 v5.1.1
github.com/labstack/gommon v0.5.0
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.22 // indirect

View file

@ -30,6 +30,8 @@ github.com/labstack/echo/v4 v4.15.1 h1:S9keusg26gZpjMmPqB5hOEvNKnmd1lNmcHrbbH2ln
github.com/labstack/echo/v4 v4.15.1/go.mod h1:xmw1clThob0BSVRX1CRQkGQ/vjwcpOMjQZSZa9fKA/c=
github.com/labstack/echo/v4 v4.15.2 h1:nnh2sCzGCVYnU+wCisMPiYapEg/QVo/gcI9ePKg5/T4=
github.com/labstack/echo/v4 v4.15.2/go.mod h1:Xzp1Ns1RA2c9fY7nSgUJkpkUZGNbEIVHZbtbOMPktBI=
github.com/labstack/echo/v5 v5.1.1 h1:4QkvKoS8ps5ch49t8b72QS9Z581ytgxhTzxuB/CBA2I=
github.com/labstack/echo/v5 v5.1.1/go.mod h1:SyvlSdObGjRXeQfCCXW/sybkZdOOQZBmpKF0bvALaeo=
github.com/labstack/gommon v0.5.0 h1:6VSQ2NOzsnEJ5W6+84E0RbcaDDmgB6NIAzWCczTEe6c=
github.com/labstack/gommon v0.5.0/go.mod h1:Rzlg7HHy1maLfzBYGg9NZcVuz1sA68HHhLjhcEllYE0=
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
@ -85,6 +87,8 @@ golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
golang.org/x/tools v0.45.0 h1:18qN3FAooORvApf5XjCXgsuayZOEtXf6JK18I3+ONa8=
golang.org/x/tools v0.45.0/go.mod h1:LuUGqqaXcXMEFEruIVJVm5mgDD8vww/z/SR1gQ4uE/0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View file

@ -38,7 +38,7 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool, i
}
}
var to_insert []models.Annunci
to_insert := []models.Annunci{}
for _, annuncio := range annunci.Annuncio {
value := models.Annunci{
Locatore: annuncio.Locatore,
@ -102,7 +102,7 @@ func AnnunciInsert(annunci typesdefs.AnnunciParsed, resetAllBeforeUpdate bool, i
} else {
_, err = stmt.Exec(postgres)
if err != nil {
return fmt.Errorf("failed to insert/update annunci batch: %w", err)
return fmt.Errorf("failed to insert/update annunci: %w", err)
}
}
}

View file

@ -3,129 +3,87 @@ package main
import (
"backend/config"
"backend/queries"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"text/template"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
type Server struct {
config *config.Config
}
var Cfg *config.Config
var u *Updater
func NewServer(cfg *config.Config) (*Server, error) {
return &Server{
config: cfg,
}, nil
}
func main() {
Cfg = config.Load()
u = NewUpdater()
func (s *Server) SetupRoutes() *echo.Echo {
funcs := map[string]any{
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"getImage": func(txt string) string {
return txt[7:]
},
"isWebp": func(txt string) bool {
return strings.Contains(txt, "webp")
},
}
t := &Template{
templates: template.Must(template.New("").Funcs(funcs).ParseGlob("public/views/*.html")),
}
e := echo.New()
e.Renderer = t
e.Use(middleware.RequestLogger())
// MISC
e.Static("/static", "public/static")
e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "main", map[string]any{
"imageOption": s.config.Images.Enabled,
"concurrentImages": s.config.Images.ConcurrentLimit,
})
})
e.GET("/test", func(c echo.Context) error {
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 {
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 {
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 {
//da inizializzare on server startup non qui
u := NewUpdater(UpdaterConfig{
e.GET("/update", func(c *echo.Context) error {
updateParams := UpdateParams{
isDryRun: c.QueryParam("dryrun") == "true",
codFilter: c.QueryParam("cod"),
forceMediaRefresh: c.QueryParam("forcemedia") == "true",
})
err := u.Update()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, "update")
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 {
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 {
e.POST("/image-option-toggle", func(c *echo.Context) error {
// Toggle the imageOption value
s.config.Images.Enabled = !s.config.Images.Enabled
Cfg.Images.Enabled = !Cfg.Images.Enabled
// Convert the boolean value to a string and set the environment variable
os.Setenv("IMAGEOPTION", strconv.FormatBool(s.config.Images.Enabled))
os.Setenv("IMAGEOPTION", strconv.FormatBool(Cfg.Images.Enabled))
return c.String(http.StatusOK, "OK")
})
return e
}
func (s *Server) Start() {
e := s.SetupRoutes()
Conversion("test.jpg")
fmt.Println("Image Option: ", s.config.Images.Enabled)
fmt.Println("Concurrent Images: ", s.config.Images.ConcurrentLimit)
fmt.Println("Server url: http://localhost:" + strconv.Itoa(s.config.Server.Port))
e.Logger.Info(e.Start(":" + strconv.Itoa(s.config.Server.Port)))
}
func main() {
cfg := config.Load()
server, err := NewServer(cfg)
if err != nil {
log.Fatalf("Server initialization error: %v", err)
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)
}
server.Start()
}
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data any, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}

View file

@ -7,6 +7,7 @@ import (
"backend/queries"
"backend/typesdefs"
"backend/utils"
"context"
"fmt"
"log"
"slices"
@ -14,7 +15,7 @@ import (
"time"
)
type UpdaterConfig struct {
type UpdateParams struct {
// Dry run flag to simulate the update process without making changes.
isDryRun bool
// Filter by codice filter for specific updates. If empty, all records are updated.
@ -27,21 +28,17 @@ type Updater struct {
isDryRun bool
codFilter string
forceMediaRefresh bool
caratterisiche typesdefs.ListaCaratteristiche
categorie []typesdefs.Categoria
imgPool []typesdefs.MediaToProcess
videoPool []typesdefs.MediaToProcess
annunci typesdefs.AnnunciParsed
caratterisiche typesdefs.ListaCaratteristiche
categorie []typesdefs.Categoria
imgPool []typesdefs.MediaToProcess
videoPool []typesdefs.MediaToProcess
annunci typesdefs.AnnunciParsed
}
func NewUpdater(props UpdaterConfig) *Updater {
u := Updater{
isDryRun: props.isDryRun,
codFilter: props.codFilter,
forceMediaRefresh: props.forceMediaRefresh,
}
fmt.Printf("updater cfg: %+v\n", props)
func NewUpdater() *Updater {
u := Updater{}
u.init_Updater()
return &u
}
@ -88,7 +85,14 @@ func genCategorie() ([]typesdefs.Categoria, error) {
return values, nil
}
func (u *Updater) Update() error {
func (u *Updater) Update(ctx context.Context, params UpdateParams) error {
u.isDryRun = params.isDryRun
u.forceMediaRefresh = params.forceMediaRefresh
u.codFilter = params.codFilter
u.annunci = typesdefs.AnnunciParsed{}
u.imgPool = []typesdefs.MediaToProcess{}
u.videoPool = []typesdefs.MediaToProcess{}
var err error
err = u.parseAnnunci()
if err != nil {