refactor: return redirect instead of json
All checks were successful
/ build-and-push-image (push) Successful in 1m19s

This commit is contained in:
log101 2024-08-05 12:16:55 +03:00
parent eeca427725
commit 554719f566
2 changed files with 31 additions and 12 deletions

View File

@ -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)

View File

@ -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")
}