package pages import ( "delayed.link/storage" _ "embed" "errors" "fmt" "github.com/google/uuid" "net/http" "time" ) func Get(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") uid, err := uuid.Parse(id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } l, err := storage.Current.Load(uid) if err != nil && errors.Is(err, storage.NotFoundError) { // if not found error, return 404 http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } else if err != nil { // else return internal error http.Error(w, err.Error(), http.StatusInternalServerError) return } valid := l.Valid() if errors.Is(valid, storage.NotFoundError) { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } if errors.Is(valid, storage.NoOpensError) { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } if errors.Is(valid, storage.NotYetOpenError) { http.Error(w, http.StatusText(http.StatusLocked), http.StatusLocked) msg := fmt.Sprintf("\n\nThis link will be usable in %v minutes.", int(l.OpensFrom.Sub(time.Now()).Minutes())) w.Write([]byte(msg)) return } err = l.Use() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } http.Redirect(w, r, l.Target, http.StatusFound) }