From 67316b5054af7f621157e9f19a1b149e96881c34 Mon Sep 17 00:00:00 2001 From: Amolith Date: Sun, 5 Jun 2022 21:11:59 -0400 Subject: [PATCH] root: redirect user to destination Implements: https://todo.sr.ht/~amolith/public-tracker/9 --- root.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/root.go b/root.go index 1edbf0a9c3062862c82e9879dd1bac74b2772911..5f6d7867a1811a693e3e27b2121e6c71b2a7343e 100644 --- a/root.go +++ b/root.go @@ -5,12 +5,34 @@ import ( "io" "log" "net/http" + "strings" "text/template" "github.com/dgraph-io/badger/v3" ) func (m model) root(writer http.ResponseWriter, request *http.Request) { + path := request.URL.Path + if path != "/" { + destinationKey := strings.TrimPrefix(path, "/") + err := m.database.View(func(txn *badger.Txn) error { + destinationEntry, err := txn.Get([]byte(destinationKey)) + if err == nil { + destinationValue, err := destinationEntry.ValueCopy(nil) + if err != nil { + return err + } + log.Println("Redirecting visitor to '" + string(destinationValue) + "'") + http.Redirect(writer, request, string(destinationValue), 302) + } + return err + }) + if err != nil { + io.WriteString(writer, string(err.Error())) + } + return + } + cookie, err := request.Cookie("access_token") if err != nil { home, err := templates.ReadFile("templates/home_unauthenticated.html")