Skip to content

Commit

Permalink
Initial changes - reading all banned ips from db
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhan05 committed Jun 23, 2021
1 parent ea98aae commit e488024
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func main() {

db := sqlx.MustConnect("pgx", dsn)
models.DB = db
models.Migrate()
models.InitializeModelsFromDB()

if *shouldMigrate {
err := models.Migrate()
Expand Down
26 changes: 26 additions & 0 deletions models/dataaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,29 @@ func GetIpAddressFromBannedIpsTable(ipAddressToBeQueried string) (string, error)

return bannedIp, nil
}

func GetAllIpAddressesFromBannedIpsTable() ([]string, error) {
rows, err := DB.Query(`
SELECT host(ip)
FROM bannedips`)

if err != nil {
log.Fatal(err)
return nil, err
}

defer rows.Close()
ipAddressList := make([]string, 0)

for rows.Next() {
var ipAddress string
scanRowErr := rows.Scan(&ipAddress)
if scanRowErr != nil {
log.Fatal(scanRowErr)
return nil, scanRowErr
}
ipAddressList = append(ipAddressList, ipAddress)
}

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

import "github.com/golang/glog"

func InitializeModelsFromDB() {
initializeBannedIpListModelFromDB()
}

func initializeBannedIpListModelFromDB() {
ipAddressList, err := GetAllIpAddressesFromBannedIpsTable()
if err != nil {
glog.Errorf("Failed to load banned ip address from the DB")
return
}

BannedIpAddresses = ipAddressList
}
20 changes: 11 additions & 9 deletions models/models.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) 2021 Orange Forum authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package models

import "github.com/jmoiron/sqlx"

var DB *sqlx.DB
// Copyright (c) 2021 Orange Forum authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package models

import "github.com/jmoiron/sqlx"

var DB *sqlx.DB

var BannedIpAddresses []string

0 comments on commit e488024

Please sign in to comment.