Пример #1
0
func CfInfo(w http.ResponseWriter, req *http.Request) {
	appEnv, err := cfenv.Current()
	if err != nil {
		panic("source the env")
	}

	r := "Binary INFO:\n"
	r += fmt.Sprintf("\n\nENV Variables - execute\n")
	r += fmt.Sprintf("cf logs %s\n", appEnv.Name)
	r += fmt.Sprintf("cf logs --recent %s\n", appEnv.Name)

	fmt.Fprintln(w, r)

	log.Printf("\n\nENV Variables\n")
	for _, e := range os.Environ() {
		log.Printf("%s\n", e)
	}

	log.Printf("\n\nCF Variables\n")
	log.Printf("ID:%s\n", appEnv.ID)
	log.Printf("Index:%s\n", appEnv.Index)
	log.Printf("Name:%s\n", appEnv.Name)
	log.Printf("Host:%s\n", appEnv.Host)
	log.Printf("Port:%s\n", appEnv.Port)
	log.Printf("Version:%s\n", appEnv.Version)
	log.Printf("Home:%s\n", appEnv.Home)
	log.Printf("MemoryLimit:%s\n", appEnv.MemoryLimit)
	log.Printf("WorkingDir:%s\n", appEnv.WorkingDir)
	log.Printf("TempDir:%s\n", appEnv.TempDir)
	log.Printf("User:%s\n", appEnv.User)
	log.Printf("Services:%s\n", appEnv.Services)
	log.Printf("\nALL:%v\n", appEnv)

	return
}
Пример #2
0
func main() {
	appEnv, _ := cfenv.Current()
	m := martini.Classic()
	newRelic := new(integrations.MyNewRelic).New(appEnv)
	gorelic.InitNewrelicAgent(newRelic.Key, newRelic.App, false)
	m.Use(gorelic.Handler)
	oauth2Client := new(integrations.MyOAuth2).New(appEnv)
	pez.ClientID = oauth2Client.ID
	pez.ClientSecret = oauth2Client.Secret
	rds := new(integrations.MyRedis).New(appEnv)
	emailServer := pez.NewEmailServerFromService(appEnv)
	m.Map(emailServer)
	defer rds.Conn.Close()
	pez.InitSession(m, &redisCreds{
		pass: rds.Pass,
		uri:  rds.URI,
	})
	h := new(integrations.MyHeritage).New(appEnv)
	heritageClient := &heritage{
		Client:   ccclient.New(h.LoginTarget, h.LoginUser, h.LoginPass, new(http.Client)),
		ccTarget: h.CCTarget,
	}
	mngo := new(integrations.MyMongo).New(appEnv)
	defer mngo.Session.Close()

	if _, err := heritageClient.Login(); err == nil {
		pez.InitRoutes(m, rds.Conn, mngo, heritageClient)
		m.Run()

	} else {
		panic(fmt.Sprintf("heritage client login error: %s", err.Error()))
	}
}
Пример #3
0
func getAppEnv() (appEnv *cfenv.App) {
	var err error

	if appEnv, err = cfenv.Current(); err != nil {
		panic(err)
	}
	return
}
Пример #4
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)
}
Пример #5
0
func getMariaService() string {
	appEnv, _ := cfenv.Current()
	s, err := appEnv.Services.WithName("kka-mariadb")
	if err != nil {
		log.Printf("NO SERVICE %s ", err)
	} else {

		log.Printf("s.name=%s\n", s.Name)
		log.Printf("s.label=%s\n", s.Label)
		log.Printf("s.tage=%v\n", s.Tags)
		log.Printf("s.plan=%s\n", s.Plan)
		for k, v := range s.Credentials {
			log.Printf("s.Credentials.%s : %v \n", k, v)
		}
	}
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
		s.Credentials["username"], s.Credentials["password"],
		s.Credentials["host"], s.Credentials["port"],
		s.Credentials["database"])
}
Пример #6
0
func GetMongoSession() *mgo.Session {
	var mongoURI string
	// Get the VCAP env
	appEnv, _ := cfenv.Current()
	if appEnv != nil {
		// Get services
		services, _ := appEnv.Services.WithTag("mongo")
		// Get mongo service info
		mongoURI = fmt.Sprint(services[0].Credentials["uri"])
	} else {
		log.Println("Defaulting to local mongo database")
		mongoURI = "mongodb://localhost"
	}
	// Setup session
	s, err := mgo.Dial(mongoURI)
	// Check if connection error, is mongo running?
	if err != nil {
		panic(err)
	}
	return s
}
Пример #7
0
func connect(res http.ResponseWriter, req *http.Request) {

	fmt.Fprintln(res, "<html><head><title>Sample go dashDB application</title></head><body>")

	// Fetch dashDB service details
	appEnv, _ := cfenv.Current()
	services, error := appEnv.Services.WithLabel("dashDB")

	if error != nil {
		fmt.Fprintln(res, "<h4>No dashDB service instance bound to the applicaiton. Please bind a dashDB service instance and retry.</h4>")
	} else {
		dashDB := services[0]

		connStr := []string{"DATABASE=", dashDB.Credentials["db"], ";", "HOSTNAME=", dashDB.Credentials["hostname"], ";",
			"PORT=", dashDB.Credentials["port"], ";", "PROTOCOL=TCPIP", ";", "UID=", dashDB.Credentials["username"], ";", "PWD=", dashDB.Credentials["password"]}
		conn := strings.Join(connStr, "")

		db, err := sql.Open("db2-cli", conn)
		if err != nil {

			fmt.Fprintln(res, "<h3>")
			fmt.Fprintln(res, err)
			fmt.Fprintln(res, "</h3>")
			return
		}

		defer db.Close()

		var (
			first_name string
			last_name  string
		)

		stmt, err := db.Prepare("SELECT FIRST_NAME, LAST_NAME from GOSALESHR.employee FETCH FIRST 10 ROWS ONLY")
		if err != nil {
			fmt.Fprintln(res, "<h3>")
			fmt.Fprintln(res, err)
			fmt.Fprintln(res, "</h3>")
			return
		}
		defer stmt.Close()

		rows, err := stmt.Query()
		if err != nil {
			fmt.Fprintln(res, "<h3>")
			fmt.Fprintln(res, err)
			fmt.Fprintln(res, "</h3>")
			return
		}
		defer rows.Close()
		fmt.Fprintln(res, "<h3>Query: <br>SELECT FIRST_NAME, LAST_NAME from GOSALESHR.employee FETCH FIRST 10 ROWS ONLY</h3>")
		fmt.Fprintln(res, "<h3>Result:<br></h3><table border=\"1\"><tr><th>First Name</th><th>Last Name</th></tr>")
		for rows.Next() {
			err := rows.Scan(&first_name, &last_name)
			if err != nil {
				fmt.Fprintln(res, "<h3>")
				fmt.Fprintln(res, err)
				fmt.Fprintln(res, "</h3>")
				return

			}
			fmt.Fprintln(res, "<tr><td>", first_name, "</td><td>", last_name, "</td></tr>")
		}
	}
	fmt.Fprintln(res, "</table></body></html>")
}