add API controllers

This commit is contained in:
mr
2026-04-10 15:20:41 +02:00
parent 74919994c2
commit e3e29295ee
6 changed files with 226 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"oc-datacenter/infrastructure/monitor"
@@ -9,6 +10,7 @@ import (
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/live"
"cloud.o-forge.io/core/oc-lib/models/utils"
beego "github.com/beego/beego/v2/server/web"
"github.com/gorilla/websocket"
@@ -161,6 +163,51 @@ func (o *DatacenterController) Delete() {
o.ServeJSON()
}
func GetResource(typ oclib.LibDataEnum) interface{} {
if typ == oclib.LibDataEnum(oclib.LIVE_DATACENTER) {
return &live.LiveDatacenter{}
}
if typ == oclib.LibDataEnum(oclib.LIVE_STORAGE) {
return &live.LiveStorage{}
}
return &live.AbstractLive{}
}
// @Title Search
// @Description search workspace
// @Param is_draft query string false
// @Param offset query string false
// @Param limit query string false
// @Param data body json true "body for data content (Json format)"
// @Success 200 {workspace} models.workspace
// @router /:type/extended/search [post]
func (o *DatacenterController) SearchExtended() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUIDLibDataEnum
isDraft := o.Ctx.Input.Query("is_draft")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
}
fmt.Println(res, oclib.FiltersFromFlatMap(res, GetResource(col)))
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(
oclib.FiltersFromFlatMap(res, GetResource(col)), "", isDraft == "true", int64(offset), int64(limit))
m[col.String()] = append(m[col.String()], s.Data...)
}
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
o.ServeJSON()
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true }, // allow all origins
}
@@ -187,3 +234,25 @@ func (o *DatacenterController) Log() {
ctx := monitor.StreamRegistry.Register(id)
monitors.Stream(ctx, id, 1*time.Second, conn)
}
// @Title Post
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param data body json true "body for data content (Json format)"
// @Success 200 {resource} models.resource
// @router /:type [post]
func (o *DatacenterController) Post() {
libs := o.collection(true)
if len(libs) == 0 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 500,
"err": "not a proper type",
}
}
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
o.Data["json"] = oclib.NewRequest(libs[0], user, peerID, groups, nil).StoreOne(res)
o.ServeJSON()
}