This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoape.go
96 lines (87 loc) · 2 KB
/
oape.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
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/buger/jsonparser"
"github.com/encima/openape"
u "github.com/encima/openape/utils"
)
func main() {
o := openape.NewServer("config")
// CUSTOM ROUTES
// j := u.ParseObject()
o.AddCustomRoute("/users/login", "POST", func(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
var res u.JSONResponse
// TODO validate model here?
body, err := ioutil.ReadAll(r.Body)
//TODO handle email login
bodyUsername, ut, _, err := jsonparser.Get(body, "username")
bodyPassword, pt, _, err := jsonparser.Get(body, "pwd")
if ut != jsonparser.NotExist && pt != jsonparser.NotExist || err == nil {
uname := string(bodyUsername[:])
pwd := string(bodyPassword[:])
getJson := fmt.Sprintf(`{
"select":{
"query":[
{
"table":"users",
"fields":[
"email",
"id",
"username",
"api_key"
]
}
]
},
"where":[
{
"field":"username",
"table":"users",
"op":"e",
"val":"%s",
"join": "a"
},
{
"field":"pwd",
"table":"users",
"op":"e",
"val":"%s"
}
]
}`, uname, pwd)
var jQuery u.JTOS
err := json.Unmarshal([]byte(getJson), &jQuery)
if err != nil {
panic(err)
}
query := u.ParseObject(jQuery)
users := o.DB.GetModels("users", query)
print(string(users.Data))
if string(users.Data) == "null" {
users.Status = 404
res = users
u.SendResponse(w, res)
} else {
_, err = jsonparser.ArrayEach(users.Data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
jsonparser.EachKey(value, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
fmt.Println(string(value))
})
})
if err != nil {
panic(err)
}
res = users
u.SendResponse(w, res)
}
} else {
res.Status = 400
//res.Data =
u.SendResponse(w, res)
}
})
o.RunServer()
}