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.

80 lines
2.1 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. func main() {
  16. //read goBlockchainDataAbalysis config
  17. readConfig("config.json")
  18. //connect with mongodb
  19. readMongodbConfig("./mongodbConfig.json")
  20. session, err := getSession()
  21. check(err)
  22. blockCollection = getCollection(session, "blocks")
  23. nodeCollection = getCollection(session, "nodes")
  24. edgeCollection = getCollection(session, "edges")
  25. // create new client instance
  26. client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
  27. HTTPPostMode: true,
  28. DisableTLS: true,
  29. Host: config.Host + ":" + config.Port,
  30. User: config.User,
  31. Pass: config.Pass,
  32. }, nil)
  33. if err != nil {
  34. log.Fatalf("error creating new btc client: %v", err)
  35. }
  36. // list accounts
  37. accounts, err := client.ListAccounts()
  38. if err != nil {
  39. log.Fatalf("error listing accounts: %v", err)
  40. }
  41. // iterate over accounts (map[string]btcutil.Amount) and write to stdout
  42. for label, amount := range accounts {
  43. log.Printf("%s: %s", label, amount)
  44. }
  45. if len(os.Args) > 1 {
  46. if os.Args[1] == "-explore" {
  47. color.Blue("starting to explore blockchain")
  48. explore(client, config.GenesisBlock)
  49. }
  50. }
  51. // Get the current block count.
  52. blockCount, err := client.GetBlockCount()
  53. if err != nil {
  54. log.Fatal(err)
  55. }
  56. log.Printf("Block count: %d", blockCount)
  57. //http server start
  58. readServerConfig("./serverConfig.json")
  59. color.Green("server running")
  60. fmt.Print("port: ")
  61. color.Green(serverConfig.ServerPort)
  62. router := NewRouter()
  63. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  64. originsOk := handlers.AllowedOrigins([]string{"*"})
  65. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  66. log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  67. //log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
  68. }