func init() { server, ok := GetString("redis_ip") if !ok { log.Err("Config values missing") panic("Config values for redis missing, unable to start the server") } pass, ok := GetString("redis_pass") if !ok { log.Err("Config values missing") panic("Config values for redis missing, unable to start the server") } RedisPool = redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", server) if err != nil { return nil, err } if len(pass) != 0 { if _, err := c.Do("AUTH", pass); err != nil { c.Close() return nil, err } } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } }
func Err(w http.ResponseWriter, e *ae.Error) { log.Err(*e) log.Err(string(debug.Stack())) jsonData, err := json.Marshal(e) if err != nil { fmt.Fprint(w, "Error") } w.Header().Set("Content-Type", "application/json") w.Header().Set("X-Content-Type-Options", "nosniff") w.WriteHeader(e.HttpStatus) fmt.Fprintln(w, string(jsonData)) }
func GetGooglUserDetails(authCode string) (person *plus.Person, tok *oauth2.Token, err error) { var clientID, clientSecret string var ok bool logNDie := func() { log.Err("Googl config values missing") panic("Googl config values missing") } if clientID, ok = config.GetString("google_client_id"); !ok { logNDie() } if clientSecret, ok = config.GetString("google_sercret"); !ok { logNDie() } config := &oauth2.Config{ ClientID: clientID, ClientSecret: clientSecret, Scopes: []string{"profile"}, Endpoint: google.Endpoint, } tok, err = config.Exchange(oauth2.NoContext, authCode) if err != nil { return } client := config.Client(oauth2.NoContext, tok) var plusService *plus.Service plusService, err = plus.New(client) if err != nil { return } person, err = plusService.People.Get("me").Do() return }
func (ut *UserToken) delCache() { conn := config.RedisPool.Get() defer conn.Close() key := "Token:" + ut.Token _, err := conn.Do("DEL", key) if err != nil { log.Err(ae.Redis("", err)) } }
func (ut *UserToken) addUserIdToTokenCache() { conn := config.RedisPool.Get() defer conn.Close() key := "Token:" + ut.Token _, err := conn.Do("HMSET", redis.Args{}.Add(key).AddFlat(ut)...) if err != nil { log.Err(ae.Redis("", err)) } }
func (ut *UserToken) getUserIdFromTokenCache() bool { conn := config.RedisPool.Get() defer conn.Close() v, err := redis.Values(conn.Do("HGETALL", "Token:"+ut.Token)) if len(v) == 0 { return false } if err != nil { log.Err(ae.Redis("", err)) return false } vMap, _ := redis.StringMap(v, nil) ut.UserID, _ = strconv.ParseUint(vMap["user_id"], 10, 64) ut.RefreshToken, _ = vMap["refresh_token"] ut.Created, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 -0700", vMap["created"]) ut.Expires, _ = time.Parse("2006-01-02 15:04:05.999999999 -0700 -0700", vMap["expires"]) return true }
func init() { dbUserName, ok := GetString("pg_uname") if !ok { log.Err("Config values missing") panic("Config values missing, unable to start the server") } dbPass, ok := GetString("pg_pass") if !ok { log.Err("Config values missing") panic("Config values missing, unable to start the server") } dbIp, ok := GetString("pg_ip") if !ok { log.Err("Config values missing") panic("Config values missing, unable to start the server") } dbPortNo, ok := GetInt("pg_port") if !ok { log.Err("Config values missing") panic("Config values missing, unable to start the server") } dbName, ok := GetString("pg_name") if !ok { log.Err("Config values missing") panic("Config values missing, unable to start the server") } if dbPass != "" { dbPass = "******" + dbPass } else { dbPass = "******" } var err error dbStringSlice := []string{"postgres://", dbUserName, dbPass, "@", dbIp, ":", strconv.Itoa(dbPortNo), "/", dbName, "?sslmode=disable"} DB, err = sql.Open("postgres", strings.Join(dbStringSlice, "")) if err != nil { log.Err("Unable to connect to DB") panic("Unable to connect to DB") } }