Skip to content

Commit

Permalink
feature: Support to change port
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Aug 27, 2017
1 parent 49eacb0 commit c9c7895
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
)

func Run(config *proxy.Config) {
fmt.Println("Ergo Proxy listening on port: 2000")
fmt.Println("Ergo Proxy listening on port: " + config.Port)
proxy.ServeProxy(config)
}
20 changes: 11 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ Usage:
Options:
-h Shows this message.
-v Shows ergo's version.
Run:
-p Set ports to proxy
`

func main() {
var command string = "run"

if len(os.Args) > 1 {
command = os.Args[1]
}

help := flag.Bool("h", false, "Shows ergo's help.")
version := flag.Bool("v", false, "Shows ergo's version.")

flag.Parse()

if *help {
Expand All @@ -48,9 +44,15 @@ func main() {
os.Exit(0)
}

config := proxy.LoadConfig("./.ergo")
config := proxy.NewConfig()

command := flag.NewFlagSet(os.Args[1], flag.ExitOnError)

command.StringVar(&config.Port, "p", "2000", "Set port to the proxy")
command.StringVar(&config.Port, "p", "2000", "Set port to the proxy")

switch command {
config.Services = proxy.LoadConfig("./.ergo")
switch os.Args[1] {
case "list":
commands.List(config)

Expand Down
19 changes: 11 additions & 8 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ func (c *Config) GetService(host string) *Service {
return nil
}

func LoadConfig(filepath string) *Config {
func NewConfig() *Config {
return &Config{
Port: "2000",
Domain: ".dev",
UrlPattern: `.*\.dev$`,
Services: nil,
}
}

func LoadConfig(filepath string) []Service {
file, e := os.Open(filepath)
defer file.Close()
if e != nil {
Expand All @@ -58,11 +67,5 @@ func LoadConfig(filepath string) *Config {
}
}

return &Config{
Port: "2000",
Domain: ".dev",
UrlPattern: `.*\.dev$`,
Services: services,
}

return services
}
3 changes: 2 additions & 1 deletion proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

func TestWhenHasErgoFile(t *testing.T) {
config := LoadConfig("../.ergo")
config := NewConfig()
config.Services = LoadConfig("../.ergo")

t.Run("It loads the services redirections", func(t *testing.T) {
expected := 3
Expand Down
3 changes: 2 additions & 1 deletion proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

func TestWhenHasCollectionFile(t *testing.T) {
config := LoadConfig("../.ergo")
config := NewConfig()
config.Services = LoadConfig("../.ergo")
proxy := NewErgoProxy(config)

t.Run("it redirects foo.dev to localhost 3000", func(t *testing.T) {
Expand Down

0 comments on commit c9c7895

Please sign in to comment.