2025-08-04 17:45:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-22 12:57:33 +02:00
|
|
|
"backend/config"
|
2025-08-22 10:11:07 +02:00
|
|
|
"backend/queries"
|
2025-08-04 17:45:44 +02:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
type Server struct {
|
2026-05-22 15:49:28 +02:00
|
|
|
config *config.Config
|
2025-08-22 12:57:33 +02:00
|
|
|
}
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func NewServer(cfg *config.Config) (*Server, error) {
|
|
|
|
|
return &Server{
|
2026-05-22 15:49:28 +02:00
|
|
|
config: cfg,
|
2025-08-22 12:57:33 +02:00
|
|
|
}, nil
|
2025-08-04 17:45:44 +02:00
|
|
|
}
|
2025-10-29 09:23:13 +01:00
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
func (s *Server) SetupRoutes() *echo.Echo {
|
2025-08-04 17:45:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
// MISC
|
|
|
|
|
e.Static("/static", "public/static")
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
|
return c.Render(http.StatusOK, "main", map[string]any{
|
2025-08-22 12:57:33 +02:00
|
|
|
"imageOption": s.config.Images.Enabled,
|
|
|
|
|
"concurrentImages": s.config.Images.ConcurrentLimit,
|
2025-08-04 17:45:44 +02:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
e.GET("/test", func(c echo.Context) error {
|
2025-10-15 14:27:06 +02:00
|
|
|
|
2025-10-27 17:58:42 +01:00
|
|
|
return c.JSONPretty(http.StatusOK,
|
|
|
|
|
map[string]any{
|
2026-05-19 18:42:34 +02:00
|
|
|
"files": "",
|
2025-10-27 17:58:42 +01:00
|
|
|
}, " ")
|
2025-08-04 17:45:44 +02:00
|
|
|
})
|
|
|
|
|
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 {
|
2025-08-22 10:11:07 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
e.GET("/update", func(c echo.Context) error {
|
2026-05-22 15:49:28 +02:00
|
|
|
//da inizializzare on server startup non qui
|
|
|
|
|
u := NewUpdater(UpdaterConfig{
|
|
|
|
|
isDryRun: c.QueryParam("dryrun") == "true",
|
|
|
|
|
codFilter: c.QueryParam("cod"),
|
|
|
|
|
forceMediaRefresh: c.QueryParam("forcemedia") == "true",
|
|
|
|
|
})
|
|
|
|
|
err := u.Update()
|
2025-11-18 15:20:14 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
2026-05-22 15:49:28 +02:00
|
|
|
return c.String(http.StatusOK, "update")
|
2025-10-15 14:27:06 +02:00
|
|
|
})
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-22 12:57:33 +02:00
|
|
|
//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
|
|
|
|
|
s.config.Images.Enabled = !s.config.Images.Enabled
|
|
|
|
|
|
|
|
|
|
// Convert the boolean value to a string and set the environment variable
|
|
|
|
|
os.Setenv("IMAGEOPTION", strconv.FormatBool(s.config.Images.Enabled))
|
|
|
|
|
|
|
|
|
|
return c.String(http.StatusOK, "OK")
|
2025-08-04 17:45:44 +02:00
|
|
|
})
|
2025-08-22 12:57:33 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
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)
|
2025-08-04 17:45:44 +02:00
|
|
|
}
|