mirror of
https://github.com/arkorty/Reduce.git
synced 2026-03-18 00:47:10 +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 {
|
func shortenURL(c echo.Context) error {
|
||||||
url := new(URL)
|
// Define a struct for binding the request body
|
||||||
if err := c.Bind(url); err != nil {
|
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
|
return err
|
||||||
}
|
}
|
||||||
url.CreatedAt = time.Now()
|
|
||||||
url.ID = generateRandomString(6)
|
// Validate the base URL
|
||||||
url.ShortURL = "http://localhost:3000/" + url.ID
|
if reqBody.BaseURL == "" {
|
||||||
db.Create(url)
|
// 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)
|
return c.JSON(http.StatusCreated, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,23 @@ export default function Home() {
|
|||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
// Use the frontend domain as the base URL
|
||||||
|
const baseURL = window.location.origin;
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${process.env.NEXT_PUBLIC_BACKEND_URL}/reduce/shorten`,
|
`${process.env.NEXT_PUBLIC_BACKEND_URL}/reduce/shorten`,
|
||||||
{
|
{
|
||||||
long_url: longUrl,
|
long_url: longUrl,
|
||||||
|
base_url: baseURL, // Include the frontend domain in the request body
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
setShortUrl(response.data.short_url);
|
setShortUrl(response.data.short_url);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error shortening URL:", error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user