Skip to content

Commit

Permalink
Merge pull request #21 from hibare/dev
Browse files Browse the repository at this point in the history
Use GoCommon
  • Loading branch information
hibare authored Jul 15, 2023
2 parents 329af19 + 9993e55 commit 4fa2bba
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 252 deletions.
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ module github.com/hibare/GoGeoIP
go 1.20

require (
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/render v1.0.2
github.com/go-chi/chi/v5 v5.0.10
github.com/go-playground/validator/v10 v10.14.1
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.5.1
github.com/hibare/GoCommon v0.0.4
github.com/oschwald/geoip2-golang v1.9.0
github.com/sirupsen/logrus v1.9.3
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
Expand All @@ -16,12 +15,12 @@ require (
)

require (
github.com/ajg/form v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/oschwald/maxminddb-golang v1.11.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/render v1.0.2 h1:4ER/udB0+fMWB2Jlf15RV3F4A2FDuYi/9f+lFttR/Lg=
github.com/go-chi/render v1.0.2/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
Expand All @@ -19,6 +15,8 @@ github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+j
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hibare/GoCommon v0.0.4 h1:NUv8o+clZQxGoShzUCY1LRdEgpPdKP0h21injTVo7AM=
github.com/hibare/GoCommon v0.0.4/go.mod h1:1YOvHY7UqXqAzF1SeczZkMyujqpzeiigAubv3XJguAw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand Down
13 changes: 5 additions & 8 deletions internal/api/handler/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
log "github.com/sirupsen/logrus"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
commonErrors "github.com/hibare/GoCommon/pkg/errors"
commonHttp "github.com/hibare/GoCommon/pkg/http"
"github.com/hibare/GoGeoIP/internal/constants"
"github.com/hibare/GoGeoIP/internal/maxmind"
)
Expand All @@ -20,15 +21,11 @@ func GeoIP(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Errorf("Error fetching record for ip %s, %s", ip, err)
if errors.Is(err, constants.ErrInvalidIP) {
render.Status(r, http.StatusBadRequest)
commonHttp.WriteErrorResponse(w, http.StatusBadRequest, err)
} else {
render.Status(r, http.StatusInternalServerError)
commonHttp.WriteErrorResponse(w, http.StatusInternalServerError, commonErrors.ErrInternalServerError)
}
render.JSON(w, r, ErrorStruct{
Message: err.Error(),
})
return
}

render.JSON(w, r, ipGeo)
commonHttp.WriteJsonResponse(w, http.StatusOK, ipGeo)
}
10 changes: 0 additions & 10 deletions internal/api/handler/healthcheck.go

This file was deleted.

5 changes: 0 additions & 5 deletions internal/api/handler/utils.go

This file was deleted.

41 changes: 0 additions & 41 deletions internal/api/middlewares/token_auth.go

This file was deleted.

9 changes: 6 additions & 3 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-playground/validator/v10"
commonHandler "github.com/hibare/GoCommon/pkg/http/handler"
commonMiddleware "github.com/hibare/GoCommon/pkg/http/middleware"
"github.com/hibare/GoGeoIP/internal/api/handler"
"github.com/hibare/GoGeoIP/internal/api/middlewares"
"github.com/hibare/GoGeoIP/internal/config"
)

Expand All @@ -32,7 +33,9 @@ func (a *App) setRouters() {
func (a *App) Get(path string, f func(w http.ResponseWriter, r *http.Request), protected bool) {
if protected {
pr := chi.NewRouter()
pr.Use(middlewares.TokenAuth)
pr.Use(func(h http.Handler) http.Handler {
return commonMiddleware.TokenAuth(h, config.Current.API.APIKeys)
})
pr.Get(path, f)
a.Router.Mount("/", pr)
} else {
Expand All @@ -56,7 +59,7 @@ func (a *App) Delete(path string, f func(w http.ResponseWriter, r *http.Request)
}

func (a *App) HealthCheck(w http.ResponseWriter, r *http.Request) {
handler.HealthCheck(w, r)
commonHandler.HealthCheck(w, r)
}

func (a *App) GeoIP(w http.ResponseWriter, r *http.Request) {
Expand Down
76 changes: 20 additions & 56 deletions internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"testing"

"github.com/hibare/GoGeoIP/internal/api/handler"
"github.com/hibare/GoGeoIP/internal/api/middlewares"
"github.com/hibare/GoCommon/pkg/errors"
commonMiddleware "github.com/hibare/GoCommon/pkg/http/middleware"
"github.com/hibare/GoGeoIP/internal/config"
"github.com/hibare/GoGeoIP/internal/constants"
"github.com/hibare/GoGeoIP/internal/testhelper"
Expand All @@ -28,62 +28,24 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func TestHealthCheckHandler(t *testing.T) {
testCases := []struct {
Name string
URL string
}{
{
Name: "URL without trailing slash",
URL: "/api/v1/health",
}, {
Name: "URL with trailing slash",
URL: "/api/v1/health/",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
r, err := http.NewRequest("GET", tc.URL, nil)

assert.NoError(t, err)

w := httptest.NewRecorder()

app.Router.ServeHTTP(w, r)

assert.Equal(t, http.StatusOK, w.Code)

expectedBody := map[string]bool{"ok": true}
responseBody := map[string]bool{}

err = json.NewDecoder(w.Body).Decode(&responseBody)

assert.NoError(t, err)

if assert.NotNil(t, responseBody) {
assert.Equal(t, responseBody, expectedBody)
}
})
}
}

func TestGeoIP400(t *testing.T) {
testCases := []struct {
Name string
URL string
expectedBody handler.ErrorStruct
expectedBody errors.Error
}{
{
Name: "URL without trailing slash",
URL: "/api/v1/ip/8.8.8",
expectedBody: handler.ErrorStruct{
expectedBody: errors.Error{
Code: http.StatusBadRequest,
Message: constants.ErrInvalidIP.Error(),
},
}, {
Name: "URL with trailing slash",
URL: "/api/v1/ip/8.8.8/",
expectedBody: handler.ErrorStruct{
expectedBody: errors.Error{
Code: http.StatusBadRequest,
Message: constants.ErrInvalidIP.Error(),
},
},
Expand All @@ -94,15 +56,15 @@ func TestGeoIP400(t *testing.T) {
r, err := http.NewRequest("GET", tc.URL, nil)
assert.NoError(t, err)

r.Header.Add(middlewares.AuthHeaderName, config.Current.API.APIKeys[0])
r.Header.Add(commonMiddleware.AuthHeaderName, config.Current.API.APIKeys[0])

w := httptest.NewRecorder()

app.Router.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Code)

responseBody := handler.ErrorStruct{}
responseBody := errors.Error{}
err = json.NewDecoder(w.Body).Decode(&responseBody)
assert.NoError(t, err)

Expand All @@ -118,19 +80,21 @@ func TestGeoIP401(t *testing.T) {
testCases := []struct {
Name string
URL string
expectedBody handler.ErrorStruct
expectedBody errors.Error
}{
{
Name: "URL without trailing slash",
URL: "/api/v1/ip",
expectedBody: handler.ErrorStruct{
Message: constants.ErrUnauthorized.Error(),
expectedBody: errors.Error{
Code: http.StatusUnauthorized,
Message: errors.ErrUnauthorized.Error(),
},
}, {
Name: "URL with trailing slash",
URL: "/api/v1/ip/",
expectedBody: handler.ErrorStruct{
Message: constants.ErrUnauthorized.Error(),
expectedBody: errors.Error{
Code: http.StatusUnauthorized,
Message: errors.ErrUnauthorized.Error(),
},
},
}
Expand All @@ -146,7 +110,7 @@ func TestGeoIP401(t *testing.T) {

assert.Equal(t, http.StatusUnauthorized, w.Code)

responseBody := handler.ErrorStruct{}
responseBody := errors.Error{}
err = json.NewDecoder(w.Body).Decode(&responseBody)
assert.NoError(t, err)

Expand Down Expand Up @@ -177,7 +141,7 @@ func TestGeoIP404(t *testing.T) {
r, err := http.NewRequest("GET", tc.URL, nil)
assert.NoError(t, err)

r.Header.Add(middlewares.AuthHeaderName, config.Current.API.APIKeys[0])
r.Header.Add(commonMiddleware.AuthHeaderName, config.Current.API.APIKeys[0])

w := httptest.NewRecorder()

Expand Down Expand Up @@ -209,7 +173,7 @@ func TestGeoIP500(t *testing.T) {
r, err := http.NewRequest("GET", tc.URL, nil)
assert.NoError(t, err)

r.Header.Add(middlewares.AuthHeaderName, config.Current.API.APIKeys[0])
r.Header.Add(commonMiddleware.AuthHeaderName, config.Current.API.APIKeys[0])

w := httptest.NewRecorder()

Expand Down Expand Up @@ -246,7 +210,7 @@ func TestGeoIP200(t *testing.T) {
r, err := http.NewRequest("GET", tc.URL, nil)
assert.NoError(t, err)

r.Header.Add(middlewares.AuthHeaderName, config.Current.API.APIKeys[0])
r.Header.Add(commonMiddleware.AuthHeaderName, config.Current.API.APIKeys[0])

w := httptest.NewRecorder()

Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"

"github.com/hibare/GoCommon/pkg/env"
"github.com/hibare/GoGeoIP/internal/constants"
"github.com/hibare/GoGeoIP/internal/env"
)

type UtilConfig struct {
Expand Down
1 change: 0 additions & 1 deletion internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
var (
ErrChecksumMismatch = errors.New("checksum Mismatch")
ErrInvalidIP = errors.New("invalid IP")
ErrUnauthorized = errors.New("unauthorized")
)

const (
Expand Down
Loading

0 comments on commit 4fa2bba

Please sign in to comment.