mirror of
https://github.com/arkorty/Reduce.git
synced 2026-03-17 16:41:42 +00:00
Fix: correct domain is used for the short url
This commit is contained in:
@@ -50,14 +50,43 @@ func generateRandomString(length int) string {
|
||||
}
|
||||
|
||||
func shortenURL(c echo.Context) error {
|
||||
url := new(URL)
|
||||
if err := c.Bind(url); err != nil {
|
||||
// Define a struct for binding the request body
|
||||
type RequestBody struct {
|
||||
LongURL string `json:"long_url"`
|
||||
BaseURL string `json:"base_url"` // Expect base URL in the request
|
||||
}
|
||||
|
||||
// Bind request body to the RequestBody struct
|
||||
reqBody := new(RequestBody)
|
||||
if err := c.Bind(reqBody); err != nil {
|
||||
return err
|
||||
}
|
||||
url.CreatedAt = time.Now()
|
||||
url.ID = generateRandomString(6)
|
||||
url.ShortURL = "http://localhost:3000/" + url.ID
|
||||
db.Create(url)
|
||||
|
||||
// Validate the base URL
|
||||
if reqBody.BaseURL == "" {
|
||||
// Fallback to BASE_URL environment variable
|
||||
reqBody.BaseURL = os.Getenv("BASE_URL")
|
||||
if reqBody.BaseURL == "" {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Base URL is not configured")
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a unique ID
|
||||
id := generateRandomString(6)
|
||||
|
||||
// Create URL record
|
||||
url := &URL{
|
||||
ID: id,
|
||||
LongURL: reqBody.LongURL,
|
||||
ShortURL: reqBody.BaseURL + "/" + id,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// Save URL record to the database
|
||||
if err := db.Create(url).Error; err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create URL record")
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, url)
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,23 @@ export default function Home() {
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const response = await axios.post(
|
||||
`${process.env.NEXT_PUBLIC_BACKEND_URL}/reduce/shorten`,
|
||||
{
|
||||
long_url: longUrl,
|
||||
},
|
||||
);
|
||||
setShortUrl(response.data.short_url);
|
||||
|
||||
// Use the frontend domain as the base URL
|
||||
const baseURL = window.location.origin;
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${process.env.NEXT_PUBLIC_BACKEND_URL}/reduce/shorten`,
|
||||
{
|
||||
long_url: longUrl,
|
||||
base_url: baseURL, // Include the frontend domain in the request body
|
||||
},
|
||||
);
|
||||
|
||||
setShortUrl(response.data.short_url);
|
||||
} catch (error) {
|
||||
console.error("Error shortening URL:", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user