Skip to content

Commit

Permalink
feat: attempt to fetch the db if it was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistTheNeil committed Oct 30, 2023
1 parent 592fbf5 commit 68ac5fd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
25 changes: 25 additions & 0 deletions internal/errortypes/ErrorTypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package errortypes

type ErrorDatabaseNotFound struct {
Message string `json:"message"`
FileName string `json:"-"`
StatusCode int `json:"-"`
Err error `json:"-"`
}

func (e *ErrorDatabaseNotFound) Error() string {
return "Database file not found"
}

func (e *ErrorDatabaseNotFound) Unwrap() error {
return e.Err
}

func NewErrorDatabaseNotFound(err error, filename string) *ErrorDatabaseNotFound {
return &ErrorDatabaseNotFound{
FileName: filename,
Message: "We couldn't access the database, please contact the administrator",
StatusCode: 500,
Err: err,
}
}
20 changes: 15 additions & 5 deletions internal/maxmind/Maxmind.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package maxmind

import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"rest-geoip/internal/errortypes"
"rest-geoip/internal/fs"
"sync"

Expand Down Expand Up @@ -48,10 +50,16 @@ type Record struct {

// Open a maxmind database
func (m *DB) Open() error {
var err error
dbLocation := viper.GetString("MAXMIND_DB_LOCATION") + viper.GetString("MAXMIND_DB")
fmt.Printf("Opening db %s\n", dbLocation)

_, err := os.Stat(dbLocation)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
e := errortypes.NewErrorDatabaseNotFound(err, dbLocation)
return fmt.Errorf("maxmind.Open: db not found: %w", e)
}
}
m.db, err = maxminddb.Open(dbLocation)
if err != nil {
return fmt.Errorf("%w", err)
Expand All @@ -68,10 +76,12 @@ func (m *DB) Update() error {
if viper.GetString("MAXMIND_LICENSE") == "" {
return fmt.Errorf("Error: Can't update database when no license key is set (MAXMIND_LICENSE env var needs to be set)")
}
err := m.Close()
if err != nil {
fmt.Println("Failed to close maxmind database")
return err
if m == nil {
err := m.Close()
if err != nil {
fmt.Println("Failed to close maxmind database")
return err
}
}
if err := DownloadAndUpdate(); err != nil {
fmt.Println("Failed to update maxmind database")
Expand Down
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"errors"
"fmt"
"os"
"rest-geoip/internal/errortypes"
"rest-geoip/internal/maxmind"
"rest-geoip/internal/random"
"rest-geoip/internal/router"
Expand Down Expand Up @@ -36,10 +38,22 @@ func main() {
err := maxmind.
GetInstance().
Open()

if err != nil {
fmt.Println(err)
fmt.Println("Error: Maxmind database not opened during initialization. Please check that it exists.")
os.Exit(1)
var ErrDatabaseNotFound *errortypes.ErrorDatabaseNotFound
if errors.As(errors.Unwrap(err), &ErrDatabaseNotFound) {
fmt.Println(err)
fmt.Println("Attempting to fetch database")
fetchErr := maxmind.GetInstance().Update()
if fetchErr != nil {
fmt.Println(fetchErr)
os.Exit(1)
}
} else {

fmt.Println("Error: Maxmind database not opened during initialization. Please check that it exists and is readable.")
os.Exit(1)
}
}

router.InitRouter()
Expand Down

0 comments on commit 68ac5fd

Please sign in to comment.