Beego set up
This commit is contained in:
@@ -9,6 +9,7 @@ import "sync"
|
||||
// ===================================================
|
||||
|
||||
type Config struct {
|
||||
APIPort int
|
||||
NATSUrl string
|
||||
MongoUrl string
|
||||
MongoDatabase string
|
||||
@@ -39,12 +40,13 @@ func GetConfig() *Config {
|
||||
return instance
|
||||
}
|
||||
|
||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *Config {
|
||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string, port int) *Config {
|
||||
GetConfig().MongoUrl = mongoUrl
|
||||
GetConfig().MongoDatabase = database
|
||||
GetConfig().NATSUrl = natsUrl
|
||||
GetConfig().LokiUrl = lokiUrl
|
||||
GetConfig().LogLevel = logLevel
|
||||
GetConfig().Whitelist = true
|
||||
GetConfig().APIPort = port
|
||||
return GetConfig()
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/beego/beego/plugins/cors"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
"github.com/beego/beego/v2/server/web/context"
|
||||
"github.com/google/uuid"
|
||||
@@ -133,6 +134,7 @@ type LibData struct {
|
||||
}
|
||||
|
||||
func InitDaemon(appName string) {
|
||||
beego.BConfig.AppName = appName
|
||||
config.SetAppName(appName) // set the app name to the logger to define the main log chan
|
||||
// create a temporary console logger for init
|
||||
logs.SetLogger(logs.CreateLogger("main"))
|
||||
@@ -146,6 +148,7 @@ func InitDaemon(appName string) {
|
||||
o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
|
||||
o.GetStringDefault("LOKI_URL", ""),
|
||||
o.GetStringDefault("LOG_LEVEL", "info"),
|
||||
o.GetIntDefault("API_PORT", 8080),
|
||||
)
|
||||
// Beego init
|
||||
beego.BConfig.AppName = appName
|
||||
@@ -199,6 +202,16 @@ func ExtractTokenInfo(request http.Request) (string, string, []string) {
|
||||
|
||||
func Init(appName string) {
|
||||
InitDaemon(appName)
|
||||
beego.BConfig.Listen.HTTPPort = config.GetConfig().APIPort
|
||||
beego.BConfig.WebConfig.DirectoryIndex = true
|
||||
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
||||
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
|
||||
AllowAllOrigins: true,
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
|
||||
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
||||
AllowCredentials: true,
|
||||
}))
|
||||
api := &tools.API{}
|
||||
api.Discovered(beego.BeeApp.Handlers.GetAllControllerInfo())
|
||||
}
|
||||
@@ -222,8 +235,8 @@ func GetLogger() zerolog.Logger {
|
||||
* @param logLevel string
|
||||
* @return *Config
|
||||
*/
|
||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string) *config.Config {
|
||||
cfg := config.SetConfig(mongoUrl, database, natsUrl, lokiUrl, logLevel)
|
||||
func SetConfig(mongoUrl string, database string, natsUrl string, lokiUrl string, logLevel string, port int) *config.Config {
|
||||
cfg := config.SetConfig(mongoUrl, database, natsUrl, lokiUrl, logLevel, port)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Init : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
|
||||
|
||||
Reference in New Issue
Block a user