refactor: return redirect instead of json
All checks were successful
/ build-and-push-image (push) Successful in 1m19s
All checks were successful
/ build-and-push-image (push) Successful in 1m19s
This commit is contained in:
parent
eeca427725
commit
554719f566
|
@ -16,14 +16,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func KonuluKonumCreate(c *fiber.Ctx) error {
|
func KonuluKonumCreate(c *fiber.Ctx) error {
|
||||||
|
clientURL := os.Getenv("CLIENT_URL")
|
||||||
if form, err := c.MultipartForm(); err == nil {
|
if form, err := c.MultipartForm(); err == nil {
|
||||||
// Get form values
|
// Get form values
|
||||||
author := form.Value["author"][0]
|
author := form.Value["author"][0]
|
||||||
description := form.Value["description"][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]
|
file := form.File["selected-photo"][0]
|
||||||
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
|
|
||||||
|
|
||||||
newFile, err := file.Open()
|
newFile, err := file.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,17 +32,19 @@ func KonuluKonumCreate(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
defer newFile.Close()
|
defer newFile.Close()
|
||||||
|
|
||||||
|
// Read image file
|
||||||
data, err := io.ReadAll(newFile)
|
data, err := io.ReadAll(newFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compress image
|
// Compress image file and convert to webp
|
||||||
newImage, err := bimg.NewImage(data).Convert(bimg.WEBP)
|
newImage, err := bimg.NewImage(data).Convert(bimg.WEBP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save image file in public folder
|
||||||
imageName := strings.Split(file.Filename, ".")[0]
|
imageName := strings.Split(file.Filename, ".")[0]
|
||||||
imagePath := fmt.Sprintf("./public/%s.webp", imageName)
|
imagePath := fmt.Sprintf("./public/%s.webp", imageName)
|
||||||
imageURL := fmt.Sprintf("%s.webp", imageName)
|
imageURL := fmt.Sprintf("%s.webp", imageName)
|
||||||
|
@ -49,20 +52,24 @@ func KonuluKonumCreate(c *fiber.Ctx) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
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]
|
chars := uniuri.StdChars[26:52]
|
||||||
randomUri := uniuri.NewLenChars(10, chars)
|
randomUri := uniuri.NewLenChars(10, chars)
|
||||||
imageUri := fmt.Sprintf("%s-%s-%s", randomUri[0:3], randomUri[3:7], randomUri[7:])
|
imageUri := fmt.Sprintf("%s-%s-%s", randomUri[0:3], randomUri[3:7], randomUri[7:])
|
||||||
|
|
||||||
|
// Write to DB
|
||||||
db := DB.GetDB()
|
db := DB.GetDB()
|
||||||
db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageURL, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0})
|
db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageURL, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0})
|
||||||
|
|
||||||
return c.JSON(fiber.Map{
|
// Return URL
|
||||||
"url": imageUri,
|
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 {
|
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")
|
uri := c.Params("locationUri")
|
||||||
if len(uri) == 0 {
|
if len(uri) == 0 {
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
|
|
18
server.go
18
server.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
DB "log101/konulu-konum-backend/db"
|
DB "log101/konulu-konum-backend/db"
|
||||||
"log101/konulu-konum-backend/handlers"
|
"log101/konulu-konum-backend/handlers"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
|
@ -18,7 +19,13 @@ func main() {
|
||||||
log.Println("Error loading .env file")
|
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()
|
DB.InitDB()
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
@ -26,14 +33,19 @@ func main() {
|
||||||
app.Use(cors.New(cors.Config{
|
app.Use(cors.New(cors.Config{
|
||||||
AllowOrigins: "http://localhost:4321",
|
AllowOrigins: "http://localhost:4321",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
// Serve static images
|
||||||
app.Static("/images", "./public")
|
app.Static("/images", "./public")
|
||||||
|
|
||||||
|
// Create konulu konum
|
||||||
app.Post("/api/location", handlers.KonuluKonumCreate)
|
app.Post("/api/location", handlers.KonuluKonumCreate)
|
||||||
|
|
||||||
|
// Get konulu konum
|
||||||
app.Get("/api/location/:locationUri", handlers.KonuluKonumGet)
|
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")
|
app.Listen(":3456")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user