From c6e0ce751eecb6fd4a74f4cca0d585f0d042ed02 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Thu, 30 May 2024 13:17:43 +0700 Subject: [PATCH] Remove `import` command It's no longer needed since any new subbmision is now from xmr.ditatompel.com --- cmd/server/import.go | 95 -------------------------------------------- cmd/server/init.go | 1 - 2 files changed, 96 deletions(-) delete mode 100644 cmd/server/import.go diff --git a/cmd/server/import.go b/cmd/server/import.go deleted file mode 100644 index 7df335d..0000000 --- a/cmd/server/import.go +++ /dev/null @@ -1,95 +0,0 @@ -package server - -import ( - "encoding/json" - "fmt" - "net/http" - "xmr-remote-nodes/internal/database" - - "github.com/spf13/cobra" -) - -type importClient struct { - db *database.DB -} - -func newImport(db *database.DB) *importClient { - return &importClient{db: db} -} - -type importData struct { - Hostname string `json:"hostname"` - Port int `json:"port"` - Protocol string `json:"protocol"` - IsTor bool `json:"is_tor"` - DateEntered int `json:"date_entered"` -} - -var importCmd = &cobra.Command{ - Use: "import", - Short: "Import Monero nodes from old API", - Long: `Import Monero nodes from old API. -This command only available during migration process and will be removed in future versions.`, - Run: func(_ *cobra.Command, _ []string) { - if err := database.ConnectDB(); err != nil { - panic(err) - } - req, err := http.NewRequest(http.MethodGet, "https://api.ditatompel.com/monero/remote-node?limit=500", nil) - if err != nil { - panic(err) - } - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - panic(err) - } - defer resp.Body.Close() - - if resp.StatusCode != 200 { - panic(fmt.Errorf("status code: %d", resp.StatusCode)) - } - - response := struct { - Data []importData `json:"data"` - }{} - - err = json.NewDecoder(resp.Body).Decode(&response) - if err != nil { - panic(err) - } - - action := newImport(database.GetDB()) - - for _, node := range response.Data { - if err := action.processData(node); err != nil { - fmt.Println(err) - } - } - - fmt.Println("Total Source Data: ", len(response.Data)) - - fmt.Println("Done!") - }, -} - -func (i *importClient) processData(node importData) error { - query := `SELECT id FROM tbl_node WHERE hostname = ? AND port = ? AND protocol = ?` - row := i.db.QueryRow(query, node.Hostname, node.Port, node.Protocol) - var id int - err := row.Scan(&id) - if err == nil { - // fmt.Printf("Skipping %s://%s:%d\n", node.Protocol, node.Hostname, node.Port) - return fmt.Errorf("node already exists") - } - - // insert - query = `INSERT INTO tbl_node (hostname, port, protocol, is_tor, nettype, ip_addr, date_entered, last_check_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)` - _, err = i.db.Exec(query, node.Hostname, node.Port, node.Protocol, node.IsTor, "", "", node.DateEntered, "[2,2,2,2,2]") - if err != nil { - return err - } - - fmt.Printf("Imported %s://%s:%d\n", node.Protocol, node.Hostname, node.Port) - - return nil -} diff --git a/cmd/server/init.go b/cmd/server/init.go index bc3b80e..c218abf 100644 --- a/cmd/server/init.go +++ b/cmd/server/init.go @@ -4,7 +4,6 @@ import "xmr-remote-nodes/cmd" func init() { cmd.Root.AddCommand(serveCmd) - cmd.Root.AddCommand(importCmd) cmd.Root.AddCommand(cronCmd) cmd.Root.AddCommand(probersCmd) probersCmd.AddCommand(listProbersCmd)