Compare commits

...

3 Commits

Author SHA1 Message Date
238b542aeb feat: add recover middleware
All checks were successful
/ build-and-push-image (push) Successful in 1m15s
2024-09-03 11:26:12 +03:00
70d446b067 feat: generate random names for uploaded images for better security
All checks were successful
/ build-and-push-image (push) Successful in 1m35s
2024-09-03 11:04:57 +03:00
36322af284 feat: return date in get handler
All checks were successful
/ build-and-push-image (push) Successful in 1m24s
2024-09-03 10:08:37 +03:00
3 changed files with 8 additions and 5 deletions

2
go.mod
View File

@ -5,6 +5,7 @@ go 1.22.3
require (
github.com/dchest/uniuri v1.2.0
github.com/gofiber/fiber/v2 v2.52.5
github.com/google/uuid v1.6.0
github.com/h2non/bimg v1.1.9
github.com/joho/godotenv v1.5.1
gorm.io/driver/sqlite v1.5.6
@ -13,7 +14,6 @@ require (
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.17.9 // indirect

View File

@ -5,13 +5,13 @@ import (
"io"
"os"
"strconv"
"strings"
DB "log101/konulu-konum-backend/db"
"log101/konulu-konum-backend/models"
"github.com/dchest/uniuri"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/h2non/bimg"
"gorm.io/gorm"
)
@ -62,9 +62,9 @@ func KonuluKonumCreate(c *fiber.Ctx) error {
}
// Save image file in public folder
imageName := strings.Split(file[0].Filename, ".")[0]
imageName := uuid.New()
imagePath := fmt.Sprintf("./public/%s.webp", imageName)
imageURL := fmt.Sprintf("%s.webp", imageName)
imageNameWithExtension := fmt.Sprintf("%s.webp", imageName)
err = bimg.Write(imagePath, newImage)
if err != nil {
fmt.Fprintln(os.Stderr, err)
@ -80,7 +80,7 @@ func KonuluKonumCreate(c *fiber.Ctx) error {
// Write to DB
db := DB.GetDB()
db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageURL, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0, Radius: radiusInt})
db.Create(&models.KonuluKonum{URI: imageUri, ImageURL: imageNameWithExtension, Coordinates: geolocation, AuthorName: author, Description: description, UnlockedCounter: 0, Radius: radiusInt})
// Return URL
redirectURL := fmt.Sprintf("%s/x?id=%s", clientURL, imageUri)
@ -112,6 +112,7 @@ func KonuluKonumGet(c *fiber.Ctx) error {
"description": konuluKonum.Description,
"radius": konuluKonum.Radius,
"unlocked_counter": konuluKonum.UnlockedCounter,
"created_at": konuluKonum.CreatedAt,
})
}

View File

@ -9,6 +9,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/joho/godotenv"
)
@ -29,6 +30,7 @@ func main() {
DB.InitDB()
app := fiber.New()
app.Use(recover.New())
app.Use(logger.New())
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:4321",