You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.4 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. mgo "gopkg.in/mgo.v2"
  8. "github.com/btcsuite/btcrpcclient"
  9. "github.com/fatih/color"
  10. "github.com/gorilla/handlers"
  11. )
  12. var blockCollection *mgo.Collection
  13. var nodeCollection *mgo.Collection
  14. var edgeCollection *mgo.Collection
  15. var dateCountCollection *mgo.Collection
  16. var hourCountCollection *mgo.Collection
  17. func main() {
  18. //read goBlockchainDataAbalysis config
  19. readConfig("config.json")
  20. //connect with mongodb
  21. readMongodbConfig("./mongodbConfig.json")
  22. session, err := getSession()
  23. check(err)
  24. blockCollection = getCollection(session, "blocks")
  25. nodeCollection = getCollection(session, "nodes")
  26. edgeCollection = getCollection(session, "edges")
  27. dateCountCollection = getCollection(session, "dateCounts")
  28. hourCountCollection = getCollection(session, "hourCounts")
  29. // create new client instance
  30. client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
  31. HTTPPostMode: true,
  32. DisableTLS: true,
  33. Host: config.Host + ":" + config.Port,
  34. User: config.User,
  35. Pass: config.Pass,
  36. }, nil)
  37. if err != nil {
  38. log.Fatalf("error creating new btc client: %v", err)
  39. }
  40. // list accounts
  41. accounts, err := client.ListAccounts()
  42. if err != nil {
  43. log.Fatalf("error listing accounts: %v", err)
  44. }
  45. // iterate over accounts (map[string]btcutil.Amount) and write to stdout
  46. for label, amount := range accounts {
  47. log.Printf("%s: %s", label, amount)
  48. }
  49. if len(os.Args) > 1 {
  50. if os.Args[1] == "-explore" {
  51. color.Blue("starting to explore blockchain")
  52. explore(client, config.GenesisBlock)
  53. }
  54. /*if os.Args[1] == "-tree" {
  55. color.Blue("starting to make tree")
  56. addressTree(client, "fY3HZxu7HFKRcYzVSTXRZpAJMP4qba2oR6")
  57. }*/
  58. }
  59. // Get the current block count.
  60. blockCount, err := client.GetBlockCount()
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. log.Printf("Block count: %d", blockCount)
  65. //http server start
  66. readServerConfig("./serverConfig.json")
  67. color.Green("server running")
  68. fmt.Print("port: ")
  69. color.Green(serverConfig.ServerPort)
  70. router := NewRouter()
  71. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  72. originsOk := handlers.AllowedOrigins([]string{"*"})
  73. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  74. log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  75. //log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
  76. }