From 640e04f35644bcce50303a5ede0b1b98bb6007d2 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 26 Nov 2024 22:07:05 -0600 Subject: [PATCH] Serve redirects --- main.go | 29 +++++++++++++++++++++++------ static/index.html | 18 ++++++++++++------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index c2bbb6f..3cc7f33 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,13 @@ func (sl *ShortLinks) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (sl *ShortLinks) serveRoot(w http.ResponseWriter, r *http.Request) { log.Printf("%s %s", r.RemoteAddr, r.URL.Path) + sl.serveRootWithPath(w, r, "") +} - err := sl.tmpl.Execute(w, nil) +func (sl *ShortLinks) serveRootWithPath(w http.ResponseWriter, r *http.Request, path string) { + err := sl.tmpl.Execute(w, map[string]any{ + "path": path, + }) if err != nil { sendError(w, http.StatusInternalServerError, "error executing template: %s", err) return @@ -57,7 +62,19 @@ func (sl *ShortLinks) serveRoot(w http.ResponseWriter, r *http.Request) { } func (sl *ShortLinks) serveShort(w http.ResponseWriter, r *http.Request) { - sendError(w, http.StatusNotImplemented, "not implemented") + log.Printf("%s %s", r.RemoteAddr, r.URL.Path) + + short := r.PathValue("short") + + row := sl.db.QueryRow(`SELECT long FROM links WHERE short = $1`, short) + var long string + err := row.Scan(&long) + if err != nil { + sl.serveRootWithPath(w, r, short) + return + } + + http.Redirect(w, r, long, http.StatusTemporaryRedirect) } func (sl *ShortLinks) serveSet(w http.ResponseWriter, r *http.Request) { @@ -78,10 +95,10 @@ func (sl *ShortLinks) serveSet(w http.ResponseWriter, r *http.Request) { } _, err = sl.db.Exec(` - INSERT INTO links (short, long) - VALUES ($1, $2) - ON CONFLICT (short) - DO UPDATE SET long = $2 +INSERT INTO links (short, long) +VALUES ($1, $2) +ON CONFLICT (short) +DO UPDATE SET long = $2; `, short, long) if err != nil { sendError(w, http.StatusInternalServerError, "upsert: %s", err) diff --git a/static/index.html b/static/index.html index b044950..ad88ad7 100644 --- a/static/index.html +++ b/static/index.html @@ -19,6 +19,7 @@ sl-icon[name="type"] { sl-icon[name="square"] { color: var(--sl-color-warning-500); + cursor: pointer; } sl-icon[name="check-square"] { @@ -123,7 +124,15 @@ function setShortItem(short, icon) { tree.insertBefore(item, tree.firstChild); } -document.addEventListener('DOMContentLoaded', () => { +document.addEventListener('DOMContentLoaded', async () => { + await Promise.all([ + customElements.whenDefined('sl-input'), + customElements.whenDefined('sl-icon'), + customElements.whenDefined('sl-button'), + customElements.whenDefined('sl-alert'), + customElements.whenDefined('sl-tree'), + ]); + document.getElementById('short').setAttribute('label', `${window.location.host}/`); document.getElementById('short').addEventListener('sl-input', () => { @@ -178,16 +187,13 @@ document.addEventListener('DOMContentLoaded', () => { document.getElementById('long').focus(); + setInputIcons(); }); - -// name="type" -// name="circle" -// name="check-circle"
- +