From ec09e8baeb43a60d9baa0518a9b41385e06c89e7 Mon Sep 17 00:00:00 2001 From: baixinsui Date: Mon, 25 Sep 2023 17:04:52 +0800 Subject: [PATCH] update port,log,health status --- .gitignore | 5 +++++ config/conf.go | 12 ++++++------ log/log.go | 6 ++++-- server/handle_health.go | 19 ++++++++++--------- server/handle_policy_eval.go | 18 +++++++++--------- server/server.go | 2 +- 6 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25e25f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +.vscode +.idea +*.iml +policy-man* diff --git a/config/conf.go b/config/conf.go index ea20254..0560e9c 100644 --- a/config/conf.go +++ b/config/conf.go @@ -22,17 +22,17 @@ type Conf struct { ShutdownTimeout int64 `mapstructure:"shutdown_timeout"` - Log SectionLog `mapstructure:"log"` + Log sectionLog `mapstructure:"log"` - SSL SectionSSL `mapstructure:"ssl"` + SSL sectionSSL `mapstructure:"ssl"` } -type SectionLog struct { +type sectionLog struct { Level string `mapstructure:"level"` Path string `mapstructure:"path"` } -type SectionSSL struct { +type sectionSSL struct { Enable bool `mapstructure:"ssl"` KeyPath string `mapstructure:"key_path"` CertPath string `mapstructure:"cert_path"` @@ -43,7 +43,7 @@ type SectionSSL struct { var defaultConf = []byte(` mode: release host: "localhost" # ip address to bind (default: any) -port: "8080" # ignore this port number if auto_tls is enabled (listen 443). +port: "9443" # ignore this port number if auto_tls is enabled (listen 443). shutdown_timeout: 30 # default is 30 second log: @@ -59,7 +59,7 @@ func LoadConf() (*Conf, error) { } conf := &Conf{ - Log: SectionLog{}, + Log: sectionLog{}, } viper.SetConfigType("yaml") diff --git a/log/log.go b/log/log.go index 46f19af..88679e8 100644 --- a/log/log.go +++ b/log/log.go @@ -12,6 +12,8 @@ import ( "os" ) +const dateFormatLayout = "2006/01/02 - 15:04:05" + var isTerm bool // nolint @@ -33,12 +35,12 @@ func InitLog(level, path string) error { basic.SetFormatter(&logrus.JSONFormatter{}) } else { basic.Formatter = &logrus.TextFormatter{ - TimestampFormat: "2006/01/02 - 15:04:05", + TimestampFormat: dateFormatLayout, FullTimestamp: true, } basic.Formatter = &logrus.TextFormatter{ - TimestampFormat: "2006/01/02 - 15:04:05", + TimestampFormat: dateFormatLayout, FullTimestamp: true, } } diff --git a/server/handle_health.go b/server/handle_health.go index 0d67612..0fe5188 100644 --- a/server/handle_health.go +++ b/server/handle_health.go @@ -5,21 +5,22 @@ package server -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "net/http" +) -type HealthStatus string +type healthStatus string const ( - healthOk HealthStatus = "OK" + healthOK healthStatus = "OK" + healthNOK healthStatus = "NOK" ) -type HealthStatusResponse struct { - HealthStatus HealthStatus `json:"healthStatus"` +type systemStatus struct { + HealthStatus healthStatus `json:"healthStatus"` } func healthHandler(c *gin.Context) { - c.JSON(200, HealthStatusResponse{ - HealthStatus: healthOk, - }) - return + c.JSON(http.StatusOK, systemStatus{HealthStatus: healthOK}) } diff --git a/server/handle_policy_eval.go b/server/handle_policy_eval.go index 49823e0..887aaa5 100644 --- a/server/handle_policy_eval.go +++ b/server/handle_policy_eval.go @@ -20,21 +20,21 @@ import ( "strings" ) -type EvalRego struct { +type evalRego struct { Policy string `json:"policy" binding:"required"` } -type EvalCmd struct { +type evalCmd struct { Policy string `json:"policy" binding:"required"` Input string `json:"input" binding:"required"` } -type EvalCmdList struct { +type evalCmdList struct { Input string `json:"input" binding:"required"` PolicyList []string `json:"policy_list" binding:"required"` } -type EvalResult struct { +type evalResult struct { Input string `json:"input,omitempty"` Policy string `json:"policy,omitempty"` IsSuccessful bool `json:"isSuccessful"` @@ -43,7 +43,7 @@ type EvalResult struct { func policiesEvaluateHandler(_ *config.Conf) gin.HandlerFunc { return func(c *gin.Context) { - var cmdList EvalCmdList + var cmdList evalCmdList if err := c.ShouldBindWith(&cmdList, binding.JSON); err != nil { log.Debug(err) @@ -58,7 +58,7 @@ func policiesEvaluateHandler(_ *config.Conf) gin.HandlerFunc { return } if !decision { - c.JSON(200, EvalResult{ + c.JSON(200, evalResult{ IsSuccessful: false, Policy: policy, Input: cmdList.Input, @@ -67,7 +67,7 @@ func policiesEvaluateHandler(_ *config.Conf) gin.HandlerFunc { } } - c.JSON(200, EvalResult{ + c.JSON(200, evalResult{ IsSuccessful: true, }) return @@ -77,7 +77,7 @@ func policiesEvaluateHandler(_ *config.Conf) gin.HandlerFunc { func policyEvaluateHandler(_ *config.Conf) gin.HandlerFunc { return func(c *gin.Context) { - var cmd EvalCmd + var cmd evalCmd if err := c.ShouldBindWith(&cmd, binding.JSON); err != nil { log.Debug(err) @@ -91,7 +91,7 @@ func policyEvaluateHandler(_ *config.Conf) gin.HandlerFunc { return } - c.JSON(200, EvalResult{ + c.JSON(200, evalResult{ IsSuccessful: decision, }) return diff --git a/server/server.go b/server/server.go index b808d03..d424012 100644 --- a/server/server.go +++ b/server/server.go @@ -25,7 +25,7 @@ func RunHTTPServer(ctx context.Context, cfg *config.Conf) error { Handler: router(cfg), } - log.Info("HTTP server is running on" + cfg.Host + ":" + cfg.Port) + log.Info("HTTP server is running on " + cfg.Host + ":" + cfg.Port) if cfg.SSL.Enable { tlsConfig := &tls.Config{ MinVersion: tls.VersionTLS10,