Skip to content

Commit

Permalink
hide database access
Browse files Browse the repository at this point in the history
  • Loading branch information
egorkaway committed Jul 21, 2024
1 parent 73c4101 commit d7dc7d1
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions code/pull_hexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,43 @@ import (
"os"
"path/filepath"

"github.com/joho/godotenv"
_ "github.com/lib/pq"
h3 "github.com/uber/h3-go/v3"
)

const (
postgresURL = "postgres://default:d1zWD7hyUFEx@ep-broken-tree-05982655-pooler.eu-central-1.aws.neon.tech:5432/verceldb?sslmode=require&options=endpoint%3Dep-broken-tree-05982655"
outputJSONFile = "reports_h3_l3.json"
openWeatherMapAPIKey = "e7e06f3f2654e34e138f3d09ea001917" // Replace with your actual API key
reportsDir = "http/reports"
outputJSONFile = "reports_h3_l3.json"
reportsDir = "http/reports"
)

type H3Data struct {
H3Index string `json:"h3_index"`
Visits int `json:"visits"`
}

func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}

func buildPostgresURL() string {
user := os.Getenv("POSTGRES_USER")
password := os.Getenv("POSTGRES_PASSWORD")
host := os.Getenv("POSTGRES_HOST")
port := os.Getenv("DB_PORT")
dbname := os.Getenv("POSTGRES_DATABASE")

return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=require", user, password, host, port, dbname)
}

func connectDB() (*sql.DB, error) {
postgresURL := buildPostgresURL()
if postgresURL == "" {
log.Fatal("Postgres URL could not be built from environment variables")
}
return sql.Open("postgres", postgresURL)
}

Expand Down Expand Up @@ -134,6 +154,11 @@ func generateGeoJSON(h3Data []H3Data) (map[string]interface{}, error) {
}

func getWeatherData(lat, lon float64) (map[string]interface{}, error) {
openWeatherMapAPIKey := os.Getenv("OPENWEATHERMAP_API_KEY")
if openWeatherMapAPIKey == "" {
log.Fatal("OPENWEATHERMAP_API_KEY not set in environment")
}

url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s&units=metric", lat, lon, openWeatherMapAPIKey)
response, err := http.Get(url)
if err != nil {
Expand Down Expand Up @@ -180,6 +205,8 @@ func arraysToInterfaces(arrays [][]float64) []interface{} {
}

func main() {
loadEnv()

// Create the reports directory if it doesn't exist
if err := os.MkdirAll(reportsDir, os.ModePerm); err != nil {
log.Fatal("Failed to create reports directory:", err)
Expand Down

0 comments on commit d7dc7d1

Please sign in to comment.