123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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://3.38.62.211: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)
- }
- }
|