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.

54 lines
1.4 KiB

  1. package main
  2. import (
  3. "log"
  4. "net"
  5. "net/http"
  6. "time"
  7. "github.com/fatih/color"
  8. "github.com/gorilla/handlers"
  9. )
  10. type Peer struct {
  11. ID string `json:"id"` //in the future, this will be the peer hash
  12. IP string `json:"ip"`
  13. Port string `json:"port"`
  14. RESTPort string `json:"restport"`
  15. Role string `json:"role"` //client or server
  16. Conn net.Conn `json:"conn"`
  17. }
  18. type PeersList struct {
  19. PeerID string
  20. Peers []Peer `json:"peerslist"`
  21. Date time.Time `json:"date"`
  22. }
  23. var peersList PeersList
  24. func main() {
  25. color.Blue("Starting CA")
  26. //read configuration file
  27. readConfig("config.json")
  28. reconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, "")
  29. //run thw webserver
  30. go webserver()
  31. //run API
  32. log.Println("api server running")
  33. log.Print("port: ")
  34. log.Println(config.Port)
  35. router := NewRouter()
  36. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  37. originsOk := handlers.AllowedOrigins([]string{"*"})
  38. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  39. log.Fatal(http.ListenAndServe(":"+config.Port, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  40. }
  41. func webserver() {
  42. log.Println("webserver in port " + config.WebServerPort)
  43. http.Handle("/", http.FileServer(http.Dir("./webapp")))
  44. http.ListenAndServe(":"+config.WebServerPort, nil)
  45. }