-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
91 lines (71 loc) · 2.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
client *mongo.Client
collection *mongo.Collection
)
type Redirection struct {
ID string `bson:"_id,omitempty" json: "id"`
OriginalURL string `bson:"originalurl" json: "originalUrl"`
NewURL string `bson:"newurl" json: "newUrl"`
CreatedAt time.Time `bson:"createdat" json: "createdAt"`
ShortUrl string `bson: "shorturl json: "shortUrl"`
}
func initMongoDB() {
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(context.Background(), clientOptions)
collection = client.Database("url_shortener").Collection("redirections")
}
func redirectToNewURL(c *fiber.Ctx) error {
shortUrl := c.Params("shortUrl")
fmt.Println(shortUrl)
var redirection Redirection
err := collection.FindOne(context.Background(), bson.M{"shorturl": shortUrl}).Decode(&redirection)
if err != nil {
return c.Status(http.StatusNotFound).SendString("URL not found")
}
return c.Redirect(redirection.OriginalURL, http.StatusSeeOther)
}
func generateShortURL(c *fiber.Ctx) error {
var redirection Redirection
if err := c.BodyParser(&redirection); err != nil {
return c.Status(http.StatusBadRequest).SendString("Invalid request body")
}
rand.Seed(time.Now().UnixNano())
chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
randomString := ""
for i := 0; i < 5; i++ {
randomString += string(chars[rand.Intn(len(chars))])
}
redirection.ShortUrl = randomString
redirection.NewURL = "http://localhost:8080/" + randomString
_, err := collection.InsertOne(context.Background(), redirection)
if err != nil {
return c.Status(http.StatusInternalServerError).SendString("Error saving data")
}
return c.Status(http.StatusCreated).JSON(redirection)
}
func main() {
initMongoDB()
app := fiber.New()
app.Static("/", "./index.html")
corsConfig := cors.New(cors.Config{
AllowOrigins: "https://shrinkit-ashy.vercel.app", // Allow requests from your domain
})
app.Use(corsConfig)
app.Get("/:shortUrl", redirectToNewURL)
app.Post("/shorten", generateShortURL)
fmt.Println("Server started on :8080")
app.Listen(":8080")
}