Serve redirects

This commit is contained in:
Ian Gulliver
2024-11-26 22:07:05 -06:00
parent bac1cde9e5
commit 640e04f356
2 changed files with 35 additions and 12 deletions

29
main.go
View File

@@ -48,8 +48,13 @@ func (sl *ShortLinks) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (sl *ShortLinks) serveRoot(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) 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 { if err != nil {
sendError(w, http.StatusInternalServerError, "error executing template: %s", err) sendError(w, http.StatusInternalServerError, "error executing template: %s", err)
return 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) { 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) { 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(` _, err = sl.db.Exec(`
INSERT INTO links (short, long) INSERT INTO links (short, long)
VALUES ($1, $2) VALUES ($1, $2)
ON CONFLICT (short) ON CONFLICT (short)
DO UPDATE SET long = $2 DO UPDATE SET long = $2;
`, short, long) `, short, long)
if err != nil { if err != nil {
sendError(w, http.StatusInternalServerError, "upsert: %s", err) sendError(w, http.StatusInternalServerError, "upsert: %s", err)

View File

@@ -19,6 +19,7 @@ sl-icon[name="type"] {
sl-icon[name="square"] { sl-icon[name="square"] {
color: var(--sl-color-warning-500); color: var(--sl-color-warning-500);
cursor: pointer;
} }
sl-icon[name="check-square"] { sl-icon[name="check-square"] {
@@ -123,7 +124,15 @@ function setShortItem(short, icon) {
tree.insertBefore(item, tree.firstChild); 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').setAttribute('label', `${window.location.host}/`);
document.getElementById('short').addEventListener('sl-input', () => { document.getElementById('short').addEventListener('sl-input', () => {
@@ -178,16 +187,13 @@ document.addEventListener('DOMContentLoaded', () => {
document.getElementById('long').focus(); document.getElementById('long').focus();
setInputIcons();
}); });
// name="type"
// name="circle"
// name="check-circle"
</script> </script>
</head> </head>
<body> <body>
<div id="container" style="width: min(500px, calc(100vw - 10px))"> <div id="container" style="width: min(500px, calc(100vw - 10px))">
<sl-input id="short"> <sl-input id="short" value="{{ .path }}">
<sl-icon id="short-icon" name="type" slot="suffix"></sl-icon> <sl-icon id="short-icon" name="type" slot="suffix"></sl-icon>
</sl-input> </sl-input>
<br/> <br/>