diff --git a/cmd/goproxy.go b/cmd/goproxy.go index 1edb411..9047341 100755 --- a/cmd/goproxy.go +++ b/cmd/goproxy.go @@ -2,17 +2,15 @@ package main import ( "flag" - "log" "runtime" domain "github.com/kenriortega/goproxy/proxy/domain" "github.com/kenriortega/goproxy/cmd/cli" "github.com/kenriortega/goproxy/internal/infra" + "github.com/kenriortega/goproxy/internal/utils" ) -var () - var ( service = "proxy" config infra.Config @@ -23,12 +21,16 @@ var ( generateApiKey = false serverList = "" portLB = 3030 + setingFile = "goproxy.yaml" ) func init() { - config, errConfig = infra.LoadConfig(".", "goproxy.yaml") + config, errConfig = infra.LoadConfig(".", setingFile) if errConfig != nil { - log.Println(errConfig) + utils.LogError(errConfig.Error()) + utils.LogInfo("config: Creating setting file by default") + // create empty file yml + utils.CreateSettingFile(setingFile) } endpoints = config.ProxyGateway.EnpointsProxy portProxy = config.ProxyGateway.Port @@ -38,6 +40,7 @@ func init() { numcpu := runtime.NumCPU() runtime.GOMAXPROCS(numcpu) } + func main() { flag.StringVar(&service, "type", service, "Main Service default is proxy") flag.IntVar(&portProxy, "portProxy", portProxy, "Port to serve to run proxy") diff --git a/internal/utils/security.go b/internal/utils/security.go index ce775b4..2a03fd5 100644 --- a/internal/utils/security.go +++ b/internal/utils/security.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "fmt" "math/rand" + "os" "time" ) @@ -35,3 +36,36 @@ func StringWithCharset() string { } return string(b) } + +func CreateSettingFile(setingFile string) { + f, err := os.Create(fmt.Sprintf("./%s", setingFile)) + ymldata := ` +proxy: + host_proxy: 0.0.0.0 + port_proxy: 5000 + security: + type: jwt # apikey|jwt|none + secret_key: key00 # apikey jwtkey this value can be replace by genkey command + # maps of microservices with routes + services_proxy: + - name: microA + host_uri: http://localhost:3000 + endpoints: + - path_endpoints: /api/v1/health/ + path_proxy: /health/ + path_protected: false + ` + if err != nil { + LogError(err.Error()) + } + + defer f.Close() + + data := []byte(fmt.Sprintf("apikey:%s", ymldata)) + + _, err = f.Write(data) + + if err != nil { + LogError(err.Error()) + } +}