Skip to content
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

Add logger #30

Merged
merged 2 commits into from
Dec 4, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pkg/rttmap.csv
.idea/
.vscode/

*.log
log/
*.history

Expand Down
25 changes: 19 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,46 @@ COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/scripts/ /app/scr
COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/cmd/cm-beetle /app/

#RUN /bin/bash -c "source /app/conf/setup.env"
## Logger configuration
# Set log file path (default ./cm-beetle.log)
ENV CM_BEETLE_LOG_PATH cm-beetle.log
ENV CM_BEETLE_LOG_MAX_SIZE 10
ENV CM_BEETLE_LOG_MAX_BACKUPS 3
ENV CM_BEETLE_LOG_MAX_AGE 30
ENV CM_BEETLE_LOG_COMPRESS false
# Set execution environment, such as development or production
ENV CM_BEETLE_APP_ENV production
# Set log level, such as trace, debug info, warn, error, fatal, and panic
ENV CM_BEETLE_LOG_LEVEL info

## Set system endpoints
ENV CMBEETLE_ROOT /app
ENV CBSTORE_ROOT /app
ENV CBLOG_ROOT /app
# ENV SPIDER_CALL_METHOD REST
# ENV SPIDER_REST_URL http://cb-spider:1024/spider

## Set internal DB config (SQLlite)
ENV DB_URL localhost:3306
ENV DB_DATABASE cm_beetle
ENV DB_USER cm_beetle
ENV DB_PASSWORD cm_beetle

# API Setting
## Set API access config
# ALLOW_ORIGINS (ex: https://cloud-barista.org,xxx.xxx.xxx.xxx or * for all)
ENV ALLOW_ORIGINS *
## Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
# Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
ENV ENABLE_AUTH true
ENV API_USERNAME default
ENV API_PASSWORD default

# Set period for auto control goroutine invocation
## Set period for auto control goroutine invocation
ENV AUTOCONTROL_DURATION_MS 10000

# Set SELF_ENDPOINT, if you want to access Swagger API dashboard from outside. (Ex: export SELF_ENDPOINT=xxx.xxx.xxx.xxx:8056)
## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
ENV SELF_ENDPOINT localhost:8056

# Environment variables that you don't need to touch

## Environment variables that you don't need to touch
# Swagger UI API document file path
ENV API_DOC_PATH /app/pkg/api/rest/docs/swagger.json

Expand Down
51 changes: 19 additions & 32 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"flag"
"fmt"
"os"
"strconv"
"sync"
Expand All @@ -29,15 +28,13 @@ import (
"github.com/cloud-barista/cm-beetle/pkg/core/common"

restServer "github.com/cloud-barista/cm-beetle/pkg/api/rest/server"
)

// init for main
func init() {
// profile := "cloud_conf"
// setConfig(profile)
}
// 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"
)

// setConfig get cloud settings from a config file
// // setConfig get cloud settings from a config file
// func setConfig(profile string) {
// viper.AddConfigPath(".") // optionally look for config in the working directory
// viper.AddConfigPath("./conf/") // optionally look for config in the working directory/conf/
Expand Down Expand Up @@ -86,28 +83,18 @@ func init() {
// Main Body

func main() {
fmt.Println("")

// giving a default value of "8056"
log.Info().Msg("starting CM-Beetle server")

// Set the default port number "8056" for the REST API server to listen on
port := flag.String("port", "8056", "port number for the restapiserver to listen to")
flag.Parse()

// validate arguments from flag
validationFlag := true
// validation: port
// set validationFlag to false if your number is not in [1-65535] range
if portInt, err := strconv.Atoi(*port); err == nil {
if portInt < 1 || portInt > 65535 {
validationFlag = false
}
} else {
validationFlag = false
}
if !validationFlag {
fmt.Printf("%s is not a valid port number.\n", *port)
fmt.Printf("Please retry with a valid port number (ex: -port=[1-65535]).\n")
os.Exit(1)
// Validate port
if portInt, err := strconv.Atoi(*port); err != nil || portInt < 1 || portInt > 65535 {
log.Fatal().Msgf("%s is not a valid port number. Please retry with a valid port number (ex: -port=[1-65535]).", *port)
}
log.Debug().Msgf("port number: %s", *port)

common.SpiderRestUrl = common.NVL(os.Getenv("SPIDER_REST_URL"), "http://localhost:1024/spider")
common.DragonflyRestUrl = common.NVL(os.Getenv("DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
Expand All @@ -128,25 +115,25 @@ func main() {
//masterConfigInfos = confighandler.GetMasterConfigInfos()

//Setup database (meta_db/dat/cmbeetle.s3db)
fmt.Println("")
fmt.Println("[Setup SQL Database]")

log.Info().Msg("setting SQL Database")
err := os.MkdirAll("./meta_db/dat/", os.ModePerm)
if err != nil {
fmt.Println(err.Error())
log.Error().Err(err).Msg("error creating directory")
}
log.Debug().Msgf("database file path: %s", "./meta_db/dat/cmbeetle.s3db")

// Watch config file changes
go func() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
log.Debug().Str("file", e.Name).Msg("config file changed")
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
log.Fatal().Err(err).Msg("fatal error in config file")
}
err = viper.Unmarshal(&common.RuntimeConf)
if err != nil {
panic(err)
log.Panic().Err(err).Msg("error unmarshaling runtime configuration")
}
})
}()
Expand Down
26 changes: 19 additions & 7 deletions conf/setup.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# Set CMBEETLE_ROOT based on path of setup.env relatively
## Set CMBEETLE_ROOT based on path of setup.env relatively
SCRIPT_DIR=`dirname ${BASH_SOURCE[0]-$0}`
export CMBEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd`
# Use CMBEETLE_ROOT directly if the SCRIPT_DIR does not work
# export CMBEETLE_ROOT=$HOME/go/src/github.com/cloud-barista/cm-beetle

# Set system endpoints
## Logger configuration
# Set log file path (default ./cm-beetle.log)
export CM_BEETLE_LOG_PATH=cm-beetle.log
export CM_BEETLE_LOG_MAX_SIZE=10
export CM_BEETLE_LOG_MAX_BACKUPS=3
export CM_BEETLE_LOG_MAX_AGE=30
export CM_BEETLE_LOG_COMPRESS=false
# Set execution environment, such as development or production
export CM_BEETLE_APP_ENV=development
# Set log level, such as trace, debug info, warn, error, fatal, and panic
export CM_BEETLE_LOG_LEVEL=debug

## Set system endpoints
export CBSTORE_ROOT=$CMBEETLE_ROOT
export CBLOG_ROOT=$CMBEETLE_ROOT
#export SPIDER_CALL_METHOD=REST
Expand All @@ -16,10 +28,10 @@ export DB_DATABASE=cm_beetle
export DB_USER=cm_beetlee
export DB_PASSWORD=cm_beetle

# Set API access config
## ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
## Set API access config
# ALLOW_ORIGINS (ex: https://cloud-barista.org,http://localhost:8080 or * for all)
export ALLOW_ORIGINS=*
## Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
# Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
export ENABLE_AUTH=true
export API_USERNAME=default
export API_PASSWORD=default
Expand All @@ -30,6 +42,6 @@ export AUTOCONTROL_DURATION_MS=10000
## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
export SELF_ENDPOINT=localhost:8056

# Environment variables that you don't need to touch
## Swagger UI API document file path
## Environment variables that you don't need to touch
# Swagger UI API document file path
export API_DOC_PATH=$CMBEETLE_ROOT/src/api/rest/docs/swagger.json
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
Expand All @@ -80,7 +81,7 @@ require (
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.7.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down Expand Up @@ -664,10 +666,13 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
13 changes: 10 additions & 3 deletions pkg/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package server

import (
"context"
"log"
"os/signal"
"sync"
"syscall"
Expand All @@ -38,6 +37,10 @@ import (
// echo-swagger middleware
_ "github.com/cloud-barista/cm-beetle/pkg/api/rest/docs"
echoSwagger "github.com/swaggo/echo-swagger"

// 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"
)

//var masterConfigInfos confighandler.MASTERCONFIGTYPE
Expand Down Expand Up @@ -83,6 +86,8 @@ const (
// @securityDefinitions.basic BasicAuth
func RunServer(port string) {

log.Info().Msg("Setting CM-Beetle REST API server")

e := echo.New()

// Middleware
Expand All @@ -96,10 +101,10 @@ func RunServer(port string) {

allowedOrigins := os.Getenv("ALLOW_ORIGINS")
if allowedOrigins == "" {
log.Fatal("ALLOW_ORIGINS env variable for CORS is " + allowedOrigins +
log.Fatal().Msg("allow_ORIGINS env variable for CORS is " + allowedOrigins +
". Please provide a proper value and source setup.env again. EXITING...")
// allowedOrigins = "*"
}

e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{allowedOrigins},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
Expand Down Expand Up @@ -215,6 +220,7 @@ func RunServer(port string) {
<-gracefulShutdownContext.Done()

fmt.Println("\n[Stop] CM-Beetle REST Server")
log.Info().Msg("stopping CM-Beetle REST Server")
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()

Expand All @@ -223,6 +229,7 @@ func RunServer(port string) {
}
}(&wg)

log.Info().Msg("starting CM-Beetle REST API server")
port = fmt.Sprintf(":%s", port)
if err := e.Start(port); err != nil && err != http.ErrServerClosed {
e.Logger.Panic("shuttig down the server")
Expand Down
Loading