130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
|
// @APIVersion 1.0.0
|
||
|
// @Title oc-catalog API
|
||
|
// @Description Backend of the oc-search project
|
||
|
// @Contact opencloud@irt-saintexupery.com
|
||
|
//// @SecurityDefinition jwtAPIToken apiKey Authorization header "API authentication with JWT tokens"
|
||
|
|
||
|
package routers
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
"cloud.o-forge.io/core/oc-catalog/controllers"
|
||
|
"cloud.o-forge.io/core/oc-catalog/services"
|
||
|
|
||
|
"github.com/beego/beego/v2/core/logs"
|
||
|
|
||
|
bee "github.com/beego/bee/v2/generate/swaggergen"
|
||
|
|
||
|
"github.com/beego/beego/v2/adapter/swagger"
|
||
|
beego "github.com/beego/beego/v2/server/web"
|
||
|
"github.com/beego/beego/v2/server/web/context"
|
||
|
)
|
||
|
|
||
|
var rootapi swagger.Swagger
|
||
|
|
||
|
func Init() {
|
||
|
// Remove old swagger comments
|
||
|
err := os.Remove("routers/commentsRouter_controllers.go")
|
||
|
if err != nil {
|
||
|
logs.Warning("Couldn't remove comments file: " + err.Error())
|
||
|
}
|
||
|
|
||
|
ns := beego.NewNamespace("/v1",
|
||
|
beego.NSNamespace("/user",
|
||
|
beego.NSInclude(
|
||
|
&controllers.UserController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/data",
|
||
|
beego.NSInclude(
|
||
|
&controllers.DataController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/computing",
|
||
|
beego.NSInclude(
|
||
|
&controllers.ComputingController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/datacenter",
|
||
|
beego.NSInclude(
|
||
|
&controllers.DatacenterController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/storage",
|
||
|
beego.NSInclude(
|
||
|
&controllers.StorageController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/search",
|
||
|
beego.NSInclude(
|
||
|
&controllers.SearchController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/workspace",
|
||
|
beego.NSInclude(
|
||
|
&controllers.WorkspaceController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/workflow",
|
||
|
beego.NSInclude(
|
||
|
&controllers.WorkflowController{},
|
||
|
),
|
||
|
),
|
||
|
beego.NSNamespace("/schedule",
|
||
|
beego.NSInclude(
|
||
|
&controllers.ScheduleController{},
|
||
|
),
|
||
|
),
|
||
|
)
|
||
|
|
||
|
beego.AddNamespace(ns)
|
||
|
|
||
|
beego.Get("/", func(ctx *context.Context) {
|
||
|
ctx.Output.Body([]byte(services.DC_NAME))
|
||
|
})
|
||
|
|
||
|
// Force regenerate swagger before consuming the data
|
||
|
bee.GenerateDocs(".")
|
||
|
|
||
|
// Open our jsonFile
|
||
|
swaggerSchemaPath := "swagger/swagger.json"
|
||
|
swaggerFile, err := os.Open(swaggerSchemaPath)
|
||
|
// if we os.Open returns an error then handle it
|
||
|
if err != nil {
|
||
|
logs.Critical("Error opening %v: %v", swaggerSchemaPath, err)
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
// defer the closing of our jsonFile so that we can parse it later on
|
||
|
defer swaggerFile.Close()
|
||
|
|
||
|
byteValue, err := ioutil.ReadAll(swaggerFile)
|
||
|
if err != nil {
|
||
|
logs.Critical("Error reading %v: %v", swaggerSchemaPath, err)
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
json.Unmarshal(byteValue, &rootapi)
|
||
|
|
||
|
// To simplify match of Paths, we must adapt swagger path representation to beego router
|
||
|
// For example: /path/{myID} will be /path/:myID
|
||
|
for k, v := range rootapi.Paths {
|
||
|
if strings.ContainsAny(k, "{}") {
|
||
|
newKey := strings.Replace(k, "{", ":", -1)
|
||
|
newKey = strings.Replace(newKey, "}", "", -1)
|
||
|
|
||
|
rootapi.Paths[newKey] = v
|
||
|
delete(rootapi.Paths, k)
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Init some extra stuff
|
||
|
initAuthMiddleware()
|
||
|
initUglyFixes()
|
||
|
}
|