Esempio n. 1
0
func main() {
	masterPool = simpleredis.NewConnectionPoolHost("redis-master:6379")
	defer masterPool.Close()
	slavePool = simpleredis.NewConnectionPoolHost("redis-slave:6379")
	defer slavePool.Close()

	r := mux.NewRouter()
	r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
	r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
	r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
	r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)

	n := negroni.Classic()
	n.UseHandler(r)
	n.Run(":3000")
}
Esempio n. 2
0
// Create a new *UserState that can be used for managing users.
// dbindex is the Redis database index (0 is a good default value).
// If randomseed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
// redisHostPort is host:port for the desired Redis server (can be blank for localhost)
// Also creates a new ConnectionPool.
func NewUserState(dbindex int, randomseed bool, redisHostPort string) *UserState {
	var pool *simpleredis.ConnectionPool

	// Connnect to the default redis server if redisHostPort is empty
	if redisHostPort == "" {
		redisHostPort = defaultRedisServer
	}

	// Test connection
	if err := simpleredis.TestConnectionHost(redisHostPort); err != nil {
		log.Fatalln(err.Error())
	}

	// Aquire connection pool
	pool = simpleredis.NewConnectionPoolHost(redisHostPort)

	state := new(UserState)

	state.users = simpleredis.NewHashMap(pool, "users")
	state.users.SelectDatabase(dbindex)

	state.usernames = simpleredis.NewSet(pool, "usernames")
	state.usernames.SelectDatabase(dbindex)

	state.unconfirmed = simpleredis.NewSet(pool, "unconfirmed")
	state.unconfirmed.SelectDatabase(dbindex)

	state.pool = pool

	state.dbindex = dbindex

	// For the secure cookies
	// This must happen before the random seeding, or
	// else people will have to log in again after every server restart
	state.cookieSecret = cookie.RandomCookieFriendlyString(30)

	// Seed the random number generator
	if randomseed {
		rand.Seed(time.Now().UnixNano())
	}

	// Cookies lasts for 24 hours by default. Specified in seconds.
	state.cookieTime = cookie.DefaultCookieTime

	// Default password hashing algorithm is "bcrypt+", which is the same as
	// "bcrypt", but with backwards compatibility for checking sha256 hashes.
	state.passwordAlgorithm = "bcrypt+" // "bcrypt+", "bcrypt" or "sha256"

	if pool.Ping() != nil {
		defer pool.Close()
		log.Fatalf("Error, wrong hostname, port or password. (%s does not reply to PING)\n", redisHostPort)
	}

	return state
}
Esempio n. 3
0
func main() {
	pool = simpleredis.NewConnectionPoolHost(os.Getenv("REDIS_MASTER_SERVICE_HOST") + ":" + os.Getenv("REDIS_MASTER_SERVICE_PORT"))
	defer pool.Close()

	r := mux.NewRouter()
	r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
	r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
	r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
	r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)

	n := negroni.Classic()
	n.UseHandler(r)
	n.Run(":3000")
}
Esempio n. 4
0
func main() {

	var connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")

	if connection == ":" {
		print("WARNING ::: If in kube, this is a failure: Missing env variable REDISMASTER_SERVICE_HOST")
		print("WARNING ::: Attempting to connect redis localhost.")
		connection = "127.0.0.1:6379"
	} else {
		print("Found redis master host " + os.Getenv("REDISMASTER_SERVICE_PORT"))
		connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")
	}

	println("Now connecting to : " + connection)
	/**
	 *  Create a connection pool.  ?The pool pointer will otherwise
	 *  not be of any use.?https://gist.github.com/jayunit100/1d00e6d343056401ef00
	 */
	pool = simpleredis.NewConnectionPoolHost(connection)

	println("Connection pool established : " + connection)

	defer pool.Close()

	r := mux.NewRouter()

	println("Router created ")

	/**
	 * Define a REST path.
	 *  - The parameters (key) can be accessed via mux.Vars.
	 *  - The Methods (GET) will be bound to a handler function.
	 */
	r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
	r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
	r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
	r.Path("/llen").Methods("GET").HandlerFunc(LLENHandler)

	//for dev environment, the site is one level up...

	r.PathPrefix("/").Handler(http.FileServer(http.Dir(pathToStaticContents())))

	r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)

	list := simpleredis.NewList(pool, "k8petstore")
	HandleError(nil, list.Add("jayunit100"))
	HandleError(nil, list.Add("tstclaire"))
	HandleError(nil, list.Add("rsquared"))

	// Verify that this is 3 on startup.
	infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64)
	fmt.Printf("\n=========== Starting DB has %d elements \n", infoL)
	if infoL < 3 {
		print("Not enough entries in DB.  something is wrong w/ redis querying")
		print(infoL)
		panic("Failed ... ")
	}

	println("===========  Now launching negroni...this might take a second...")
	n := negroni.Classic()
	n.UseHandler(r)
	n.Run(":3000")
	println("Done ! Web app is now running.")

}