2024-07-03 18:33:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-03 19:28:20 +00:00
|
|
|
"io"
|
2024-07-04 17:12:39 +00:00
|
|
|
"log"
|
|
|
|
DB "log101/konulu-konum-backend/db"
|
|
|
|
"log101/konulu-konum-backend/models"
|
2024-07-05 14:25:48 +00:00
|
|
|
"strings"
|
2024-07-04 17:12:39 +00:00
|
|
|
|
2024-07-03 19:28:20 +00:00
|
|
|
"os"
|
2024-07-03 18:33:46 +00:00
|
|
|
|
2024-07-04 16:52:51 +00:00
|
|
|
"github.com/dchest/uniuri"
|
2024-07-03 18:33:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-07-05 14:25:48 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2024-07-03 18:33:46 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2024-07-03 19:28:20 +00:00
|
|
|
"github.com/h2non/bimg"
|
2024-07-04 17:12:39 +00:00
|
|
|
"github.com/joho/godotenv"
|
2024-07-05 14:54:28 +00:00
|
|
|
"gorm.io/gorm"
|
2024-07-03 18:33:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-04 17:12:39 +00:00
|
|
|
// Load environment variables
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error loading .env file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize db
|
|
|
|
DB.InitDB()
|
|
|
|
db := DB.GetDB()
|
|
|
|
|
2024-07-03 18:33:46 +00:00
|
|
|
app := fiber.New()
|
|
|
|
app.Use(logger.New())
|
2024-07-05 14:25:48 +00:00
|
|
|
app.Use(cors.New(cors.Config{
|
|
|
|
AllowOrigins: "http://localhost:4321",
|
2024-07-03 19:28:20 +00:00
|
|
|
}))
|
2024-07-05 14:25:48 +00:00
|
|
|
app.Static("/images", "./public")
|
2024-07-03 18:33:46 +00:00
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.SendString("Sample")
|
|
|
|
})
|
|
|
|
|
2024-07-04 17:59:47 +00:00
|
|
|
app.Post("/api/location", func(c *fiber.Ctx) error {
|
2024-07-03 18:33:46 +00:00
|
|
|
if form, err := c.MultipartForm(); err == nil {
|
2024-07-05 14:25:48 +00:00
|
|
|
// Get form values
|
|
|
|
author := form.Value["author"][0]
|
|
|
|
description := form.Value["description"][0]
|
|
|
|
geolocation := fmt.Sprintf("[%s]", form.Value["geolocation"][0])
|
2024-07-03 18:33:46 +00:00
|
|
|
|
2024-07-05 14:25:48 +00:00
|
|
|
file := form.File["selected-photo"][0]
|
2024-07-04 16:52:51 +00:00
|
|
|
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-04 16:52:51 +00:00
|
|
|
newFile, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
defer newFile.Close()
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-04 16:52:51 +00:00
|
|
|
data, err := io.ReadAll(newFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-04 16:52:51 +00:00
|
|
|
// Compress image
|
|
|
|
newImage, err := bimg.NewImage(data).Convert(bimg.WEBP)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-05 14:25:48 +00:00
|
|
|
imageName := strings.Split(file.Filename, ".")[0]
|
|
|
|
imagePath := fmt.Sprintf("./public/%s.webp", imageName)
|
|
|
|
imageURL := fmt.Sprintf("%s.webp", imageName)
|
|
|
|
bimg.Write(imagePath, newImage)
|
|
|
|
|
2024-07-04 16:52:51 +00:00
|
|
|
// Generate public uri for the image
|
|
|
|
chars := uniuri.StdChars[26:52]
|
|
|
|
randomUri := uniuri.NewLenChars(10, chars)
|
|
|
|
imageUri := fmt.Sprintf("%s-%s-%s", randomUri[0:3], randomUri[3:7], randomUri[7:])
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-05 14:25:48 +00:00
|
|
|
db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageURL, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0})
|
2024-07-03 18:33:46 +00:00
|
|
|
|
2024-07-05 14:25:48 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"url": imageUri,
|
|
|
|
})
|
2024-07-04 17:59:47 +00:00
|
|
|
}
|
2024-07-04 17:12:39 +00:00
|
|
|
|
2024-07-04 17:59:47 +00:00
|
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
|
|
})
|
2024-07-04 17:12:39 +00:00
|
|
|
|
2024-07-04 17:59:47 +00:00
|
|
|
app.Get("/api/location/:locationUri", func(c *fiber.Ctx) error {
|
|
|
|
uri := c.Params("locationUri")
|
|
|
|
if len(uri) == 0 {
|
|
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
|
|
}
|
2024-07-04 17:12:39 +00:00
|
|
|
|
2024-07-04 17:59:47 +00:00
|
|
|
var konuluKonum models.KonuluKonum
|
|
|
|
rows := db.Where("URI = ?", uri).First(&konuluKonum)
|
|
|
|
if rows.Error != nil {
|
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
2024-07-03 18:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 14:25:48 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"url": konuluKonum.URI,
|
|
|
|
"blob_url": konuluKonum.ImageURL,
|
|
|
|
"loc": konuluKonum.Coordinates,
|
|
|
|
"author": konuluKonum.AuthorName,
|
|
|
|
"description": konuluKonum.Description,
|
|
|
|
"unlocked_counter": konuluKonum.UnlockedCounter,
|
|
|
|
})
|
2024-07-03 18:33:46 +00:00
|
|
|
})
|
|
|
|
|
2024-07-05 14:54:28 +00:00
|
|
|
app.Patch("/api/location/increment/:locationUri", func(c *fiber.Ctx) error {
|
|
|
|
uri := c.Params("locationUri")
|
|
|
|
if len(uri) == 0 {
|
|
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
var konuluKonum models.KonuluKonum
|
|
|
|
rows := db.Where("URI = ?", uri).First(&konuluKonum)
|
|
|
|
if rows.Error != nil {
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows = db.Model(&konuluKonum).Where("uri = ?", uri).UpdateColumn("unlocked_counter", gorm.Expr("unlocked_counter + 1"))
|
|
|
|
if rows.Error != nil {
|
|
|
|
c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"counter": konuluKonum.ID,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-07-03 18:33:46 +00:00
|
|
|
app.Listen(":3000")
|
2024-07-03 19:28:20 +00:00
|
|
|
|
2024-07-03 18:33:46 +00:00
|
|
|
}
|