Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
qiuqunfeng
waf-console
Commits
911facea
Commit
911facea
authored
Feb 05, 2025
by
qiuqunfeng
Browse files
config
parent
ffb317c3
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
20 deletions
+57
-20
api/router.go
api/router.go
+2
-17
cmd/app/cmd.go
cmd/app/cmd.go
+21
-3
cmd/app/config.go
cmd/app/config.go
+28
-0
config/config.yaml
config/config.yaml
+6
-0
No files found.
api/router.go
View file @
911facea
package
api
package
api
import
(
import
(
"fmt"
"io"
"io"
"net/http"
"net/http"
...
@@ -10,26 +9,12 @@ import (
...
@@ -10,26 +9,12 @@ import (
"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
"gitlab.com/tensorsecurity-rd/waf-console/internal/config"
"gitlab.com/tensorsecurity-rd/waf-console/internal/utils"
"gitlab.com/tensorsecurity-rd/waf-console/internal/utils"
"gitlab.com/tensorsecurity-rd/waf-console/pkg/generated/clientset/versioned"
"gitlab.com/tensorsecurity-rd/waf-console/pkg/generated/clientset/versioned"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm"
"k8s.io/client-go/rest"
"k8s.io/client-go/rest"
)
)
const
(
func
SetRouters
(
db
*
gorm
.
DB
)
*
gin
.
Engine
{
DB_USER
=
"ivan"
DB_PASSWORD
=
"Mysql-ha@123"
DB_HOST
=
"localhost"
DB_PORT
=
"3306"
DB_NAME
=
"waf"
)
func
SetRouters
()
*
gin
.
Engine
{
var
engine
*
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
{
if
!
config
.
Conf
.
Debug
{
// 生产模式
// 生产模式
...
@@ -53,7 +38,7 @@ func SetRouters() *gin.Engine {
...
@@ -53,7 +38,7 @@ func SetRouters() *gin.Engine {
)
)
}
}
// set up trusted agents
// set up trusted agents
err
=
engine
.
SetTrustedProxies
([]
string
{
"127.0.0.1"
})
err
:
=
engine
.
SetTrustedProxies
([]
string
{
"127.0.0.1"
})
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
...
cmd/app/cmd.go
View file @
911facea
package
app
package
app
import
(
import
(
"fmt"
"os"
"os"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"gitlab.com/security-rd/go-pkg/logging"
"gitlab.com/security-rd/go-pkg/logging"
"gitlab.com/tensorsecurity-rd/waf-console/api"
"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
{
func
NewRootCommand
()
*
cobra
.
Command
{
...
@@ -15,13 +19,27 @@ func NewRootCommand() *cobra.Command {
...
@@ -15,13 +19,27 @@ func NewRootCommand() *cobra.Command {
Short
:
"Start waf-console service."
,
Short
:
"Start waf-console service."
,
Args
:
cobra
.
ExactArgs
(
0
),
Args
:
cobra
.
ExactArgs
(
0
),
RunE
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
RunE
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
config
:=
LoadConfig
()
debugMode
:=
os
.
Getenv
(
"DEBUG_MODE"
)
debugMode
:=
os
.
Getenv
(
"DEBUG_MODE"
)
logging
.
Get
()
.
Info
()
.
Msgf
(
"DEBUG_MODE: %s"
,
debugMode
)
logging
.
Get
()
.
Info
()
.
Msgf
(
"DEBUG_MODE: %s"
,
debugMode
)
if
debugMode
==
"true"
{
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"
)
return
e
.
Run
(
":8080"
)
},
},
}
}
...
...
cmd/app/config.go
View file @
911facea
...
@@ -7,8 +7,26 @@ import (
...
@@ -7,8 +7,26 @@ import (
"gopkg.in/yaml.v2"
"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
{
type
Config
struct
{
DBConfig
*
DBConfig
`yaml:"db_config"`
RegionConfigs
[]
RegionConfig
`yaml:"region_configs"`
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
{
type
RegionConfig
struct
{
...
@@ -37,6 +55,16 @@ func LoadConfig() *Config {
...
@@ -37,6 +55,16 @@ func LoadConfig() *Config {
logging
.
Get
()
.
Error
()
.
Err
(
err
)
.
Msg
(
"Failed to parse config file"
)
logging
.
Get
()
.
Error
()
.
Err
(
err
)
.
Msg
(
"Failed to parse config file"
)
return
nil
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
return
&
config
}
}
config/config.yaml
View file @
911facea
...
@@ -5,3 +5,9 @@ region_configs:
...
@@ -5,3 +5,9 @@ region_configs:
token
:
"
"
token
:
"
"
client_cert_data
:
"
"
client_cert_data
:
"
"
client_key_data
:
"
"
client_key_data
:
"
"
db_config
:
user
:
ivan
password
:
Mysql-ha@123
host
:
localhost
port
:
3306
database
:
waf
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment