package main import ( "database/sql" "encoding/json" "flag" "fmt" "io/ioutil" "log" "net/http" "os" "os/signal" "sync-block/collector" "sync-block/er" "sync-block/eth" "syscall" "time" _ "github.com/go-sql-driver/mysql" ) type Config struct { Db struct { Addr string DbName string ID string Password string } CollectionAmount int64 } type ConfigRipple struct { Db struct { Addr string DbName string ID string Password string } } const URL = "http://10.0.0.84:8545" //const URL = "https://mainnet.infura.io/v3/ce1c08d3371f40a7997b2bd690c52d71" func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) configPath := "" flag.StringVar(&configPath, "c", "config.json", "") flag.Parse() conf := Config{} data, err := ioutil.ReadFile(configPath) if err != nil { panic(err) } json.Unmarshal(data, &conf) log.Printf("%#v\n", conf) eth, err := eth.New(URL) db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", conf.Db.ID, conf.Db.Password, conf.Db.Addr, conf.Db.DbName)) err = db.Ping() er.Check(err) c := collector.New(db, eth, conf.CollectionAmount) ch := make(chan os.Signal) signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) go func() { for { sig := <-ch switch sig { case syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM: c.Stop() break default: fmt.Printf("Unknown signal(%d)\n", sig) } } }() configPathRipple := "" flag.StringVar(&configPathRipple, "d", "config-ripple.json", "") flag.Parse() confRipple := ConfigRipple{} dataRipple, err := ioutil.ReadFile(configPathRipple) if err != nil { panic(err) } json.Unmarshal(dataRipple, &confRipple) log.Printf("%#v\n", conf) dbRipple, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", confRipple.Db.ID, confRipple.Db.Password, confRipple.Db.Addr, confRipple.Db.DbName)) err = dbRipple.Ping() er.Check(err) go xrpUpdater(dbRipple) log.Println("sync block started") c.Run() log.Println("sync block terminated") } func xrpUpdater(db *sql.DB) { for { resp, err := http.Get("https://api.upbit.com/v1/ticker?markets=KRW-XRP") var m []map[string]interface{} var data []byte var scr float64 var ok bool if err != nil { log.Println(err) goto end } data, err = ioutil.ReadAll(resp.Body) if err != nil { log.Println(err) goto end } err = json.Unmarshal(data, &m) if err != nil { log.Println(err) goto end } if len(m) == 0 { goto end } scr, ok = m[0]["signed_change_rate"].(float64) if ok { _, err = db.Exec("update crp_token_group set updated_on = ?, curr_krw_price = ?, change_rate = ? where id = 3", time.Now().Unix(), m[0]["trade_price"], scr*100) } end: log.Println("updated xrp price") time.Sleep(time.Minute * 10) } }