Skip to content

Commit

Permalink
update port,log,health status
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Sep 25, 2023
1 parent 90b0d9b commit ec09e8b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default ignored files
.vscode
.idea
*.iml
policy-man*
12 changes: 6 additions & 6 deletions config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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:
Expand All @@ -59,7 +59,7 @@ func LoadConf() (*Conf, error) {
}

conf := &Conf{
Log: SectionLog{},
Log: sectionLog{},
}

viper.SetConfigType("yaml")
Expand Down
6 changes: 4 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
)

const dateFormatLayout = "2006/01/02 - 15:04:05"

var isTerm bool

// nolint
Expand All @@ -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,
}
}
Expand Down
19 changes: 10 additions & 9 deletions server/handle_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
18 changes: 9 additions & 9 deletions server/handle_policy_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -67,7 +67,7 @@ func policiesEvaluateHandler(_ *config.Conf) gin.HandlerFunc {
}
}

c.JSON(200, EvalResult{
c.JSON(200, evalResult{
IsSuccessful: true,
})
return
Expand All @@ -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)
Expand All @@ -91,7 +91,7 @@ func policyEvaluateHandler(_ *config.Conf) gin.HandlerFunc {
return
}

c.JSON(200, EvalResult{
c.JSON(200, evalResult{
IsSuccessful: decision,
})
return
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ec09e8b

Please sign in to comment.