Skip to content

Add the proxy Middleware, enhance logger and config pkgs #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
"os"
"strconv"
"sync"
"time"

// Black import (_) is for running a package's init() function without using its other contents.
_ "github.com/cloud-barista/cm-beetle/pkg/config"
_ "github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/cloud-barista/cm-beetle/pkg/config"
"github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"

//_ "github.com/go-sql-driver/mysql"
Expand All @@ -35,8 +36,88 @@ import (
restServer "github.com/cloud-barista/cm-beetle/pkg/api/rest"
)

// NoOpLogger is an implementation of resty.Logger that discards all logs.
type NoOpLogger struct{}

func (n *NoOpLogger) Errorf(format string, v ...interface{}) {}
func (n *NoOpLogger) Warnf(format string, v ...interface{}) {}
func (n *NoOpLogger) Debugf(format string, v ...interface{}) {}

func init() {

common.SystemReady = false

// Initialize the configuration from "config.yaml" file or environment variables
config.Init()

// Initialize the logger
logger := logger.NewLogger(logger.Config{
LogLevel: viper.GetString("beetle.loglevel"),
LogWriter: viper.GetString("beetle.logwriter"),
LogFilePath: viper.GetString("beetle.logfile.path"),
MaxSize: viper.GetInt("beetle.logfile.maxsize"),
MaxBackups: viper.GetInt("beetle.logfile.maxbackups"),
MaxAge: viper.GetInt("beetle.logfile.maxage"),
Compress: viper.GetBool("beetle.logfile.compress"),
})

// Set the global logger
log.Logger = *logger

// Check Tumblebug readiness
tumblebugRestUrl := viper.GetString("beetle.tumblebug.rest.url")
url := tumblebugRestUrl + "/readyz"
isReady, err := checkReadiness(url)

if err != nil || !isReady {
log.Fatal().Err(err).Msg("Tumblebug is not ready. Exiting...")
}

log.Info().Msg("Tumblebug is ready. Initializing Beetle...")

}

func checkReadiness(url string) (bool, error) {
// Create a new resty client
client := resty.New()

// Disable Resty default logging by setting a no-op logger
client.SetLogger(&NoOpLogger{})

// Set for retries
retryMaxAttempts := 20
retryWaitTime := 3 * time.Second
retryMaxWaitTime := 80 * time.Second
// Configure retries
client.
// Set retry count to non zero to enable retries
SetRetryCount(retryMaxAttempts).
// You can override initial retry wait time.
// Default is 100 milliseconds.
SetRetryWaitTime(retryWaitTime).
// MaxWaitTime can be overridden as well.
// Default is 2 seconds.
SetRetryMaxWaitTime(retryMaxWaitTime).
// SetRetryAfter sets callback to calculate wait time between retries.
// Default (nil) implies exponential backoff with jitter
SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
attempt := resp.Request.Attempt // Current attempt number
maxAttempts := retryMaxAttempts // Maximum attempt number

log.Info().Msgf("check readiness by %s. Attempt %d/%d.",
resp.Request.URL, attempt, maxAttempts)

// Always retry after the calculated wait time
return retryWaitTime, nil
})

resp, err := client.R().Get(url)

if err != nil || resp.IsError() {
return false, err
}

return true, nil
}

// @title CM-Beetle REST API
Expand Down
55 changes: 29 additions & 26 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@ beetle:
cblog:
root: # To be set in runtime (based on beetle.root)

## Set API access config
api:
# Set API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
allow:
origins: "*"

# Set API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
auth:
enabled: true

username: default
password: default

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
self:
endpoint: localhost:8056

tumblebug:
rest:
url: http://localhost:1323/tumblebug
api:
username: default
password: default

## Set internal DB config (SQLlite)
db:
url: localhost:3306
database: cm_beetle
user: cm_beetle
password: cm_beetle

## Logger configuration
logfile:
# Set log file path (default logfile path: ./beetle.log)
path: ./beetle.log
# Set log file path (default logfile path: ./log/beetle.log)
path: ./log/beetle.log
maxsize: 10
maxbackups: 3
maxage: 30
Expand All @@ -31,30 +58,6 @@ beetle:
node:
env: development

## Set internal DB config (SQLlite)
db:
url: localhost:3306
database: cm_beetle
user: cm_beetle
password: cm_beetle

## Set API access config
api:
# Set API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
allow:
origins: "*"

# Set API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
auth:
enabled: true

username: default
password: default

## Set period for auto control goroutine invocation
autocontrol:
duration_ms: 10000

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
self:
endpoint: localhost:8056
8 changes: 4 additions & 4 deletions conf/setup.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export BEETLE_SELF_ENDPOINT=localhost:8056

## Set API access config
export BEETLE_TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug

export BEETLE_TUMBLEBUG_API_USERNAME=default
export BEETLE_TUMBLEBUG_API_PASSWORD=default

## Set internal DB config (SQLlite)
export BEETLE_SQLITE_URL=localhost:3306
Expand All @@ -27,8 +28,8 @@ export BEETLE_SQLITE_USER=cm_beetle
export BEETLE_SQLITE_PASSWORD=cm_beetle

## Logger configuration
# Set log file path (default logfile path: ./beetle.log)
export BEETLE_LOGFILE_PATH=beetle.log
# Set log file path (default logfile path: ./log/beetle.log)
export BEETLE_LOGFILE_PATH=log/beetle.log
export BEETLE_LOGFILE_MAXSIZE=10
export BEETLE_LOGFILE_MAXBACKUPS=3
export BEETLE_LOGFILE_MAXAGE=30
Expand All @@ -40,6 +41,5 @@ export BEETLE_LOGWRITER=both
# Set execution environment, such as development or production
export BEETLE_NODE_ENV=development


## Set period for auto control goroutine invocation
export BEETLE_AUTOCONTROL_DURATION_MS=10000
56 changes: 29 additions & 27 deletions conf/template-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,41 @@ beetle:
cblog:
root: # To be set in runtime (based on beetle.root)

## Set Tumblebug API endpoint
## Set API access config
api:
# Set API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
allow:
origins: "*"

# Set API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
auth:
enabled: true

username: default
password: default

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
self:
endpoint: localhost:8056

tumblebug:
rest:
url: http://localhost:1323/tumblebug
api:
username: default
password: default

## Set internal DB config (SQLlite)
db:
url: localhost:3306
database: cm_beetle
user: cm_beetle
password: cm_beetle

## Logger configuration
logfile:
# Set log file path (default logfile path: ./beetle.log)
path: ./beetle.log
# Set log file path (default logfile path: ./log/beetle.log)
path: ./log/beetle.log
maxsize: 10
maxbackups: 3
maxage: 30
Expand All @@ -32,30 +58,6 @@ beetle:
node:
env: development

## Set internal DB config (SQLlite)
db:
url: localhost:3306
database: cm_beetle
user: cm_beetle
password: cm_beetle

## Set API access config
api:
# Set API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
allow:
origins: "*"

# Set API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
auth:
enabled: true

username: default
password: default

## Set period for auto control goroutine invocation
autocontrol:
duration_ms: 10000

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
self:
endpoint: localhost:8056
36 changes: 20 additions & 16 deletions conf/template-setup.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@ export BEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd`
export BEETLE_CBSTORE_ROOT=$BEETLE_ROOT
export BEETLE_CBLOG_ROOT=$BEETLE_ROOT

## Set API access config
# BEETLE_API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
export BEETLE_API_ALLOW_ORIGINS=*
# Set BEETLE_API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
export BEETLE_API_AUTH_ENABLED=true
export BEETLE_API_USERNAME=default
export BEETLE_API_PASSWORD=default

## Set BEETLE_SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
export BEETLE_SELF_ENDPOINT=localhost:8056

## Set API access config
export BEETLE_TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug
export BEETLE_TUMBLEBUG_API_USERNAME=default
export BEETLE_TUMBLEBUG_API_PASSWORD=default

## Set internal DB config (SQLlite)
export BEETLE_SQLITE_URL=localhost:3306
export BEETLE_SQLITE_DATABASE=cm_beetle
export BEETLE_SQLITE_USER=cm_beetle
export BEETLE_SQLITE_PASSWORD=cm_beetle

## Logger configuration
# Set log file path (default logfile path: ./beetle.log)
Expand All @@ -22,22 +41,7 @@ export BEETLE_LOGWRITER=both
# Set execution environment, such as development or production
export BEETLE_NODE_ENV=development

## Set internal DB config (SQLlite)
export BEETLE_SQLITE_URL=localhost:3306
export BEETLE_SQLITE_DATABASE=cm_beetle
export BEETLE_SQLITE_USER=cm_beetle
export BEETLE_SQLITE_PASSWORD=cm_beetle

## Set API access config
# BEETLE_API_ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
export BEETLE_API_ALLOW_ORIGINS=*
# Set BEETLE_API_AUTH_ENABLED=true currently for basic auth for all routes (i.e., url or path)
export BEETLE_API_AUTH_ENABLED=true
export BEETLE_API_USERNAME=default
export BEETLE_API_PASSWORD=default

## Set period for auto control goroutine invocation
export BEETLE_AUTOCONTROL_DURATION_MS=10000

## Set BEETLE_SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
export BEETLE_SELF_ENDPOINT=localhost:8056

2 changes: 0 additions & 2 deletions pkg/api/rest/controller/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/cloud-barista/cm-beetle/pkg/core/migration"
"github.com/labstack/echo/v4"

// Black import (_) is for running a package's init() function without using its other contents.
_ "github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 0 additions & 2 deletions pkg/api/rest/controller/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/cloud-barista/cm-beetle/pkg/core/recommendation"
"github.com/labstack/echo/v4"

// Black import (_) is for running a package's init() function without using its other contents.
_ "github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/rs/zerolog/log"
)

Expand Down
Loading