Improved conf and dockerisation

This commit is contained in:
pb 2024-02-19 17:52:08 +01:00
commit c7cf3b40fb
9 changed files with 100 additions and 38 deletions

View File

@ -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

57
conf/conf.go Normal file
View File

@ -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
}

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -2,7 +2,7 @@ version: '3.4'
services:
oc-search:
build: .
image: oc-search
container_name: oc-search
ports:
- 8080:8080

3
docker_search.json Normal file
View File

@ -0,0 +1,3 @@
{
"oc-catalog" : "oc-catalog:49618"
}

3
local_search.json Normal file
View File

@ -0,0 +1,3 @@
{
"oc-catalog" : "localhost:49618"
}

40
main.go
View File

@ -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()
}