-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
685f05d
commit 22d6d3d
Showing
8 changed files
with
258 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
vendor/ | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: '3.8' | ||
|
||
services: | ||
db: | ||
image: mongo:4.2 | ||
volumes: | ||
- ./data/db:/data/db | ||
ports: | ||
- "27017:27017" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/TomerHeber/go-short-url" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/labstack/echo/v4" | ||
"github.com/pkg/browser" | ||
) | ||
|
||
type CreateUrlRequest struct { | ||
Url string `json:"url" validate:"required,url"` | ||
} | ||
|
||
type CreateUrlResponse struct { | ||
ShortUrl string `json:"shortUrl"` | ||
} | ||
|
||
type CustomValidator struct { | ||
validator *validator.Validate | ||
} | ||
|
||
func (cv *CustomValidator) Validate(i interface{}) error { | ||
if err := cv.validator.Struct(i); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
s, err := short.NewShortener() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
e := echo.New() | ||
|
||
e.Validator = &CustomValidator{validator: validator.New()} | ||
|
||
e.File("/", "example/public/index.html") | ||
e.File("/index.html", "example/public/index.html") | ||
|
||
e.POST("/create", func(c echo.Context) error { | ||
cur := new(CreateUrlRequest) | ||
if err := c.Bind(cur); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
} | ||
|
||
if err := c.Validate(cur); err != nil { | ||
return err | ||
} | ||
|
||
surl, err := s.CreateShortenedUrl(c.Request().Context(), cur.Url) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | ||
} | ||
|
||
return c.JSON(http.StatusOK, &CreateUrlResponse{ | ||
ShortUrl: surl.GetUrl(), | ||
}) | ||
}) | ||
|
||
e.GET("/:id", func(c echo.Context) error { | ||
id := c.Param("id") | ||
|
||
if len(id) > 7 { | ||
return echo.NewHTTPError(http.StatusNotFound) | ||
} | ||
|
||
url, err := s.GetUrlFromShortenedUrlId(c.Request().Context(), id) | ||
if err != nil { | ||
if errors.Is(err, &short.IdNotFoundError{}) { | ||
return echo.NewHTTPError(http.StatusNotFound, err.Error()) | ||
} | ||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) | ||
} | ||
|
||
return c.Redirect(http.StatusMovedPermanently, url) | ||
}) | ||
|
||
//nolint | ||
go browser.OpenURL("http://localhost:8080") | ||
e.Logger.Fatal(e.Start(":8080")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Url Shortner Example</title> | ||
<script | ||
src="https://code.jquery.com/jquery-3.6.1.slim.min.js" | ||
integrity="sha256-w8CvhFs7iHNVUtnSP0YKEg00p9Ih13rlL9zGqvLdePA=" | ||
crossorigin="anonymous"></script> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="d-flex pt-5"> | ||
<input class="form-control" type="text" placeholder="Enter url to shorten (E.g. https://www.google.com)" id="url-input"> | ||
<button class="btn btn-primary" id="apply-button">Apply</button> | ||
</div> | ||
<div class="pt-2"> | ||
<a href="" target="_blank" id="short-url"></a> | ||
<span class="text-danger" id="error-span"></span> | ||
</div> | ||
</div> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0/axios.min.js" integrity="sha512-26uCxGyoPL1nESYXHQ+KUmm3Maml7MEQNWU8hIt1hJaZa5KQAQ5ehBqK6eydcCOh6YAuZjV3augxu/5tY4fsgQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | ||
<script> | ||
$(function () { | ||
$("#apply-button").on("click", function() { | ||
const self = $(this); | ||
|
||
self.prop("disabled", true); | ||
$("#error-span").text(""); | ||
$("#short-url").text(""); | ||
|
||
const url = $("#url-input").val(); | ||
|
||
axios.post("/create", { | ||
url: url, | ||
}).then(function (res) { | ||
const shortUrl = res.data.shortUrl; | ||
$("#short-url").text(shortUrl).attr("href", shortUrl); | ||
}).catch(function (err) { | ||
let data = ""; | ||
if (err.response && err.response.data) { | ||
data = err.response.data; | ||
} | ||
$("#error-span").text(`${err} ${JSON.stringify(data)}`); | ||
}).finally(function () { | ||
self.prop("disabled", false); | ||
}) | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters