Skip to content

Commit

Permalink
feat: adds command local to bind localhost:80 (#151)
Browse files Browse the repository at this point in the history
* feat: adds command local to bind localhost:80

* docs fixes typo

* remove logger

* only change port & domain for local cmd

* docs: better feature explanation

* feat: adds /__ergo__/ endpoint

* adds note about sudo
  • Loading branch information
cristianoliveira authored Apr 9, 2024
1 parent cb8cace commit 4ad6502
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,17 @@ Make sure you have `$GOPATH/bin` in your path: `export PATH=$PATH:$GOPATH/bin`

Ergo looks for a `.ergo` file inside the current directory. It must contain the names and URL of the services following the same format as `/etc/hosts` (`domain`+`space`+`url`). The main difference is it also considers the specified port.

### Simplest Setup
### Subdomains to http://localhost

Run `ergo local` it'll attempt to bind to `localhost:80` and listen for requests to your services as "subdmains" eg. `http://serviceone.localhost` and `http://servicetwo.localhost`. (Check [examples](https://github.com/cristianoliveira/ergo/tree/master/examples) for more)

**Note:** It __may__ requires sudo to bind to port 80.

You can give it a different port by `ergo local -p <port>` and access it through `http://serviceone.localhost:<port>`.

You can also add a different loopback in `/etc/hosts` like `echo '127.0.0.1 localapp' >> /etc/hosts` and run `ergo local -domain localapp` to access your services through `http://serviceone.localapp` and `http://servicetwo.localapp`.

### Setting up as a webproxy

**You need to set the `http://127.0.0.1:2000/proxy.pac` configuration on your system network config.**

Expand Down
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ Then access: `http://serviceone.dev` and `http://servicetwo.dev`
On `./examples/.ergo` are the configured domains.

Simple :)

### Using localhost loopback

If you want to use subdomains with localhost loopback run the following commands:

```bash
# It requires sudo to bind to port 80
sudo ergo local
```

Then access: `http://serviceone.localhost` and `http://servicetwo.localhost`
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ func prepareSubCommand(args []string) (commands.Command, *proxy.Config) {

return commands.RunCommand{}, config

case "local":
if config.Domain == "" {
config.Domain = ".localhost"
}
if config.Port == "" {
config.Port = "80"
}

command.Parse(args[2:])

return commands.RunCommand{}, config

case "add":
if len(args) < 4 {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewErgoProxy(config *Config) *httputil.ReverseProxy {
fmt.Println(formatRequest(req))
}

service, err := config.GetService(req.URL.Host)
service, err := config.GetService(req.Host)
if err != nil {
fmt.Printf("Error getting service: %v", err)
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func ServeProxy(config *Config) error {

http.HandleFunc("/proxy.pac", proxy(config))

http.HandleFunc("/_ergo/list", list(config))
http.HandleFunc("/__ergo__/", list(config))

http.Handle("/", NewErgoProxy(config))

Expand Down

0 comments on commit 4ad6502

Please sign in to comment.