diff --git a/cmd/communes.go b/cmd/communes.go index c015c9f..dea0c14 100644 --- a/cmd/communes.go +++ b/cmd/communes.go @@ -56,7 +56,7 @@ to quickly create a Cobra application.`, panic(err) } - index, err := mada.InitializeBleve() + index, err := mada.InitializeBleve(db) if err != nil { panic(err) } diff --git a/cmd/districts.go b/cmd/districts.go index af0d2fe..6616047 100644 --- a/cmd/districts.go +++ b/cmd/districts.go @@ -58,7 +58,7 @@ to quickly create a Cobra application.`, panic(err) } - index, err := mada.InitializeBleve() + index, err := mada.InitializeBleve(db) if err != nil { panic(err) } diff --git a/cmd/fokontany.go b/cmd/fokontany.go index 441c378..8ca6787 100644 --- a/cmd/fokontany.go +++ b/cmd/fokontany.go @@ -58,7 +58,7 @@ to quickly create a Cobra application.`, panic(err) } - index, err := mada.InitializeBleve() + index, err := mada.InitializeBleve(db) if err != nil { panic(err) } diff --git a/cmd/init.go b/cmd/init.go index 0ffb073..9b1785a 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -46,7 +46,12 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - mada.Init() + db, err := mada.OpenDatabaseConnection() + + if err != nil { + panic(err) + } + mada.Init(db) }, } diff --git a/cmd/regions.go b/cmd/regions.go index e3b0d46..1067015 100644 --- a/cmd/regions.go +++ b/cmd/regions.go @@ -58,7 +58,7 @@ to quickly create a Cobra application.`, panic(err) } - index, err := mada.InitializeBleve() + index, err := mada.InitializeBleve(db) if err != nil { panic(err) } diff --git a/cmd/search.go b/cmd/search.go index 0d72350..3256a0d 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -72,7 +72,7 @@ to quickly create a Cobra application.`, panic(err) } - index, err := mada.InitializeBleve() + index, err := mada.InitializeBleve(db) if err != nil { panic(err) } diff --git a/mada/init.go b/mada/init.go index e5eb495..b496237 100644 --- a/mada/init.go +++ b/mada/init.go @@ -62,7 +62,7 @@ type Geometry struct { //go:embed shp/* var Assets embed.FS -func Init() (bleve.Index, error) { +func Init(db *sql.DB) (bleve.Index, error) { index, err := CreateOrOpenBleve() @@ -70,23 +70,10 @@ func Init() (bleve.Index, error) { log.Fatal(err) } - var db *sql.DB - if os.Getenv("MADA_POSTGRES_URL") != "" { - db, err = OpenPostgresConnection() - - if err != nil { - log.Fatal(err) - } createPostgresTables(db) addPostgresGeometryColumns(db) } else { - db, err = OpenSQLiteConnection() - - if err != nil { - log.Fatal(err) - } - initializeSpatialMetadata(db) createTables(db) addGeometryColumns(db) diff --git a/mada/search.go b/mada/search.go index 34bc111..378e92b 100644 --- a/mada/search.go +++ b/mada/search.go @@ -40,9 +40,9 @@ func (s *SearchService) Search(term string, opt types.SearchOptions) (*types.Sea return &types.SearchResult{Result: searchResults}, err } -func InitializeBleve() (bleve.Index, error) { +func InitializeBleve(db *sql.DB) (bleve.Index, error) { if _, err := os.Stat(DATABASE_PATH); os.IsNotExist(err) { - return Init() + return Init(db) } return bleve.Open(DATABASE_PATH) } diff --git a/mada/server.go b/mada/server.go index 06da2db..6c0a4d3 100644 --- a/mada/server.go +++ b/mada/server.go @@ -35,7 +35,7 @@ func StartHttpServer(db *sql.DB) { MaxAge: 300, // Maximum value not ignored by any of major browsers }) - index, err := InitializeBleve() + index, err := InitializeBleve(db) if err != nil { panic(err) }