-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommon.go
70 lines (61 loc) · 1.69 KB
/
common.go
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
package main
import (
"fmt"
"golang.org/x/crypto/bcrypt"
"net/http"
"reflect"
"runtime"
)
func perror(err error) {
if err != nil {
_, file, line, _ := runtime.Caller(2)
fmt.Println(fmt.Sprintf("%s:%d, error: %s", file, line, err.Error()))
panic(err)
}
}
func attributes(m interface{}) map[string]reflect.Type {
typ := reflect.TypeOf(m)
// if a pointer to a struct is passed, get the type of the dereferenced object
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
// create an attribute data structure as a map of types keyed by a string.
attrs := make(map[string]reflect.Type)
// Only structs are supported so return an empty result if the passed object
// isn't a struct
if typ.Kind() != reflect.Struct {
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
attrs[p.Name] = p.Type
}
}
return attrs
}
func encryptPass(password []byte) (string, error) {
// Hashing the password with the cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(password, 10)
return string(hashedPassword), err
}
func checkPass(hashedPassword, password []byte) (bool, error) {
// Comparing the password with the hash
err := bcrypt.CompareHashAndPassword(hashedPassword, password)
// no error means success
return (err == nil), nil
}
// cloneRequest returns a clone of the provided *http.Request. The clone is a
// shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
// shallow copy of the struct
r2 := new(http.Request)
*r2 = *r
// deep copy of the Header
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}
return r2
}