Пример #1
0
func main() {
	quitChannels = make(map[string]chan struct{})
	var port = ""
	var s3Auth aws.Auth
	var s3SpecialRegion aws.Region
	var s3BucketName string
	_, err := cfenv.Current()
	if err != nil {
		port = "80"
		s3Auth = aws.Auth{
			AccessKey: os.Getenv("S3_ACCESS_KEY"),
			SecretKey: os.Getenv("S3_SECRET_KEY"),
		}
		s3SpecialRegion = aws.Region{
			Name:       "Special",
			S3Endpoint: os.Getenv("S3_ENDPOINT"),
		}
		s3BucketName = os.Getenv("S3_BUCKET")
	} else {
		cfenv := cfenv.CurrentEnv()
		port = os.Getenv("PORT")
		//port = os.Getenv("VCAP_APP_PORT")
		s3Auth = aws.Auth{
			AccessKey: cfenv["S3_ACCESS_KEY"],
			SecretKey: cfenv["S3_SECRET_KEY"],
		}
		s3SpecialRegion = aws.Region{
			Name:       "Special",
			S3Endpoint: cfenv["S3_ENDPOINT"],
		}
		s3BucketName = cfenv["S3_BUCKET"]
	}

	s3Client := s3.New(s3Auth, s3SpecialRegion)
	s3Bucket = s3Client.Bucket(s3BucketName)

	go gc()

	// See http://godoc.org/github.com/unrolled/render
	rendering = render.New(render.Options{Directory: "app/templates"})

	// See http://www.gorillatoolkit.org/pkg/mux
	router := mux.NewRouter()
	router.HandleFunc("/", Index).Methods("GET")
	router.Handle("/api/v1/start", appHandler(Start)).Methods("POST")
	router.Handle("/api/v1/stop", appHandler(Stop)).Methods("POST")
	router.Handle("/api/v1/status", appHandler(Status)).Methods("POST")
	router.PathPrefix("/app/").Handler(http.StripPrefix("/app/", http.FileServer(http.Dir("app"))))

	n := negroni.Classic()
	n.UseHandler(RecoverHandler(router))
	//http.ListenAndServeTLS(":" + port, "fe1b47ba5bcb246b.crt", "connectspeople.com.key", n)
	n.Run(":" + port)

	fmt.Printf("Listening on port " + port)
}
Пример #2
0
func Start(w http.ResponseWriter, r *http.Request) *appError {
	decoder := json.NewDecoder(r.Body)
	var s map[string]string
	err := decoder.Decode(&s)
	if err != nil {
		fmt.Println(err)
	}

	userS3Auth := aws.Auth{
		AccessKey: s["accesskey"],
		SecretKey: s["secretkey"],
	}

	userS3SpecialRegion := aws.Region{
		Name:       "Special",
		S3Endpoint: s["endpoint"],
	}

	userS3BucketName := s["bucket"]

	userS3Client := s3.New(userS3Auth, userS3SpecialRegion)
	userS3Bucket := userS3Client.Bucket(userS3BucketName)

	userS3ObjectSize, err := strconv.Atoi(s["objectsize"])
	if err != nil {
		log.Fatal(err)
	}

	userKeywords := s["keywords"]

	client := twitterstream.NewClient(s["twitterconsumerkey"], s["twitterconsumersecret"], s["twitteraccesstoken"], s["twitteraccesssecret"])

	if isJobRunning(userS3Bucket, userKeywords) {
		return &appError{err: err, status: http.StatusForbidden, json: "Job already running"}
	} else {
		writeJobObject(userS3Bucket, userKeywords)
		jobChannel := make(chan []byte)
		str := s["accesskey"] + s["secretkey"]
		h := sha1.New()
		h.Write([]byte(str))
		hString := hex.EncodeToString(h.Sum(nil))
		key := hString + "_" + s["bucket"] + "." + s["keywords"]
		quitChannel := make(chan struct{})
		quitChannels[key] = quitChannel
		go worker(quitChannel, jobChannel, userS3ObjectSize, userS3Bucket, userKeywords)
		go job(quitChannel, jobChannel, client, userKeywords)
	}

	return nil
}