Skip to content

Commit

Permalink
some minor updates- logging and moved data access to a common place
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhan05 committed Jun 17, 2021
1 parent f62de0a commit 3f7ec44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
21 changes: 1 addition & 20 deletions middleware/ipfilter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middleware

import (
"database/sql"
"net"
"net/http"

Expand Down Expand Up @@ -39,29 +38,11 @@ func IpFilter(handler http.Handler) http.Handler {
}

func checkIfIpAddressIsBanned(ipAddress string) (bool, error) {
queriedIpFromDB, readError := getIpAddressFromDB(ipAddress)
queriedIpFromDB, readError := models.GetIpAddressFromBannedIpsTable(ipAddress)

if readError != nil {
return false, readError
}

return queriedIpFromDB != "", nil
}

func getIpAddressFromDB(ipAddressToBeQueried string) (string, error) {
row := models.DB.QueryRow(`
SELECT host(ip)
FROM bannedips
WHERE ip = $1`, ipAddressToBeQueried)

var bannedIp string
err := row.Scan(&bannedIp)

if err == sql.ErrNoRows {
return "", nil
} else if err != nil {
return "", err
}

return bannedIp, nil
}
25 changes: 25 additions & 0 deletions models/dataaccess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package models

import (
"database/sql"
"log"
)

func GetIpAddressFromBannedIpsTable(ipAddressToBeQueried string) (string, error) {
row := DB.QueryRow(`
SELECT host(ip)
FROM bannedips
WHERE ip = $1`, ipAddressToBeQueried)

var bannedIp string
err := row.Scan(&bannedIp)

if err == sql.ErrNoRows {
return "", nil
} else if err != nil {
log.Fatal(err)
return "", err
}

return bannedIp, nil
}

0 comments on commit 3f7ec44

Please sign in to comment.