From 554719f56644661d1640c54a65bbfa1c97222eb0 Mon Sep 17 00:00:00 2001 From: log101 Date: Mon, 5 Aug 2024 12:16:55 +0300 Subject: [PATCH] refactor: return redirect instead of json --- handlers/handlers.go | 25 ++++++++++++++++--------- server.go | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 248e0be..e2235c8 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -16,14 +16,15 @@ import ( ) func KonuluKonumCreate(c *fiber.Ctx) error { + clientURL := os.Getenv("CLIENT_URL") if form, err := c.MultipartForm(); err == nil { // Get form values author := form.Value["author"][0] description := form.Value["description"][0] - geolocation := fmt.Sprintf("[%s]", form.Value["geolocation"][0]) + // Geolocation is stored as JSON array string + geolocation := fmt.Sprintf("[%s]", form.Value["geolocation"][0]) file := form.File["selected-photo"][0] - fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0]) newFile, err := file.Open() if err != nil { @@ -31,17 +32,19 @@ func KonuluKonumCreate(c *fiber.Ctx) error { } defer newFile.Close() + // Read image file data, err := io.ReadAll(newFile) if err != nil { fmt.Fprintln(os.Stderr, err) } - // Compress image + // Compress image file and convert to webp newImage, err := bimg.NewImage(data).Convert(bimg.WEBP) if err != nil { fmt.Fprintln(os.Stderr, err) } + // Save image file in public folder imageName := strings.Split(file.Filename, ".")[0] imagePath := fmt.Sprintf("./public/%s.webp", imageName) imageURL := fmt.Sprintf("%s.webp", imageName) @@ -49,20 +52,24 @@ func KonuluKonumCreate(c *fiber.Ctx) error { if err != nil { fmt.Fprintln(os.Stderr, err) } - // Generate public uri for the image + + // Generate public uri for the image this will be the + // id for the konulu konum chars := uniuri.StdChars[26:52] randomUri := uniuri.NewLenChars(10, chars) imageUri := fmt.Sprintf("%s-%s-%s", randomUri[0:3], randomUri[3:7], randomUri[7:]) + // Write to DB db := DB.GetDB() db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageURL, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0}) - return c.JSON(fiber.Map{ - "url": imageUri, - }) + // Return URL + redirectURL := fmt.Sprintf("%s/x?id=%s", clientURL, imageUri) + return c.Redirect(redirectURL) } - return c.SendStatus(fiber.StatusBadRequest) + redirectUrl := fmt.Sprintf("%s?error=%s", clientURL, "true") + return c.Redirect(redirectUrl) } func KonuluKonumGet(c *fiber.Ctx) error { @@ -88,7 +95,7 @@ func KonuluKonumGet(c *fiber.Ctx) error { }) } -func KonuluKonumUpdateCounter(c *fiber.Ctx) error { +func KonuluKonumCounterUpdate(c *fiber.Ctx) error { uri := c.Params("locationUri") if len(uri) == 0 { return c.SendStatus(fiber.StatusBadRequest) diff --git a/server.go b/server.go index 13cbb31..027de9a 100644 --- a/server.go +++ b/server.go @@ -4,6 +4,7 @@ import ( "log" DB "log101/konulu-konum-backend/db" "log101/konulu-konum-backend/handlers" + "os" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -18,7 +19,13 @@ func main() { log.Println("Error loading .env file") } - // initialize db + // Create public folder for images + err = os.Mkdir("public", 0750) + if err != nil && !os.IsExist(err) { + log.Fatal(err) + } + + // Initialize db DB.InitDB() app := fiber.New() @@ -26,14 +33,19 @@ func main() { app.Use(cors.New(cors.Config{ AllowOrigins: "http://localhost:4321", })) + + // Serve static images app.Static("/images", "./public") + // Create konulu konum app.Post("/api/location", handlers.KonuluKonumCreate) + // Get konulu konum app.Get("/api/location/:locationUri", handlers.KonuluKonumGet) - app.Patch("/api/location/increment/:locationUri", handlers.KonuluKonumUpdateCounter) + // Update 'seen' counter of konulu konum + // This is shown at the bottom of the web page + app.Patch("/api/location/increment/:locationUri", handlers.KonuluKonumCounterUpdate) app.Listen(":3456") - }