Skip to content

Commit

Permalink
Upgrade Go and use new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bfdes committed Feb 15, 2024
1 parent d159bb5 commit 1595473
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ URL shortener and redirect service [designed for](https://www.notion.so/URL-shor

### Requirements

- [Go](https://golang.org/) 1.21.*
- [Go](https://golang.org/) 1.22.*
- [Docker Engine](https://docs.docker.com/engine/) 20.*
- [Docker Compose](https://docs.docker.com/compose/) 2.*

Expand Down
2 changes: 1 addition & 1 deletion go.mod
@@ -1,6 +1,6 @@
module github.com/bfdes/url-shortener

go 1.21
go 1.22

require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
Expand Down
12 changes: 1 addition & 11 deletions handler.go
Expand Up @@ -9,12 +9,7 @@ import (

func RedirectHandler(service LinkService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
msg := http.StatusText(http.StatusMethodNotAllowed)
http.Error(w, msg, http.StatusMethodNotAllowed)
return
}
slug := r.URL.Path[1:]
slug := r.PathValue("slug")
url, err := service.Get(slug)
if err == ErrDecodeFailure {
msg := http.StatusText(http.StatusBadRequest)
Expand All @@ -30,11 +25,6 @@ func RedirectHandler(service LinkService) http.Handler {

func CreateLinkHandler(service LinkService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
msg := http.StatusText(http.StatusMethodNotAllowed)
http.Error(w, msg, http.StatusMethodNotAllowed)
return
}
if r.Body == nil || r.Body == http.NoBody {
msg := http.StatusText(http.StatusBadRequest)
http.Error(w, msg, http.StatusBadRequest)
Expand Down
26 changes: 0 additions & 26 deletions handler_test.go
Expand Up @@ -45,19 +45,6 @@ func TestRedirect(t *testing.T) {
}
}

func TestRedirectWrongMethod(t *testing.T) {
service := linkServiceStub{}
handler := RedirectHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPost, "/xyz", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusMethodNotAllowed {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusMethodNotAllowed, res.StatusCode)
}
}

func TestRedirectMalformedSlug(t *testing.T) {
service := linkServiceStub{
get: func(slug string) (string, error) {
Expand Down Expand Up @@ -121,19 +108,6 @@ func TestCreate(t *testing.T) {
}
}

func TestCreateWrongMethod(t *testing.T) {
service := linkServiceStub{}
handler := CreateLinkHandler(service)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPut, "/api/links", nil)
handler.ServeHTTP(recorder, req)
res := recorder.Result()
if res.StatusCode != http.StatusMethodNotAllowed {
msg := "unexpected status code: wanted %d, got %d instead"
t.Fatalf(msg, http.StatusMethodNotAllowed, res.StatusCode)
}
}

func TestCreateNoBody(t *testing.T) {
service := linkServiceStub{}
handler := CreateLinkHandler(service)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Expand Up @@ -65,8 +65,8 @@ func main() {
db := initDb(dbHost, dbPort, dbUser, dbPassword, dbName)
defer db.Close()
linkService := linkService{cache, db}
http.Handle("/api/links", CreateLinkHandler(linkService))
http.Handle("/", RedirectHandler(linkService))
http.Handle("POST /api/links", CreateLinkHandler(linkService))
http.Handle("GET /{slug...}", RedirectHandler(linkService))
port := fmt.Sprintf(":%s", port)
log.Fatal(http.ListenAndServe(port, nil))
}

0 comments on commit 1595473

Please sign in to comment.