"org/openpaas/sample/Godeps/_workspace/src/gopkg.in/redis.v3"
"org/openpaas/sample/datasource"
"org/openpaas/sample/handler"
"org/openpaas/sample/message"
type Config map[string]string
// Datasource - MySql, Cubrid, MongoDB 타입별 처리
var dbconfig *datasource.DBConfig
var mgodbconfig *datasource.MgoDBConfig
var handlers http.Handler
fmt.Println("##### Go Sample Application start!!!")
//============================================
// dbtype 정보는 시스템 프로퍼티에서 가져온다.
dbtype := os.Getenv("dbtype")
//============================================
//============================================
// Sample VCAP_SERVICE INFO Parsing
os_args := os.Getenv("VCAP_SERVICES")
readOSEnvironment(os_args)
//============================================
//============================================
config, err := ReadConfig(`config.ini`)
//============================================
// Default Redis Client Create - For Login & Logout
redis_client := initRedis(config["redis.addr"])
defer redis_client.Close()
// default dbytype = "mysql"
//endpoint := "amqps://929b5a98-6521-4da5-86c1-0fcaa2450a86:[email protected]:5671/3c14abdc-c922-428c-af70-d61eb91ef818" endpoint := readOSEnvironment_mq()
fmt.Println("########## credentials-uri:", endpoint)
mq := message.NewRabbitMQ(endpoint)
defer message.CloseMQ(mq)
maxConnection, err := strconv.Atoi(config["mysql.maxconn"])
dbconfig = datasource.NewDBConfig(config["mysql.userid"], config["mysql.userpwd"], config["mysql.dburl"], maxConnection)
fmt.Println("dbmap:", dbconfig.DBMAP)
if dbconfig.DBMAP == nil {
log.Panic("Couldn't create Database Connection properly - MySQL!!!")
// Route Path 정보와 처리 서비스 연결
handlers = handler.NewHandler(dbconfig.DBMAP, redis_client, mq.Ch)
} else if dbtype == "mongodb" {
mgodbconfig = datasource.NewMgoDBConfig(config["mongodb.userid"], config["mongodb.userpwd"], config["mongodb.dburl"])
defer mgodbconfig.CloseDb()
fmt.Println("session:", mgodbconfig.SESSION)
if mgodbconfig.SESSION == nil {
log.Panic("Couldn't create Database Connection properly - MongoDB!!!")
// Route Path 정보와 처리 서비스 연결
handlers = handler.NewMgoHandler(mgodbconfig.SESSION, redis_client, mq.Ch)
fmt.Println("No database type found.")
if err := http.ListenAndServe(fmt.Sprintf(":%v", config["server.port"]), handlers); err != nil {
func ReadConfig(filename string) (Config, error) {
// init with some bogus data
"server.ip": "127.0.0.1",
file, err := os.Open(filename)
reader := bufio.NewReader(file)
line, err := reader.ReadString('\n')
// check if the line has = sign
// and process the line. Ignore the rest.
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value = strings.TrimSpace(line[equal+1:])
func initRedis(addr string) *redis.Client {
client := redis.NewClient(&redis.Options{