Added proper redirection, improved loading error display

This commit is contained in:
2018-11-24 04:07:39 +00:00
committed by cdramey
parent 7ebbfc8da9
commit b3ee5c5e1b
4 changed files with 47 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http"
"qurl/static"
"qurl/storage"
"qurl/qurl"
)
type RootHandler struct {
@ -39,7 +40,19 @@ func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx.ServeAPI(w, r)
default:
fmt.Printf("Path: %s\n", fname)
id, err := qurl.FromString(fname[1:len(fname)])
if err != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
q, err := ctx.Storage.GetQURLByID(id)
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %s", err.Error()),
http.StatusInternalServerError)
return
}
http.Redirect(w, r, q.URL, http.StatusPermanentRedirect)
}
}