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

View File

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