This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
functions.go
153 lines (125 loc) · 3.62 KB
/
functions.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package staticbackend
import (
"net/http"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/database"
"github.com/staticbackendhq/core/function"
"github.com/staticbackendhq/core/middleware"
"github.com/staticbackendhq/core/model"
)
type functions struct {
dbName string
datastore database.Persister
}
func (f *functions) add(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
var data model.ExecData
if err := parseBody(r.Body, &data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, err := backend.DB.AddFunction(conf.Name, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (f *functions) update(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := new(struct {
ID string `json:"id"`
Code string `json:"code"`
Trigger string `json:"trigger"`
})
if err := parseBody(r.Body, &data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := backend.DB.UpdateFunction(conf.Name, data.ID, data.Code, data.Trigger); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (f *functions) del(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
name := getURLPart(r.URL.Path, 3)
if err := backend.DB.DeleteFunction(conf.Name, name); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (f *functions) exec(w http.ResponseWriter, r *http.Request) {
conf, auth, err := middleware.Extract(r, true)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
//TODONOW: this is not needed as only the fn name is required here
/*var data internal.ExecData
if err := parseBody(r.Body, &data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}*/
functionName := getURLPart(r.URL.Path, 3)
fn, err := backend.DB.GetFunctionForExecution(conf.Name, functionName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
env := &function.ExecutionEnvironment{
Auth: auth,
BaseName: conf.Name,
DataStore: backend.DB,
Search: backend.Search,
Volatile: backend.Cache,
Data: fn,
Email: backend.Emailer,
Log: backend.Log,
}
if err := env.Execute(r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (f *functions) list(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
results, err := backend.DB.ListFunctions(conf.Name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, results)
}
func (f *functions) info(w http.ResponseWriter, r *http.Request) {
conf, _, err := middleware.Extract(r, false)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
name := getURLPart(r.URL.Path, 3)
fn, err := backend.DB.GetFunctionByName(conf.Name, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
respond(w, http.StatusOK, fn)
}