Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit a390814

Browse files
committed
changes for the CLI functions mgmt
1 parent 54fff13 commit a390814

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

function/management.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ func GetByID(db *mongo.Database, id string) (ExecData, error) {
113113
return result, nil
114114
}
115115

116+
func GetByName(db *mongo.Database, name string) (ExecData, error) {
117+
var result ExecData
118+
119+
filter := bson.M{"name": name}
120+
121+
ctx := context.Background()
122+
sr := db.Collection("sb_functions").FindOne(ctx, filter)
123+
if err := sr.Decode(&result); err != nil {
124+
return result, err
125+
} else if err := sr.Err(); err != nil {
126+
return result, err
127+
}
128+
129+
return result, nil
130+
}
131+
116132
func List(db *mongo.Database) ([]ExecData, error) {
117133
opt := &options.FindOptions{}
118134
opt.SetProjection(bson.M{"h": 0})

function/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (env *ExecutionEnvironment) addDatabaseFunctions(vm *goja.Runtime) {
126126
})
127127
vm.Set("getById", func(call goja.FunctionCall) goja.Value {
128128
if len(call.Arguments) != 2 {
129-
return vm.ToValue(Result{Content: "argument missmatch: you need 2 arguments for get(repo, id)"})
129+
return vm.ToValue(Result{Content: "argument missmatch: you need 2 arguments for get(col, id)"})
130130
}
131131
var col, id string
132132
if err := vm.ExportTo(call.Argument(0), &col); err != nil {

functions.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (f *functions) del(w http.ResponseWriter, r *http.Request) {
7676
return
7777
}
7878

79+
if err := function.Delete(curDB, name); err != nil {
80+
http.Error(w, err.Error(), http.StatusInternalServerError)
81+
return
82+
}
83+
7984
w.WriteHeader(http.StatusOK)
8085
}
8186

@@ -113,3 +118,41 @@ func (f *functions) exec(w http.ResponseWriter, r *http.Request) {
113118

114119
w.WriteHeader(http.StatusOK)
115120
}
121+
122+
func (f *functions) list(w http.ResponseWriter, r *http.Request) {
123+
conf, _, err := middleware.Extract(r, false)
124+
if err != nil {
125+
http.Error(w, err.Error(), http.StatusUnauthorized)
126+
return
127+
}
128+
129+
curDB := client.Database(conf.Name)
130+
131+
results, err := function.List(curDB)
132+
if err != nil {
133+
http.Error(w, err.Error(), http.StatusInternalServerError)
134+
return
135+
}
136+
137+
respond(w, http.StatusOK, results)
138+
}
139+
140+
func (f *functions) info(w http.ResponseWriter, r *http.Request) {
141+
conf, _, err := middleware.Extract(r, false)
142+
if err != nil {
143+
http.Error(w, err.Error(), http.StatusUnauthorized)
144+
return
145+
}
146+
147+
curDB := client.Database(conf.Name)
148+
149+
name := getURLPart(r.URL.Path, 3)
150+
151+
fn, err := function.GetByName(curDB, name)
152+
if err != nil {
153+
http.Error(w, err.Error(), http.StatusInternalServerError)
154+
return
155+
}
156+
157+
respond(w, http.StatusOK, fn)
158+
}

server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ func Start(dbHost, port string) {
162162
http.Handle("/fn/add", middleware.Chain(http.HandlerFunc(f.add), stdRoot...))
163163
http.Handle("/fn/update", middleware.Chain(http.HandlerFunc(f.update), stdRoot...))
164164
http.Handle("/fn/delete/", middleware.Chain(http.HandlerFunc(f.del), stdRoot...))
165+
http.Handle("/fn/info/", middleware.Chain(http.HandlerFunc(f.info), stdRoot...))
165166
http.Handle("/fn/exec", middleware.Chain(http.HandlerFunc(f.exec), stdAuth...))
167+
http.Handle("/fn", middleware.Chain(http.HandlerFunc(f.list), stdRoot...))
166168

167169
// ui routes
168170
webUI := ui{base: &db.Base{PublishDocument: volatile.PublishDocument}}

0 commit comments

Comments
 (0)