-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcommon.go
86 lines (76 loc) · 2.15 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/pkg/errors"
)
func setCORS(w http.ResponseWriter) http.ResponseWriter {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
return w
}
func getAuthFromHeader(r *http.Request) (string, string, error) {
// <username>:<token>
authVal := r.Header.Get("Authentication")
auth := strings.Split(authVal, ":")
if len(auth) != 2 || len(auth[0]) == 0 || len(auth[1]) == 0 {
return "", "", errors.New("Invalid auth header")
}
return auth[0], auth[1], nil
}
func getSession(db *gorm.DB, r *http.Request) (*Session, error) {
username, token, err := getAuthFromHeader(r)
if err != nil {
return nil, err
}
sess := Session{}
where := "token = ? AND username = ? AND expiration > ?"
err = db.Where(where, token, username, time.Now().Unix()).First(&sess).Error
if isRecordNotFound(err) {
err = errors.New("Invalid/Expired auth token, please login first to get new auth token")
}
return &sess, err
}
func getAccountInfoByName(db *gorm.DB, username string) (*Account, error) {
var user Account
err := db.Where(&Account{Username: username}).First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func isRecordNotFound(err error) bool {
return gorm.IsRecordNotFoundError(err)
}
func replyError(err error, code int, w http.ResponseWriter) {
if err == nil {
return
}
log.Println(err)
//w.Header().Set("Content-Type", "application/json")
w = setCORS(w)
http.Error(w, err.Error(), code)
}
func reply(msg []byte, code int, w http.ResponseWriter) {
w = setCORS(w)
w.WriteHeader(code)
w.Write(msg)
}
func replyJson(obj interface{}, code int, w http.ResponseWriter) {
resp, err := json.Marshal(obj)
if err != nil {
code = http.StatusInternalServerError
reply([]byte(err.Error()), code, w)
return
}
w.Header().Set("Content-Type", "application/json")
reply(resp, code, w)
}