Compare commits

...

3 commits

Author SHA1 Message Date
745e465034
feat: add SHORTEN_MAIL for support email 2023-12-21 22:39:26 +01:00
b9a275b0d5
fix: don't redirect on missing with 301
closes #1
2023-12-21 22:38:55 +01:00
4394883200
fix: remove unnecessary debug logs 2023-12-21 22:38:00 +01:00
3 changed files with 15 additions and 10 deletions

View file

@ -7,4 +7,5 @@
Environmental variables:
- `SHORTEN_HOST` - hostname
- `SHORTEN_BIND` - bind address (default: `127.0.0.1:4488`)
- `POSTGRES_URI` - lib/pq connection string (see [here](https://pkg.go.dev/github.com/lib/pq#section-documentation))
- `SHORTEN_MAIL` - optional email for support/abuse reports
- `POSTGRES_URI` - lib/pq connection string (see [here](https://pkg.go.dev/github.com/lib/pq#section-documentation))

View file

@ -10,6 +10,7 @@
height: calc(100vh - 32px);
justify-content: center;
align-items: center;
font-family: sans-serif;
}
form {
display: flex;
@ -31,10 +32,15 @@
font-size: 30px;
}
a {
font-family: sans-serif;
color: #dddddd;
text-decoration: none;
}
footer {
width: 100vw;
position: absolute;
bottom: 10px;
text-align: center;
}
</style>
</head>
<body>
@ -45,5 +51,8 @@
<input type="text" {{ with .error }}placeholder="{{ . }}"{{ end }} name="url" autocomplete="off" autocapitalize="off">
</form>
{{ end }}
{{ if .mail }}
<footer>support, takedown, abuse reports: {{ .mail }}</footer>
{{ end }}
</body>
</html>

11
main.go
View file

@ -96,8 +96,6 @@ func (h *Handler) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
}
func (h *Handler) HomepageHandler(wr http.ResponseWriter, req *http.Request) {
log.Println("HomepageHandler")
url := req.URL.Query().Get("url")
if url != "" {
code, err := h.GetCode(url, req.RemoteAddr)
@ -113,8 +111,6 @@ func (h *Handler) HomepageHandler(wr http.ResponseWriter, req *http.Request) {
}
func (h *Handler) CreateHandler(wr http.ResponseWriter, req *http.Request) {
log.Println("CreateHandler")
ip := req.RemoteAddr
if strings.HasPrefix(ip, "127.0.0.1") {
proxyIp := strings.Split(req.Header.Get("X-Forwarded-For"), ",")[0]
@ -133,8 +129,6 @@ func (h *Handler) CreateHandler(wr http.ResponseWriter, req *http.Request) {
}
func (h *Handler) RedirectHandler(wr http.ResponseWriter, req *http.Request) {
log.Println("RedirectHandler")
code := req.URL.Path[1:]
var url string
var hits uint64
@ -143,7 +137,7 @@ func (h *Handler) RedirectHandler(wr http.ResponseWriter, req *http.Request) {
log.Println("hits query error:", err)
}
wr.Header().Set("Location", "/")
wr.WriteHeader(http.StatusMovedPermanently)
wr.WriteHeader(http.StatusFound)
return
}
@ -169,7 +163,6 @@ func (h *Handler) CodeExists(code string) bool {
}
func (h *Handler) GetCode(url string, ip string) (string, error) {
log.Printf("url: %#v\n", url)
if !strings.HasPrefix(url, "http") || !govalidator.IsURL(url) {
return "", fmt.Errorf("invalid URL")
}
@ -180,6 +173,7 @@ func (h *Handler) GetCode(url string, ip string) (string, error) {
return code, nil
}
if err != sql.ErrNoRows {
log.Println("sql error:", err)
return "", fmt.Errorf("query: %w", err)
}
@ -201,6 +195,7 @@ func (h *Handler) GetCode(url string, ip string) (string, error) {
func Render(wr http.ResponseWriter, data map[string]string) {
wr.Header().Set("Content-Type", "text/html")
data["host"] = os.Getenv("SHORTEN_HOST")
data["mail"] = os.Getenv("SHORTEN_MAIL")
err := tmpl.Execute(wr, data)
if err != nil {
log.Println("error writing template:", err)