(service *Service, listenAddr string, version string)
| 663 | } |
| 664 | |
| 665 | func startAPI(service *Service, listenAddr string, version string) (server *http.Server, err error) { |
| 666 | statikFS, err := fs.New() |
| 667 | if err != nil { |
| 668 | log.WithError(err).Fatal("unable to create statik fs") |
| 669 | } |
| 670 | |
| 671 | router := mux.NewRouter() |
| 672 | fsh := http.FileServer(statikFS) |
| 673 | router.Handle("/", fsh) |
| 674 | router.Handle("/static/{type}/{file}", fsh) |
| 675 | router.PathPrefix("/dbs").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 676 | r2 := new(http.Request) |
| 677 | *r2 = *request |
| 678 | r2.URL = new(url.URL) |
| 679 | r2.URL.Path = "/" |
| 680 | fsh.ServeHTTP(writer, r2) |
| 681 | }) |
| 682 | router.HandleFunc("/version", func(rw http.ResponseWriter, r *http.Request) { |
| 683 | sendResponse(http.StatusOK, true, nil, map[string]interface{}{ |
| 684 | "version": version, |
| 685 | }, rw) |
| 686 | }).Methods("GET") |
| 687 | |
| 688 | api := &explorerAPI{ |
| 689 | service: service, |
| 690 | } |
| 691 | apiRouter := router.PathPrefix(apiProxyPrefix).Subrouter() |
| 692 | v1Router := apiRouter.PathPrefix("/v1").Subrouter() |
| 693 | v1Router.HandleFunc("/ack/{db}/{hash}", api.GetAck).Methods("GET") |
| 694 | v1Router.HandleFunc("/offset/{db}/{offset:[0-9]+}", |
| 695 | func(writer http.ResponseWriter, request *http.Request) { |
| 696 | sendResponse(500, false, fmt.Sprintf("not supported in %v", version), nil, writer) |
| 697 | }, |
| 698 | ).Methods("GET") |
| 699 | v1Router.HandleFunc("/request/{db}/{hash}", api.GetRequest).Methods("GET") |
| 700 | v1Router.HandleFunc("/block/{db}/{hash}", api.GetBlock).Methods("GET") |
| 701 | v1Router.HandleFunc("/count/{db}/{count:[0-9]+}", api.GetBlockByCount).Methods("GET") |
| 702 | v1Router.HandleFunc("/height/{db}/{height:[0-9]+}", api.GetBlockByHeight).Methods("GET") |
| 703 | v1Router.HandleFunc("/head/{db}", api.GetHighestBlock).Methods("GET") |
| 704 | v2Router := apiRouter.PathPrefix("/v2").Subrouter() |
| 705 | v2Router.HandleFunc("/head/{db}", api.GetHighestBlockV2).Methods("GET") |
| 706 | v3Router := apiRouter.PathPrefix("/v3").Subrouter() |
| 707 | v3Router.HandleFunc("/response/{db}/{hash}", api.GetResponse).Methods("GET") |
| 708 | v3Router.HandleFunc("/block/{db}/{hash}", api.GetBlockV3).Methods("GET") |
| 709 | v3Router.HandleFunc("/count/{db}/{count:[0-9]+}", api.GetBlockByCountV3).Methods("GET") |
| 710 | v3Router.HandleFunc("/height/{db}/{height:[0-9]+}", api.GetBlockByHeightV3).Methods("GET") |
| 711 | v3Router.HandleFunc("/head/{db}", api.GetHighestBlockV3).Methods("GET") |
| 712 | v3Router.HandleFunc("/subscriptions", api.GetAllSubscriptions).Methods("GET") |
| 713 | |
| 714 | server = &http.Server{ |
| 715 | Addr: listenAddr, |
| 716 | WriteTimeout: apiTimeout * 10, |
| 717 | ReadTimeout: apiTimeout, |
| 718 | IdleTimeout: apiTimeout, |
| 719 | Handler: handlers.CORS( |
| 720 | handlers.AllowedHeaders([]string{"Content-Type"}), |
| 721 | )(router), |
| 722 | } |
no test coverage detected