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.

100 lines
2.9 KiB

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "time"
  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 txCollection *mgo.Collection
  14. var addressCollection *mgo.Collection
  15. var nodeCollection *mgo.Collection
  16. var edgeCollection *mgo.Collection
  17. var dateCountCollection *mgo.Collection
  18. var hourCountCollection *mgo.Collection
  19. func main() {
  20. savelog()
  21. //read goBlockchainDataAbalysis config
  22. readConfig("config.json")
  23. //connect with mongodb
  24. //readMongodbConfig("./mongodbConfig.json")
  25. session, err := getSession()
  26. check(err)
  27. blockCollection = getCollection(session, "blocks")
  28. txCollection = getCollection(session, "txs")
  29. addressCollection = getCollection(session, "address")
  30. nodeCollection = getCollection(session, "nodes")
  31. edgeCollection = getCollection(session, "edges")
  32. dateCountCollection = getCollection(session, "dateCounts")
  33. hourCountCollection = getCollection(session, "hourCounts")
  34. if len(os.Args) > 1 {
  35. if os.Args[1] == "-explore" {
  36. // create new client instance
  37. client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
  38. HTTPPostMode: true,
  39. DisableTLS: true,
  40. Host: config.Host + ":" + config.Port,
  41. User: config.User,
  42. Pass: config.Pass,
  43. }, nil)
  44. if err != nil {
  45. log.Fatalf("error creating new btc client: %v", err)
  46. }
  47. // list accounts
  48. accounts, err := client.ListAccounts()
  49. if err != nil {
  50. log.Fatalf("error listing accounts: %v", err)
  51. }
  52. // iterate over accounts (map[string]btcutil.Amount) and write to stdout
  53. for label, amount := range accounts {
  54. log.Printf("%s: %s", label, amount)
  55. }
  56. color.Blue("starting to explore blockchain")
  57. start := time.Now()
  58. explore(client, config.GenesisBlock)
  59. log.Println("blockchain exploration finished, time:")
  60. log.Println(time.Since(start))
  61. // Get the current block count.
  62. blockCount, err := client.GetBlockCount()
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. log.Printf("Block count: %d", blockCount)
  67. }
  68. }
  69. //run thw webserver
  70. go webserver()
  71. //http server start
  72. //readServerConfig("./serverConfig.json")
  73. log.Println("server running")
  74. log.Print("port: ")
  75. log.Println(config.Server.ServerPort)
  76. router := NewRouter()
  77. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  78. originsOk := handlers.AllowedOrigins([]string{"*"})
  79. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  80. log.Fatal(http.ListenAndServe(":"+config.Server.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  81. //log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
  82. }
  83. func webserver() {
  84. log.Println("webserver in port " + config.Server.WebServerPort)
  85. http.Handle("/", http.FileServer(http.Dir("./web")))
  86. http.ListenAndServe(":"+config.Server.WebServerPort, nil)
  87. }