110 lines
2.9 KiB
Go
110 lines
2.9 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()
|
|
skipper := func(c *echo.Context) bool {
|
|
// Skip health check endpoint
|
|
return c.Request().URL.Path == "/health"
|
|
}
|
|
|
|
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
|
LogStatus: true,
|
|
LogURI: true,
|
|
LogMethod: true,
|
|
Skipper: skipper,
|
|
HandleError: true, // forwards error to the global error handler, so it can decide appropriate status code
|
|
LogValuesFunc: func(c *echo.Context, v middleware.RequestLoggerValues) error {
|
|
var errMsg string
|
|
if v.Error != nil {
|
|
errMsg = fmt.Sprintf(" ERROR: %s", v.Error.Error())
|
|
}
|
|
fmt.Printf("[%s %s]: %d%s\n", v.Method, v.URI, v.Status, errMsg)
|
|
return nil
|
|
},
|
|
}))
|
|
e.GET("/", func(c *echo.Context) error {
|
|
return c.String(http.StatusOK, "OK")
|
|
})
|
|
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)
|
|
}
|
|
}
|