summaryrefslogtreecommitdiff
path: root/app/utilities.go
blob: a3c36171e10db4d691b3011c304a0788d7094ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"bytes"
	"crypto/md5"
	"encoding/gob"
	"errors"
	"fmt"
	"github.com/garyburd/redigo/redis"
	"golang.org/x/crypto/bcrypt"
	"io"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"os"
	"time"
)

/*
type errorString struct {
	s string
}

func (e *errorString) Error() string {
	return e.s
}

// New returns an error that formats as the given text.
func New(text string) error {
	return &errorString{text}
}
*/

// Returns headers as map and the content of a webpage as string
func HttpGet(url string) (*http.Response, string, error) {
	r, err := http.Get(url)
	if err != nil {
		return r, "Get request failed.", err
	}

	defer r.Body.Close()
	contents, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return r, "Reading body failed.", err
	}

	return r, string(contents), nil
}

// Hashs and returns a string (md5)
func Md5Hash(content string) string {
	h := md5.New()
	io.WriteString(h, content)
	hash := fmt.Sprintf("%x", h.Sum(nil))

	return hash
}

// Creates a random string
func RandomKey() string {
	letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	key := make([]rune, 40)
	for i := range key {
		key[i] = letters[rand.Intn(len(letters))]
	}

	return string(key)
}

var pool = newPool()

// Creates a pool with connections to Redis
func newPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:     3,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			redis_server := os.Getenv("STATUS_REDIS_SERVER")
			redis_port := os.Getenv("STATUS_REDIS_PORT")
			c, err := redis.Dial("tcp", redis_server+":"+redis_port)
			if err != nil {
				return nil, err
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}

// Hashs password with bcrypt and returns the string
func HashPassword(password string) (string, error) {
	if password == "" {
		return "", errors.New("Empty password.")
	}
	p := []byte(password)
	hash, err := bcrypt.GenerateFromPassword(p, 10)
	if err != nil {
		return "", err
	}
	return string(hash), nil
}

// Verify password and hash
func VerifyPassword(password, hash string) bool {
	if hash == "" || password == "" {
		return false
	}
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	if err != nil {
		log.Printf("%s \n", err)
		return false
	}
	return true
}

func GetBytes(key interface{}) ([]byte, error) {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(key)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}