Esempio n. 1
0
// RegisterRoutes ...
func (r *DownloadResource) RegisterRoutes(parentRouter *mux.Router) {
	parentRouter.HandleFunc("/", r.Index(r.AllIndex())).Methods("GET", "HEAD")

	parentRouter.HandleFunc("/finished", r.Index(r.FinishedIndex())).Methods("GET", "HEAD")
	parentRouter.HandleFunc("/finished/stats", r.Stats(r.FinishedIndex())).Methods("GET", "HEAD")

	parentRouter.HandleFunc("/notfinished", r.Index(r.NotFinishedIndex())).Methods("GET", "HEAD")
	parentRouter.HandleFunc("/notfinished/stats", r.Stats(r.NotFinishedIndex())).Methods("GET", "HEAD")

	parentRouter.HandleFunc("/inprogress", r.Index(r.InProgressIndex())).Methods("GET", "HEAD")
	parentRouter.HandleFunc("/inprogress/stats", r.Stats(r.InProgressIndex())).Methods("GET", "HEAD")

	parentRouter.HandleFunc("/waiting", r.Index(r.WaitingIndex())).Methods("GET", "HEAD")
	parentRouter.HandleFunc("/waiting/stats", r.Stats(r.WaitingIndex())).Methods("GET", "HEAD")

	parentRouter.HandleFunc("/all", r.Index(r.AllIndex())).Methods("GET", "HEAD")
	parentRouter.HandleFunc("/all/stats", r.Stats(r.AllIndex())).Methods("GET", "HEAD")

	// regexp matches ids that look like '8671301b-49fa-416c-4bc0-2869963779e5'
	parentRouter.HandleFunc("/{id:[a-f0-9-]{36}}", r.Get()).Methods("GET", "HEAD").Name("download")
	parentRouter.HandleFunc("/{id:[a-f0-9-]{36}}", r.Delete()).Methods("DELETE").Name("download-delete")

	parentRouter.HandleFunc("/{id:[a-f0-9-]{36}}/data", r.GetData()).Methods("GET", "HEAD").Name("download-data")

	parentRouter.HandleFunc("/{id:[a-f0-9-]{36}}/verify", r.VerifyData()).Methods("GET", "HEAD").Name("download-verify")

	r.router = parentRouter
	r.linkResolver = api.NewLinkResolver(parentRouter)
}
Esempio n. 2
0
// CreateServer ...
func CreateServer(config *Config) {
	s := dh.NewServer(&dh.Config{ListenAddress: config.ListenAddress}, os.Stdout)

	//	downloadStore, err := local.NewDownloadStore(config.DownloadDataFile)

	c := rethinkdb.Config{Address: config.RethinkDBAddress,
		MaxIdle:  10,
		MaxOpen:  20,
		Database: "Downloaderd"}

	downloadStore, err := rethinkdb.NewDownloadStore(c)

	if err != nil {
		log.Printf("init-download-store-error: %v", err)
	}

	fileStore := local.NewFileStore(config.DownloadDirectory)
	//c3 := s3.Config{BucketName: "downloaderd", RegionName: "us-east-1"}
	//fileStore, err := s3.NewFileStore(c3)
	//if err != nil {
	//		log.Printf("s3-init-filestore-error: %v", err)
	//	}

	requestStore, err := local.NewRequestStore(config.RequestDataFile)
	//requestStore, err := rethinkdb.NewRequestStore(c)
	if err != nil {
		log.Printf("init-request-store-error: %v", err)
	}

	hookStore, err := local.NewHookStore(config.HookDataFile)
	//hookStore, err := rethinkdb.NewHookStore(c)
	if err != nil {
		log.Printf("init-hook-store-error: %v", err)
	}

	linkResolver := api.NewLinkResolver(s.Router)
	linkResolver.DefaultScheme = "http"
	linkResolver.DefaultHost = config.ListenAddress

	downloadService := download.NewDownloadService(downloadStore, fileStore, config.WorkerCount, config.QueueLength)
	downloadService.HookService = download.NewHookService(hookStore, linkResolver)

	requestService := download.NewRequestService(requestStore, downloadService)

	downloadResource := dh.NewDownloadResource(downloadService, linkResolver)
	s.AddResource("/download", downloadResource)

	requestResource := dh.NewRequestResource(requestService, linkResolver)
	s.AddResource("/request", requestResource)

	downloadService.Start()

	err = s.ListenAndServe()
	log.Printf("init-listen-error: %v", err)
}