1
Fork 0

Little improvements
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jan-Lukas Else 2020-11-15 17:05:09 +01:00
parent 6e6d27beab
commit 7308548a4e
1 changed files with 5 additions and 9 deletions

14
main.go
View File

@ -35,8 +35,8 @@ func initRouter() {
appRouter.With(loginMiddleware).Get("/d", deleteFormHandler)
appRouter.With(loginMiddleware).Post("/d", deleteHandler)
appRouter.With(loginMiddleware).Get("/l", listHandler)
appRouter.HandleFunc("/{slug}", shortenedURLHandler)
appRouter.NotFound(catchAllHandler)
appRouter.Get("/{slug}", shortenedURLHandler)
appRouter.Get("/", defaultURLRedirectHandler)
}
func main() {
@ -274,7 +274,7 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {
}
if e, err := slugExists(slug); err != nil || !e {
http.Error(w, "Slug not found", http.StatusNotFound)
http.NotFound(w, r)
return
}
@ -300,7 +300,7 @@ func deleteHandler(w http.ResponseWriter, r *http.Request) {
}
if e, err := slugExists(slug); !e || err != nil {
http.Error(w, "Slug not found", http.StatusNotFound)
http.NotFound(w, r)
return
}
@ -374,10 +374,6 @@ func slugExists(slug string) (exists bool, err error) {
func shortenedURLHandler(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
if slug == "" {
catchAllHandler(w, r)
return
}
var redirectURL, typeString string
err := appDb.QueryRow("SELECT url, type FROM redirect WHERE slug = ?", slug).Scan(&redirectURL, &typeString)
@ -399,6 +395,6 @@ func shortenedURLHandler(w http.ResponseWriter, r *http.Request) {
}
}
func catchAllHandler(w http.ResponseWriter, r *http.Request) {
func defaultURLRedirectHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, viper.GetString("defaultUrl"), http.StatusTemporaryRedirect)
}