Commit 911facea authored by qiuqunfeng's avatar qiuqunfeng
Browse files

config

parent ffb317c3
package api
import (
"fmt"
"io"
"net/http"
......@@ -10,26 +9,12 @@ import (
"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
"gitlab.com/tensorsecurity-rd/waf-console/internal/utils"
"gitlab.com/tensorsecurity-rd/waf-console/pkg/generated/clientset/versioned"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"k8s.io/client-go/rest"
)
const (
DB_USER = "ivan"
DB_PASSWORD = "Mysql-ha@123"
DB_HOST = "localhost"
DB_PORT = "3306"
DB_NAME = "waf"
)
func SetRouters() *gin.Engine {
func SetRouters(db *gorm.DB) *gin.Engine {
var engine *gin.Engine
dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME)
db, err := gorm.Open(mysql.Open(dns), &gorm.Config{})
if err != nil {
panic(err)
}
if !config.Conf.Debug {
// 生产模式
......@@ -53,7 +38,7 @@ func SetRouters() *gin.Engine {
)
}
// set up trusted agents
err = engine.SetTrustedProxies([]string{"127.0.0.1"})
err := engine.SetTrustedProxies([]string{"127.0.0.1"})
if err != nil {
panic(err)
}
......
package app
import (
"fmt"
"os"
"github.com/spf13/cobra"
"gitlab.com/security-rd/go-pkg/logging"
"gitlab.com/tensorsecurity-rd/waf-console/api"
"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
// "gitlab.com/tensorsecurity-rd/waf-console/internal/config"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func NewRootCommand() *cobra.Command {
......@@ -15,13 +19,27 @@ func NewRootCommand() *cobra.Command {
Short: "Start waf-console service.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
config := LoadConfig()
debugMode := os.Getenv("DEBUG_MODE")
logging.Get().Info().Msgf("DEBUG_MODE: %s", debugMode)
if debugMode == "true" {
config.Conf.Debug = true
config.Debug = true
// config.Conf.Debug = true
}
dbConfig := config.DBConfig
var db *gorm.DB
var err error
if dbConfig != nil {
dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Database)
db, err = gorm.Open(mysql.Open(dns), &gorm.Config{})
if err != nil {
panic(err)
}
} else {
panic("dbConfig is nil")
}
e := api.SetRouters()
e := api.SetRouters(db)
return e.Run(":8080")
},
}
......
......@@ -7,8 +7,26 @@ import (
"gopkg.in/yaml.v2"
)
const (
DB_USER = "ivan"
DB_PASSWORD = "Mysql-ha@123"
DB_HOST = "localhost"
DB_PORT = "3306"
DB_NAME = "waf"
)
type Config struct {
DBConfig *DBConfig `yaml:"db_config"`
RegionConfigs []RegionConfig `yaml:"region_configs"`
Debug bool `yaml:"debug"`
}
type DBConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Database string `yaml:"database"`
}
type RegionConfig struct {
......@@ -37,6 +55,16 @@ func LoadConfig() *Config {
logging.Get().Error().Err(err).Msg("Failed to parse config file")
return nil
}
// 如果config.DBConfig为nil,则使用默认值
if config.DBConfig == nil {
config.DBConfig = &DBConfig{
User: DB_USER,
Password: DB_PASSWORD,
Host: DB_HOST,
Port: DB_PORT,
Database: DB_NAME,
}
}
return &config
}
......@@ -5,3 +5,9 @@ region_configs:
token: ""
client_cert_data: ""
client_key_data: ""
db_config:
user: ivan
password: Mysql-ha@123
host: localhost
port: 3306
database: waf
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment