Example #1
0
func (a App) Run() {
	fmt.Println(path.Join(a.ProjectPath, "tmp"))
	//a.Store = sessions.NewFilesystemStore(path.Join(a.ProjectPath, "tmp"), []byte("something-very-secret"))
	iredistore, rediserr := redistore.NewRediStore(10, "tcp", ":6379", "", []byte("elellelsdll1234l235l2345l2354sdfkjngfj2435"))

	if rediserr != nil {
		fmt.Println(rediserr)
	}
	a.Store = iredistore
	defer a.Store.Close()

	r := mux.NewRouter()

	/*dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}*/

	for k, v := range a.Routing {
		reg := regexp.MustCompile(`[^\s\t]+`)
		routeParams := reg.FindAllString(k, -1)
		method := routeParams[0]
		route := routeParams[1]
		handler := v
		r.HandleFunc(route, a.ReqWrapper(handler)).Methods(method)
	}
	r.PathPrefix("/").Handler(http.FileServer(http.Dir(a.StaticRoot)))
	http.Handle("/", r)
	fmt.Println("Started on " + "8080")
	http.ListenAndServe(":8080", nil)
}
Example #2
0
// size: maximum number of idle connections.
// network: tcp or udp
// address: host:port
// password: redis-password
// Keys are defined in pairs to allow key rotation, but the common case is to set a single
// authentication key and optionally an encryption key.
//
// The first key in a pair is used for authentication and the second for encryption. The
// encryption key can be set to nil or omitted in the last pair, but the authentication key
// is required in all pairs.
//
// It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
// if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
func NewRedisStore(size int, network, address, password string, keyPairs ...[]byte) (RedisStore, error) {
	store, err := redistore.NewRediStore(size, network, address, password, keyPairs...)
	if err != nil {
		return nil, err
	}
	return &redisStore{store}, nil
}
Example #3
0
func init() {
	var err error
	// init redis for sessions
	rediStore, err = redistore.NewRediStore(10, "tcp", ip+":6379", "", []byte("secret-key"))
	if err != nil {
		panic(err)
	}
	// init couchdb
	client, err := couchdb.NewClient("http://" + ip + ":5984/")
	if err != nil {
		panic(err)
	}
	log.Println(client.Info())
	db = database{
		Client: client,
	}
}
Example #4
0
// NewRediStore returns a new RediStore.
// size: maximum number of idle connections.
func NewRediStore(size int, network, address, password string, keyPairs []byte) *redisStore {
	store, _ := redistore.NewRediStore(size, network, address, password, keyPairs)
	return &redisStore{store}
}
func main() {
	pflag.BoolVar(&sentinelMode, "sentinel-mode", false,
		"Whether using Sentinel")
	pflag.StringVar(&redisAddress, "redis-addr", ":6379",
		"Redis address, can be ignored while setting sentinel mode")
	pflag.StringVar(&conf.MasterName, "master-name", "mymaster",
		"Redis Sentinel master name")
	pflag.StringSliceVar(&conf.Addresses, "sentinel-ips",
		[]string{"172.31.33.2:26379", "172.31.33.3:26379", "172.31.75.4:26379"},
		"Sentinel failover addresses")
	pflag.Parse()

	if sentinelMode {
		fmt.Println("block to wait connection")
		var err error
		//go func() {store, err = connectSentinel()}()
		store, err = connectSentinel()
		if err != nil {
			fmt.Println("Failed to create connection! Please contact SysOps")
			return
		}
	} else {
		//store = sessions.NewCookieStore([]byte("something-very-secret"))
		//store = sessions.NewFilesystemStore("", []byte("something-very-secret"))
		//store, err := redistore.NewRediStore(10, "tcp", ".6379", "", []byte("secret-key"))
		redistore, err := redistore.NewRediStore(10, "tcp",
			redisAddress, "", []byte("authentication-secret-key"))
		if err != nil {
			fmt.Println("Failed to ping Redis! Please contact SysOps")
			return
		}
		store = redistore
	}

	gob.Register(&Person{})

	port := os.Getenv("PORT")
	if port == "" {
		port = "80"
	}

	router := mux.NewRouter()
	router.HandleFunc("/signup/{signup}", makeHandler(signupHandler)).Methods("GET", "POST")
	router.HandleFunc("/signin/{signin}", makeHandler(signinHandler)).Methods("GET", "POST")
	router.HandleFunc("/profile/{profile}", makeHandler(profileHandler)).Methods("GET", "POST")
	router.HandleFunc("/signout/{signout}", makeHandler(signoutHandler)).Methods("GET", "POST")
	//router.HandleFunc("/index.html", makeHandler(indexHandler)).Methods("GET") // substituted by following statement
	router.HandleFunc("/{others}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		others := vars["others"]
		if m := indexRegex.FindStringSubmatch(strings.ToLower(others)); m != nil {
			indexHandler(w, r, others[:len("index")])
			return
		}
		http.NotFound(w, r)
	}).Methods("GET")
	router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			http.Redirect(w, r, "/index.html", http.StatusFound)
		}
	}).Methods("GET")

	http.Handle("/", router)
	loadTemplates()

	fmt.Printf("Listening on port %s\n", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}