diff --git a/Dockerfile b/Dockerfile index ef0ccd6..b0fd28e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,7 +32,7 @@ COPY scripts scripts # Configuration file : aims at making your variables suit your environment (local, docker, prod, etc) -COPY local_search.json /etc/oc/search.json +COPY docker_search.json /etc/oc/search.json # FROM golang diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..165d78d --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,57 @@ +package conf + +import ( + "sync" + + "github.com/beego/beego/logs" + "github.com/goraz/onion" +) + +type Config struct { + OcCatalogUrl string +} + +var instance *Config +var once sync.Once + +const defaultConfigFile = "/etc/oc/search.json" +const localConfigFile = "./local_search.json" + + +func init(){ + + configFile := "" + var o *onion.Onion + + l3 := onion.NewEnvLayerPrefix("_", "OCSEARCH_") + l2, err := onion.NewFileLayer(defaultConfigFile, nil) + if err == nil { + logs.Info("Config file found : " + defaultConfigFile) + configFile = defaultConfigFile + } + l1, err := onion.NewFileLayer(localConfigFile, nil) + if err == nil { + logs.Info("Local config file found " + localConfigFile + ", overriding default file") + configFile = localConfigFile + } + if configFile == "" { + logs.Info("No config file found, using env") + o = onion.New(l3) + } else if l1 == nil && l2 == nil { + o = onion.New(l1, l2, l3) + } else if l1 == nil { + o = onion.New(l2, l3) + } else if l2 == nil { + o = onion.New(l1, l3) + } + + GetConfig().OcCatalogUrl = o.GetStringDefault("oc-catalog", "localhost:49618") + +} + +func GetConfig() *Config { + once.Do(func() { + instance = &Config{} + }) + return instance +} diff --git a/controllers/default.go b/controllers/default.go index e8ebd55..6f27e68 100644 --- a/controllers/default.go +++ b/controllers/default.go @@ -2,8 +2,9 @@ package controllers import ( OCCatalog_cli "oc-search/api-client/oc-catalog" - "oc-search/models" + conf "oc-search/conf" + "github.com/beego/beego/logs" beego "github.com/beego/beego/v2/server/web" ) @@ -14,10 +15,20 @@ type MainController struct { var OCCatalogAPI *OCCatalog_cli.APIClient -func CreateOCCatalogAPI() { - config := models.GetConfig() - // auth := context.WithValue(context.Background(), OCCatalog_cli.ContextAPIKey, OCCatalog_cli.APIKey{Key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2MTI1OTI3MDIsInVzZXJfaWQiOiJ4ZCJ9.kTLb1FtpdnaobUpe5u9Jw8S7Cc6gf7ExmU4U3XMcC2o"}) - OCCatalogAPI = OCCatalog_cli.NewAPIClient(&OCCatalog_cli.Configuration{BasePath: "http://"+config.OcCatalogUrl+"/v1", +// This method should not be called init() because it will +// lead to the parameter BasePath to not be able to +// retrieve its value from a config file, that will +// be read after the init has happened (at import of the package) + +func init() { + + // OcCatalogURL := conf.GetConfig().OcCatalogUrl + // if OcCatalogURL == "" { + // panic("OcCatalogUrl is blank") + // } + + // auth := context.WithValue(context.Background(), OCCatalog_cli.ContextAPIKey, OCCatalog_cli.APIKey{Key: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2MTI1OTI3MDIsInVzZXJfaWQiOiJ4ZCJ9.kTLb1FtpdnaobUpe5u9Jw8S7Cc6gf7ExmU4U3XMcC2o"}) + OCCatalogAPI = OCCatalog_cli.NewAPIClient(&OCCatalog_cli.Configuration{BasePath: "http://"+ conf.GetConfig().OcCatalogUrl +"/v1", DefaultHeader: map[string]string{ "authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE2MTMwMDI0NjAsInVzZXJfaWQiOiJhc2QifQ.TXT18aeulnCrtedKKFVaD0BapOTdVAFcJJdVS7zk0I8", }, @@ -27,6 +38,9 @@ func CreateOCCatalogAPI() { // Get provides the main page func (c *MainController) Get() { + + logs.Debug("received : %v",c.Ctx.Input.RequestBody) + c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" diff --git a/controllers/search.go b/controllers/search.go index 2b8a5db..b18bf70 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -35,6 +35,10 @@ func loadSingleFile(filename string) []models.Resource { // Get implements simple search func (c *SearchController) Get() { + + logs.Debug("received : %v",c.Ctx.Input.RequestBody) + + query := c.GetString("q") var resources OCCatalog_cli.ModelsSearchResult diff --git a/controllers/workflow.go b/controllers/workflow.go index 991e963..1ad7ddc 100644 --- a/controllers/workflow.go +++ b/controllers/workflow.go @@ -7,6 +7,7 @@ import ( "os" "strings" + "github.com/beego/beego/logs" beego "github.com/beego/beego/v2/server/web" "github.com/sbabiv/xml2map" ) @@ -19,6 +20,8 @@ type WorkflowController struct { // Get i func (c *WorkflowController) Get() { + logs.Debug("received : %v",c.Ctx.Input.RequestBody) + c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "workflow.tpl" diff --git a/docker-compose.yml b/docker-compose.yml index 4aa6d99..b4d620f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.4' services: oc-search: - build: . + image: oc-search container_name: oc-search ports: - 8080:8080 diff --git a/docker_search.json b/docker_search.json new file mode 100644 index 0000000..c8eff2b --- /dev/null +++ b/docker_search.json @@ -0,0 +1,3 @@ +{ + "oc-catalog" : "oc-catalog:49618" +} \ No newline at end of file diff --git a/local_search.json b/local_search.json new file mode 100644 index 0000000..d2d79e9 --- /dev/null +++ b/local_search.json @@ -0,0 +1,3 @@ +{ + "oc-catalog" : "localhost:49618" +} \ No newline at end of file diff --git a/main.go b/main.go index 365766d..a3e061e 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,18 @@ package main import ( - "oc-search/controllers" - "oc-search/models" + conf "oc-search/conf" _ "oc-search/routers" _ "oc-search/views" "github.com/beego/beego/logs" beego "github.com/beego/beego/v2/server/web" - "github.com/goraz/onion" ) -const defaultConfigFile = "/etc/oc/search.json" -const localConfigFile = "./search.json" func main() { loadConfig() - controllers.CreateOCCatalogAPI() beego.BConfig.WebConfig.Session.SessionOn = true beego.SetStaticPath("/favicon.ico", "/static/favicon.ico") @@ -33,30 +28,13 @@ func loadConfig(){ log := logs.NewLogger(10000) log.SetLogger("console") - configFile := "" - var o *onion.Onion - l3 := onion.NewEnvLayerPrefix("_", "OCDISCOVERY_") - l2, err := onion.NewFileLayer(defaultConfigFile, nil) - if err == nil { - logs.Info("Config file found : " + defaultConfigFile) - configFile = defaultConfigFile - } - l1, err := onion.NewFileLayer(localConfigFile, nil) - if err == nil { - logs.Info("Local config file found " + localConfigFile + ", overriding default file") - configFile = localConfigFile - } - if configFile == "" { - logs.Info("No config file found, using env") - o = onion.New(l3) - } else if l1 == nil && l2 == nil { - o = onion.New(l1, l2, l3) - } else if l1 == nil { - o = onion.New(l2, l3) - } else if l2 == nil { - o = onion.New(l1, l3) - } - models.GetConfig().OcCatalogUrl = o.GetStringDefault("oc-catalog", "localhost:49618") - logs.Info("Value for oc-catalog url : " + models.GetConfig().OcCatalogUrl) + beego.BConfig.WebConfig.Session.SessionOn = true + beego.SetStaticPath("/favicon.ico", "/static/favicon.ico") + beego.BConfig.WebConfig.DirectoryIndex = true + beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" + + logs.Info("Will contact oc-catalog at : %v", conf.GetConfig().OcCatalogUrl) + + beego.Run() }