Skip to content

Commit

Permalink
use new path param thing from http.NewServeMux()
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Sep 13, 2024
1 parent 2fcd541 commit 41fb0ff
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions cmd/shorturl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"os"
"strings"

"github.com/aws/aws-lambda-go/lambda"
"github.com/function61/gokit/app/aws/lambdautils"
Expand Down Expand Up @@ -56,16 +55,15 @@ func runServer(ctx context.Context) error {
func newServerHandlerWithDb(db map[string]string) http.Handler {
mux := http.NewServeMux()

mux.HandleFunc("/go/", func(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/go/") // "/go/foobar" => "foobar"
mux.HandleFunc("/go/{linkid...}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("linkid") // "/go/foobar" => "foobar"

target, found := db[id]
if !found {
http.NotFound(w, r)
return
} else {
http.Redirect(w, r, target, http.StatusFound)
}

http.Redirect(w, r, target, http.StatusFound)
})

return mux
Expand Down

0 comments on commit 41fb0ff

Please sign in to comment.