Skip to content

Commit

Permalink
feat: add create setting file when doesn`t exist
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Jul 6, 2021
1 parent 906c333 commit 63f4c7f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
13 changes: 8 additions & 5 deletions cmd/goproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
34 changes: 34 additions & 0 deletions internal/utils/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"fmt"
"math/rand"
"os"
"time"
)

Expand Down Expand Up @@ -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())
}
}

0 comments on commit 63f4c7f

Please sign in to comment.