func (c *mcCache) connect() { if cn, err := mc.Dial("tcp", c.server); err != nil { logger.Panicf("Error connecting to memcached: %s", err) } else { c.conn = cn } }
func getMcConn(u Other) *mc.Conn { if !mcPrepared { if addr := os.Getenv("MEMCACHIER_SERVERS"); addr != "" { if m, err := mc.Dial("tcp", addr); err == nil { username := os.Getenv("MEMCACHIER_USERNAME") password := os.Getenv("MEMCACHIER_PASSWORD") if username != "" && password != "" { // only try to authenticate if both username and password are set err = m.Auth(os.Getenv("MEMCACHIER_USERNAME"), os.Getenv("MEMCACHIER_PASSWORD")) if err == nil { u.Infof("Other.getMcConn: mc.Auth ok") mcConn = m } else { u.Errorf("Other.getMcConn: mc.Auth error %v", err) } } else { // most of the case, the server does not require authentication u.Infof("Other.getMcConn: mc.Dial ok") mcConn = m } } else { u.Errorf("Other.getMcConn: mc.Dial error %v", err) } } mcPrepared = true } return mcConn }
// NewMemcachedConn creates a memcache connection for the Auth service func NewMemcachedConn(cfg *config.Config) (*mc.Conn, error) { conn, err := mc.Dial("tcp", cfg.Get("memcached_server")) if err != nil { return nil, fmt.Errorf("Unable to connect to Memcached Server: %s.", cfg.Get("memcached_server")) } if (cfg.Get("memcached_username") != "") && (cfg.Get("memcached_password") != "") { err = conn.Auth(cfg.Get("memcached_username"), cfg.Get("memcached_password")) if err != nil { return nil, fmt.Errorf("Unable to authenticate to Memcached Server: %s.", cfg.Get("memcached_server")) } } return conn, nil }
func main() { memcachedURL := getEnv("MEMCACHEDCLOUD_SERVERS", "localhost:11211") var err error cn, err = mc.Dial("tcp", memcachedURL) if err != nil { log.Fatalf("Memcached connection error: %s", err) } memcachedUsername := os.Getenv("MEMCACHEDCLOUD_USERNAME") memcachedPassword := os.Getenv("MEMCACHEDCLOUD_PASSWORD") if memcachedUsername != "" && memcachedPassword != "" { if err := cn.Auth(memcachedUsername, memcachedPassword); err != nil { log.Fatalf("Memcached auth error: %s", err) } } pgxcfg, err := pgx.ParseURI(os.Getenv("DATABASE_URL")) if err != nil { log.Fatalf("Parse URI error: %s", err) } pool, err = pgx.NewConnPool(pgx.ConnPoolConfig{ ConnConfig: pgxcfg, MaxConnections: 20, AfterConnect: func(conn *pgx.Conn) error { _, err := conn.Prepare("getPackage", `SELECT name, url FROM packages WHERE name = $1`) return err }, }) if err != nil { log.Fatalf("Connection error: %s", err) } defer pool.Close() binary, err := exec.LookPath("node") if err != nil { log.Fatalf("Could not lookup node path: %s", err) } cmd := exec.Command(binary, "--expose_gc", "index.js") env := os.Environ() env = append([]string{"PORT=3001"}, env...) cmd.Env = env cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Start(); err != nil { log.Fatalf("Could not start node: %s", err) } go func() { if err := cmd.Wait(); err != nil { log.Fatalf("Node process failed: %s", err) } }() proxy = goproxy.NewProxyHttpServer() proxy.Verbose = false proxy.NonproxyHandler = http.HandlerFunc(nonProxy) proxy.OnRequest(pathIs("/packages")).DoFunc(listPackages) proxy.OnRequest(urlHasPrefix("/packages/")).DoFunc(getPackage) port := getEnv("PORT", "3000") log.Println("Starting web server at port", port) log.Fatal(http.ListenAndServe(":"+port, proxy)) }
func main() { log.SetFlags(log.Ltime | log.Lshortfile) // // Setup environment from Heroku // port := os.Getenv("PORT") if port == "" { port = "5000" } // // pathRoot := os.Getenv("PWD") // Root of the application install // mcServers := os.Getenv("MEMCACHIER_SERVERS") mcUser := os.Getenv("MEMCACHIER_USERNAME") mcPasswd := os.Getenv("MEMCACHIER_PASSWORD") // // mongoUri := os.Getenv("MONGOLAB_URI") dbName := "jfuexample" // If no DB name specified, use "jfuexample" if mongoUri != "" { switch _, auth, _, err := mgourl.ParseURL(mongoUri); true { case err != nil: log.Fatal("Could not parse MongoDB URL:", err) case auth.Db != "": dbName = auth.Db } } else { mongoUri = "localhost" } // // Initialize MongoDB connection // log.Println("Connecting to MongoDB on", mongoUri) conn, err := mgo.Dial(mongoUri) if err != nil { log.Fatalln(err) } db := conn.DB(dbName) gfs := db.GridFS("test_foobar") store := jfu.NewMongoStore(gfs) // // Initialize Memcache connection // if mcServers == "" { mcServers = "localhost:11211" } client, err := mc.Dial("tcp", mcServers) if err != nil { log.Panic(err) } if mcUser != "" && mcPasswd != "" { client.Auth(mcUser, mcPasswd) if err != nil { log.Panic(err) } } // // Initialize UploadHandler // conf := jfu.DefaultConfig conf.MaxFileSize = 100 << 10 // 100kb uh := jfu.UploadHandler{ Prefix: "/jfu", Store: &store, Conf: &conf, Cache: client, } // // Register Handlers // http.Handle("/jfu", &uh) http.Handle("/jfu/", &uh) log.Println("Serve JFU") path := http.Dir(filepath.Join(pathRoot, "static")) http.Handle("/", http.FileServer(path)) log.Println("Serve files from ", path) // // Start the webserver // addr := "0.0.0.0:" + port log.Println("Starting webserver on", addr, "...") log.Fatal(http.ListenAndServe(addr, nil)) }