IP기반 Seed 데이터 생성, 업데이트에 반영
This commit is contained in:
@@ -4,16 +4,23 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"geoip-rest/internal/geo"
|
||||
"geoip-rest/internal/importer"
|
||||
)
|
||||
|
||||
const defaultMMDBPath = "/initial_data/GeoLite2-City.mmdb"
|
||||
|
||||
type SyncConfig struct {
|
||||
MySQL MySQLConfig
|
||||
DatabaseURL string
|
||||
Backend Backend
|
||||
MMDBPath string
|
||||
LookupQuery string
|
||||
InitialCSV string
|
||||
UpdateDir string
|
||||
LogDir string
|
||||
@@ -34,6 +41,9 @@ func (c *SyncConfig) defaults() {
|
||||
if c.Schema == "" {
|
||||
c.Schema = DefaultSchema
|
||||
}
|
||||
if c.MMDBPath == "" {
|
||||
c.MMDBPath = defaultMMDBPath
|
||||
}
|
||||
if c.Logger == nil {
|
||||
c.Logger = log.Default()
|
||||
}
|
||||
@@ -90,6 +100,10 @@ func Sync(ctx context.Context, cfg SyncConfig) error {
|
||||
return fmt.Errorf("import updates: %w", err)
|
||||
}
|
||||
|
||||
if err := ensureIPGeoInfo(ctx, cfg, conn); err != nil {
|
||||
cfg.Logger.Printf("ip_geoinfo update warning: %v", err)
|
||||
}
|
||||
|
||||
cfg.Logger.Printf("sync complete (last_id=%d -> %d)", lastID, upperID)
|
||||
|
||||
if err := verifyCounts(ctx, cfg, dumper, conn, upperID); err != nil {
|
||||
@@ -116,3 +130,39 @@ func verifyCounts(ctx context.Context, cfg SyncConfig, dumper *Dumper, conn *pgx
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureIPGeoInfo(ctx context.Context, cfg SyncConfig, conn *pgx.Conn) error {
|
||||
if err := EnsureIPGeoInfoTable(ctx, conn, cfg.Schema); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ts := time.Now().In(kst()).Format("20060102-150405")
|
||||
ipListPath := filepath.Join(cfg.UpdateDir, fmt.Sprintf("public_ip_list_%s.csv", ts))
|
||||
if err := ExportPublicIPs(ctx, conn, cfg.Schema, ipListPath); err != nil {
|
||||
return fmt.Errorf("export public ip list: %w", err)
|
||||
}
|
||||
|
||||
resolver, err := ResolveBackend(geo.Config{
|
||||
Backend: geo.Backend(cfg.Backend),
|
||||
MMDBPath: cfg.MMDBPath,
|
||||
DatabaseURL: cfg.DatabaseURL,
|
||||
LookupQuery: cfg.LookupQuery,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("init resolver for ip_geoinfo: %w", err)
|
||||
}
|
||||
defer resolver.Close()
|
||||
|
||||
sqlPath := filepath.Join(cfg.UpdateDir, fmt.Sprintf("ip_geoinfo_update-%s.sql", ts))
|
||||
count, err := GenerateIPGeoInfoSQL(ctx, conn, cfg.Schema, resolver, sqlPath, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate ip_geoinfo sql: %w", err)
|
||||
}
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := ExecuteSQLFile(ctx, conn, sqlPath); err != nil {
|
||||
return fmt.Errorf("execute ip_geoinfo sql: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user